Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

5.4 KB
Raw Blame
// Importing env files here to validate on build
import path from "node:path";

import "@homarr/auth/env";
import "@homarr/core/infrastructure/db/env";
import "@homarr/common/env";
import "@homarr/core/infrastructure/logs/env";
import "@homarr/docker/env";

import type { NextConfig } from "next";
import createNextIntlPlugin from "next-intl/plugin";

// Package path does not work... so we need to use relative path
const withNextIntl = createNextIntlPlugin({
  requestConfig: "../../packages/translation/src/request.ts",
});

const getDevelopmentServiceAliases = () => {
  if (process.env.NODE_ENV !== "development") {
    return undefined;
  }

  return {
    "@homarr/tasks": "./src/instrumentation-noop.ts",
    "@homarr/websocket": "./src/instrumentation-noop.ts",
  };
};

const contentSecurityPolicy = `
  default-src 'self';
  script-src * 'unsafe-inline' 'unsafe-eval';
  worker-src * blob:;
  base-uri 'self';
  connect-src *;
  style-src * 'unsafe-inline';
  frame-ancestors *;
  frame-src *;
  form-action 'self';
  img-src * data:;
  font-src * data:;
  media-src * data: blob:;
`
  .replace(/\s{2,}/g, " ")
  .trim();

const mcpContentSecurityPolicy = contentSecurityPolicy.replace("frame-ancestors *;", "frame-ancestors 'none';");
const mcpConsentContentSecurityPolicy = mcpContentSecurityPolicy.replace(
  "form-action 'self';",
  "form-action 'self' http: https:;",
);

const nextConfig: NextConfig = {
  // Next previews otherwise create agent instruction files in the application
  // directory during development.
  agentRules: false,
  env: {
    HOMARR_VERSION: process.env.HOMARR_VERSION ?? "unknown",
  },
  output: "standalone",
  reactStrictMode: true,
  // react compiler breaks mantine-react-table, so disabled for now
  //reactCompiler: true,
  /** We already do typechecking as separate tasks in CI */
  typescript: { ignoreBuildErrors: true },
  /**
   * dockerode is required in the external server packages because of https://github.com/homarr-labs/homarr/issues/612
   * isomorphic-dompurify and jsdom are required, see https://github.com/kkomelin/isomorphic-dompurify/issues/356
   */
  serverExternalPackages: ["dockerode", "isomorphic-dompurify", "jsdom", "better-sqlite3"],
  experimental: {
    optimizePackageImports: ["@mantine/core", "@mantine/hooks", "@tabler/icons-react"],
    preloadEntriesOnStart: true,
    turbopackFileSystemCacheForBuild: true,
    useTypeScriptCli: true,
  },
  turbopack: {
    root: path.resolve(import.meta.dirname, "../.."),
    // Development runs tasks and WebSocket as separate processes. These aliases
    // keep their production-only instrumentation imports out of the dev graph.
    resolveAlias: getDevelopmentServiceAliases(),
  },
  transpilePackages: ["@homarr/ui", "@homarr/notifications", "@homarr/modals", "@homarr/spotlight", "@homarr/widgets"],
  images: {
    localPatterns: [
      {
        pathname: "/**",
        search: "",
      },
    ],
  },
  async rewrites() {
    return [
      { source: "/.well-known/:path*", destination: "/well-known/:path*" },
      {
        source: "/api/mcp/:transport/.well-known/:path*",
        destination: "/well-known/:path*/api/mcp/:transport",
      },
    ];
  },
  // skipcq: JS-0116
  // eslint-disable-next-line @typescript-eslint/require-await,no-restricted-syntax
  async headers() {
    return [
      {
        source: "/tabler-icons/:version/:asset",
        headers: [{ key: "Cache-Control", value: "public, max-age=31536000, immutable" }],
      },
      {
        source: "/sw.js",
        headers: [
          { key: "Content-Type", value: "application/javascript; charset=utf-8" },
          { key: "Cache-Control", value: "no-cache, no-store, must-revalidate" },
          { key: "Content-Security-Policy", value: "default-src 'self'; script-src 'self'" },
        ],
      },
      {
        source: "/(.*)", // Apply CSP to all routes
        headers: [
          {
            key: "Accept-CH",
            value: "Sec-CH-Viewport-Width",
          },
          {
            key: "Content-Security-Policy",
            // worker-src / media-src with blob: is necessary for video.js, see https://github.com/homarr-labs/homarr/issues/3912 and https://stackoverflow.com/questions/65792855/problem-with-video-js-and-content-security-policy-csp
            value: contentSecurityPolicy,
          },
        ],
      },
      {
        source: "/api/mcp/oauth/:path*",
        locale: false,
        headers: [
          { key: "Content-Security-Policy", value: mcpContentSecurityPolicy },
          { key: "X-Frame-Options", value: "DENY" },
          { key: "Referrer-Policy", value: "no-referrer" },
          { key: "Cache-Control", value: "no-store" },
        ],
      },
      ...["/auth/mcp/:path*", "/:locale/auth/mcp/:path*"].map((source) => ({
        source,
        locale: false as const,
        headers: [
          { key: "Content-Security-Policy", value: mcpContentSecurityPolicy },
          { key: "X-Frame-Options", value: "DENY" },
          { key: "Referrer-Policy", value: "no-referrer" },
          { key: "Cache-Control", value: "no-store" },
        ],
      })),
      ...["/auth/mcp", "/:locale/auth/mcp"].map((source) => ({
        source,
        locale: false as const,
        headers: [
          { key: "Content-Security-Policy", value: mcpConsentContentSecurityPolicy },
          { key: "Referrer-Policy", value: "strict-origin" },
        ],
      })),
    ];
  },
};

export default withNextIntl(nextConfig);