Skip to content

Open-source status page, incident response and uptime monitoring

14.8 KB
Raw Blame
import { readFileSync } from "node:fs";
import { join } from "node:path";

import { withSentryConfig } from "@sentry/nextjs";
import type { NextConfig } from "next";

// Read the MCP server version at build time from apps/server/package.json so the
// `serverInfo.version` we publish in /.well-known/mcp/server-card.json never drifts.
// Falls back to "0.0.0" if the sibling app isn't present in the build context (e.g.
// a deploy that excludes apps/server). Exposed to runtime via Next's `env` config.
function readMcpServerVersion(): string {
  try {
    const pkgPath = join(__dirname, "..", "server", "package.json");
    const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as {
      version?: string;
    };
    return pkg.version ?? "0.0.0";
  } catch {
    return "0.0.0";
  }
}

// REMINDER: avoid Clickjacking attacks by setting the frame-ancestors directive
const securityHeaders = [
  {
    key: "Content-Security-Policy",
    value: "frame-ancestors 'self' https://shoogle.dev",
  },
];

// Link headers for agent discovery (RFC 8288 / RFC 8631).
// service-doc: human-readable docs. service-desc: machine-readable API description.
const homepageLinkHeader = [
  '</.well-known/api-catalog>; rel="api-catalog"; type="application/linkset+json"',
  '</.well-known/agent-skills/index.json>; rel="agent-skills"; type="application/json"',
  '</.well-known/mcp.json>; rel="mcp-server"; type="application/json"',
  '<https://www.openstatus.dev/docs>; rel="service-doc"; type="text/html"',
  '<https://www.openstatus.dev/openapi.json>; rel="service-desc"; type="application/json"',
  '<https://www.openstatus.dev/llms.txt>; rel="describedby"; type="text/plain"',
  '<https://www.openstatus.dev/llms-full.txt>; rel="alternate"; type="text/plain"; title="llms-full"',
  '<https://www.openstatus.dev/terms>; rel="terms-of-service"',
  '<https://www.openstatus.dev/privacy>; rel="privacy-policy"',
].join(", ");

const agentDiscoveryHeaders = [
  {
    key: "Link",
    value: homepageLinkHeader,
  },
];

// ICON_SET=nucleo swaps the icon set at resolution time (see packages/icons)
const useNucleoIcons = process.env.ICON_SET === "nucleo";

// `text/markdown` anywhere in Accept, unless its q-value is 0 (an explicit
// refusal; `q=0.5` and friends still count). Next anchors `has` values as
// `^...$`, so the `(.*)` on both sides stay.
const ACCEPTS_MARKDOWN =
  "(.*)text/markdown(?!\\s*;\\s*q=0(?:\\.0*)?\\s*(?:,|$))(.*)";

/** @type {import('next').NextConfig} */
const nextConfig: NextConfig = {
  reactStrictMode: true,
  transpilePackages: ["@openstatus/ui", "@openstatus/api", "next-mdx-remote"],
  turbopack: useNucleoIcons
    ? { resolveAlias: { "@openstatus/icons": "@openstatus/icons/nucleo" } }
    : undefined,
  webpack: (config) => {
    if (useNucleoIcons) {
      config.resolve.alias["@openstatus/icons$"] = "@openstatus/icons/nucleo";
    }
    return config;
  },
  env: {
    OPENSTATUS_MCP_SERVER_VERSION: readMcpServerVersion(),
  },
  outputFileTracingIncludes: {
    "/": [
      "./node_modules/.pnpm/@google-cloud/tasks/build/esm/src/**/*.json",
      "./node_modules/@google-cloud/tasks/build/esm/src/**/*.js",
    ],
    "/.well-known/agent-skills/index.json": [
      "./public/.well-known/agent-skills/**/*.md",
    ],
  },
  experimental: {
    turbopackScopeHoisting: false,
    // serverMinification:false,
    // barrel-optimize only when unaliased — the rewrite would bypass the nucleo alias
    optimizePackageImports: useNucleoIcons ? [] : ["@openstatus/icons"],
  },
  serverExternalPackages: ["@google-cloud/tasks"],
  expireTime: 180, // 3 minutes
  logging: {
    fetches: {
      fullUrl: true,
    },
  },
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "**.public.blob.vercel-storage.com",
      },
      {
        protocol: "https",
        hostname: "screenshot.openstat.us",
      },
      {
        protocol: "https",
        hostname: "www.openstatus.dev",
      },
    ],
  },
  async headers() {
    return [
      { source: "/(.*)", headers: securityHeaders },
      { source: "/", headers: agentDiscoveryHeaders },
    ];
  },
  async redirects() {
    return [
      {
        source: "/:path*",
        has: [{ type: "host", value: "docs.openstatus.dev" }],
        destination: "https://www.openstatus.dev/docs/:path*",
        permanent: true,
      },
      {
        source: "/docs/guides/how-to-connect-openstatus-to-claude-code",
        destination: "/docs/guides/how-to-connect-openstatus-to-your-agent",
        permanent: true,
      },
      {
        source: "/docs/reference/api",
        destination: "https://api.openstatus.dev/openapi",
        permanent: true,
      },
      {
        source: "/legal/terms",
        destination: "/terms",
        permanent: true,
      },
      {
        source: "/legal/privacy",
        destination: "/privacy",
        permanent: true,
      },
      {
        source: "/features/monitoring",
        destination: "/uptime-monitoring",
        permanent: true,
      },
      {
        source: "/features/status-page",
        destination: "/status-page",
        permanent: true,
      },
      {
        source: "/api-monitoring",
        destination: "/uptime-monitoring",
        permanent: true,
      },
      {
        source: "/monitoring-as-code",
        destination: "/uptime-monitoring",
        permanent: true,
      },
      {
        source: "/private-locations",
        destination: "/uptime-monitoring",
        permanent: true,
      },
      {
        source: "/app/:path*",
        destination: "https://app.openstatus.dev/",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-create-monitor",
        destination: "/docs/tutorial/create-your-first-monitor",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-create-status-page",
        destination: "/docs/tutorial/create-your-first-status-page",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-configure-status-page",
        destination: "/docs/guides/how-to-configure-status-page",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-setup-slack-agent",
        destination: "/docs/guides/how-to-setup-slack-agent",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-create-private-location",
        destination: "/docs/guides/how-to-create-private-location",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-import-status-page",
        destination: "/docs/guides/how-to-import-status-page",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-manage-openstatus-with-terraform-cli",
        destination: "/docs/guides/how-to-manage-openstatus-with-terraform",
        permanent: true,
      },
      {
        source: "/docs/guides/how-to-manage-openstatus-with-terraform-cli",
        destination: "/docs/guides/how-to-manage-openstatus-with-terraform",
        permanent: true,
      },
      {
        source: "/docs/tutorial/how-to-connect-openstatus-to-claude-code",
        destination: "/docs/guides/how-to-connect-openstatus-to-your-agent",
        permanent: true,
      },
      {
        source: "/compare/atlassian-statuspage",
        destination: "/guides/top-five-atlassian-statuspage-alternatives",
        permanent: true,
      },
      {
        source: "/compare/instatus",
        destination: "/guides/top-five-instatus-alternatives",
        permanent: true,
      },
      {
        source: "/compare/pingdom",
        destination: "/guides/top-five-pingdom-alternatives",
        permanent: true,
      },
      {
        source: "/compare/uptime-kuma",
        destination: "/guides/hosted-uptime-kuma-alternative",
        permanent: true,
      },
    ];
  },
  async rewrites() {
    return {
      beforeFiles: [
        // `/openapi.json` is the path agents probe for an API description, and
        // they probe it on the site they were pointed at. The spec is generated
        // in apps/server, so proxy rather than keep a second copy in sync.
        // Host-scoped and ahead of the status-page rules below: those catch
        // every path on any host once `sp_mode=new` is set, and a customer's
        // custom domain has no business serving the openstatus API spec.
        {
          source: "/openapi.json",
          has: [{ type: "host", value: "(www\\.)?openstatus\\.dev" }],
          destination: "https://api.openstatus.dev/openapi.json",
        },
        {
          source: "/openapi.yaml",
          has: [{ type: "host", value: "(www\\.)?openstatus\\.dev" }],
          destination: "https://api.openstatus.dev/openapi.yaml",
        },
        {
          source: "/status-page/themes/:path*",
          destination: "https://www.stpg.dev/:path*",
        },
        {
          source: "/:path*",
          has: [
            {
              type: "host",
              value: "themes.openstatus.dev",
            },
          ],
          destination: "https://www.stpg.dev/:path*",
        },
        // New design: proxy app routes to external host with slug prefix
        {
          source: "/:path*",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "(?<slug>[^.]+)\\.(openstatus\\.dev|localhost)",
            },
          ],
          destination: "https://:slug.stpg.dev/:path*",
        },
        // Handle custom domains (e.g., status.mxkaske.dev)
        {
          source:
            "/:path((?!api|assets|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?!.*\\.openstatus\\.dev$)(?!openstatus\\.dev$)$",
            },
          ],
          destination: "https://www.stpg.dev/:path*",
        },
        // enfore routes to avoid infinite redirects - https://github.com/vercel/vercel/issues/6126#issuecomment-823523122
        // testing with https://validator.w3.org/feed/check.cgi
        {
          source: "/feed/rss",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?!.*\\.openstatus\\.dev$)(?!openstatus\\.dev$)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain/feed/rss",
        },
        {
          source: "/feed/atom",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?!.*\\.openstatus\\.dev$)(?!openstatus\\.dev$)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain/feed/atom",
        },
        {
          source: "/feed/rss",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain/feed/rss",
        },
        {
          source: "/feed/atom",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain/feed/atom",
        },
        {
          source:
            "/:path((?!api|assets|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|badge|feed|events|monitors|protected|verify).*)",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain*",
        },
        {
          source:
            "/:path((?!api|assets|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value: "^(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/:domain/:path*",
        },
        // Handle API routes for custom domains
        {
          source: "/api/:path*",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value:
                "^(?!.*\\.openstatus\\.dev$)(?!openstatus\\.dev$)(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/api/:path*",
        },
        // Handle static assets for custom domains
        {
          source: "/_next/:path*",
          has: [
            { type: "cookie", key: "sp_mode", value: "new" },
            {
              type: "host",
              value:
                "^(?!.*\\.openstatus\\.dev$)(?!openstatus\\.dev$)(?<domain>.+)$",
            },
          ],
          destination: "https://www.stpg.dev/_next/:path*",
        },
        // Markdown via .md URL suffix (e.g. /blog/foo.md → /api/markdown/blog/foo)
        {
          source: "/:path*\\.md",
          destination: "/api/markdown/:path*",
        },
        // Markdown content negotiation for AI tools. beforeFiles rewrites
        // chain, so skip paths the .md rule above already rewrote — otherwise
        // /pricing.md + Accept: text/markdown ends up at /api/markdown/api/markdown/pricing
        {
          source: "/:path((?!api/markdown(?:/|$)).+)",
          destination: "/api/markdown/:path",
          has: [
            {
              type: "header",
              key: "accept",
              value: ACCEPTS_MARKDOWN,
            },
          ],
        },
        // `.+` above needs at least one path character, so the home page
        // gets its own rule.
        {
          source: "/",
          destination: "/api/markdown",
          has: [
            {
              type: "header",
              key: "accept",
              value: ACCEPTS_MARKDOWN,
            },
          ],
        },
      ],
    };
  },
};

// For detailed options, refer to the official documentation:
// - Webpack plugin options: https://github.com/getsentry/sentry-webpack-plugin#options
// - Next.js Sentry setup guide: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
const sentryConfig = {
  // Prevent log output unless running in a CI environment (helps reduce noise in logs)
  silent: !process.env.CI,
  org: "openstatus",
  project: "openstatus",
  authToken: process.env.SENTRY_AUTH_TOKEN,

  // Upload a larger set of source maps for improved stack trace accuracy (increases build time)
  widenClientFileUpload: true,

  // If set to true, transpiles Sentry SDK to be compatible with IE11 (increases bundle size)
  transpileClientSDK: false,

  // Tree-shake Sentry logger statements to reduce bundle size
  webpack: {
    treeshake: {
      removeDebugLogging: true,
    },
  },
};

export default withSentryConfig(nextConfig, sentryConfig);