File size: 2,840 Bytes
a1cca1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env node
/**
 * Post-build sanity check (runs automatically after `npm run build`).
 *
 * Next.js resolves the App Router folder as the FIRST existing one of
 * `app/`, `src/app/`. A stray top-level `app/` directory (for example a Python
 * package from another project uploaded into the same Space) silently wins over
 * `src/app/`, so the build "succeeds" with only the 404 page and every route
 * returns `404: This page could not be found.`
 *
 * This script turns that silent failure into a loud one, and tells you exactly
 * which folder to rename/remove.
 */
import { existsSync, readdirSync } from "node:fs";
import path from "node:path";

const root = process.cwd();
const nextDir = path.join(root, ".next");

const fail = (lines) => {
  console.error("\n\u001b[31m✖ Build verification failed\u001b[0m");
  for (const line of lines) console.error("  " + line);
  console.error("");
  process.exit(1);
};

if (!existsSync(nextDir)) fail(["`.next/` is missing — did the build run?"]);

// 1) App Router output must contain our real pages, not just the 404 shell.
const appOutput = path.join(nextDir, "server", "app");
const hasAppOutput = existsSync(appOutput);
const hasHomeRoute = existsSync(path.join(appOutput, "page.js"));
const hasApiRoutes = existsSync(path.join(appOutput, "api"));
const hasDashboard = existsSync(path.join(appOutput, "dashboard"));

if (!hasAppOutput || !hasHomeRoute) {
  const strayApp = existsSync(path.join(root, "app"));
  const straySrcApp = existsSync(path.join(root, "src", "app"));
  const lines = [
    "The App Router output is empty: no home page was compiled.",
    `  expected: ${path.relative(root, path.join(appOutput, "page.js"))}`,
    `  src/app/ exists: ${straySrcApp}`,
    `  app/ exists    : ${strayApp}`,
  ];
  if (strayApp) {
    lines.push(
      "",
      "A top-level `app/` folder shadows `src/app/` and Next.js picked it instead.",
      "Rename or delete that folder (for example `app/` -> `pyapp/`) and rebuild.",
    );
  }
  fail(lines);
}

if (!hasApiRoutes || !hasDashboard) {
  fail([
    "Some routes are missing from the build:",
    `  app/api/ compiled : ${hasApiRoutes}`,
    `  app/dashboard/    : ${hasDashboard}`,
    "Remove any stray top-level `app/` folder and rebuild.",
  ]);
}

// 2) A silent 404 usually also means the middleware/proxy was not emitted.
const hasProxy = [
  path.join(nextDir, "server", "middleware.js"),
  path.join(nextDir, "server", "proxy.js"),
  path.join(nextDir, "middleware.js"),
  path.join(nextDir, "proxy.js"),
].some((f) => existsSync(f));
const routes = readdirSync(appOutput, { withFileTypes: true })
  .filter((e) => e.isDirectory() || e.name.endsWith(".js"))
  .length;

console.log(
  `\u001b[32m✔\u001b[0m build verified — ${routes} app-router entries` +
    `, proxy: ${hasProxy ? "yes" : "no"}`,
);