|
4 | 4 | // that header, so a worker at /serwist/sw.js would be capped to /serwist/ scope |
5 | 5 | // and never control the app. Copying it to the site root makes its default |
6 | 6 | // scope `/` with no header required, which is what static hosting needs. |
7 | | -import { copyFileSync, existsSync, readdirSync } from "node:fs"; |
| 7 | +import { copyFileSync, existsSync } from "node:fs"; |
8 | 8 |
|
9 | | -// `next build` with `output: "export"` is meant to write the route handler |
10 | | -// output to out/serwist/sw.js, but that export step is unreliable across |
11 | | -// platforms: it lands on disk locally (Windows) yet is silently missing in CI |
12 | | -// (Linux), with the same Next and Node versions. The compiled body is always |
13 | | -// written to .next/server/app/serwist/<param>.body during the build, before and |
14 | | -// independent of the export phase, and is byte-identical to the exported file. |
15 | | -// Fall back to it so the root copy is produced in either environment. |
16 | | -function firstExisting(candidates) { |
17 | | - return candidates.find((p) => existsSync(p)); |
18 | | -} |
19 | | - |
20 | | -const swSrc = firstExisting([ |
21 | | - "out/serwist/sw.js", |
22 | | - ".next/server/app/serwist/sw.js.body", |
23 | | -]); |
24 | | - |
25 | | -if (!swSrc) { |
26 | | - console.error("[copy-sw] service worker not found - did 'next build' run?"); |
27 | | - for (const dir of ["out/serwist", ".next/server/app/serwist"]) { |
28 | | - if (existsSync(dir)) { |
29 | | - console.error(`[copy-sw] ${dir}: ${readdirSync(dir).join(", ")}`); |
30 | | - } |
31 | | - } |
| 9 | +const src = "out/serwist/sw.js"; |
| 10 | +if (!existsSync(src)) { |
| 11 | + console.error(`[copy-sw] ${src} not found - did 'next build' export to out/?`); |
32 | 12 | process.exit(1); |
33 | 13 | } |
34 | 14 |
|
35 | | -copyFileSync(swSrc, "out/sw.js"); |
36 | | -console.log(`[copy-sw] ${swSrc} -> out/sw.js`); |
| 15 | +copyFileSync(src, "out/sw.js"); |
| 16 | +console.log(`[copy-sw] ${src} -> out/sw.js`); |
37 | 17 |
|
38 | 18 | // The worker ends with sourceMappingURL=sw.js.map, which resolves to /sw.js.map |
39 | 19 | // once served from the root, so copy the map alongside it to avoid a 404. A |
40 | 20 | // missing map is not fatal - it only affects debugging. |
41 | | -const mapSrc = firstExisting([ |
42 | | - "out/serwist/sw.js.map", |
43 | | - ".next/server/app/serwist/sw.js.map.body", |
44 | | -]); |
45 | | - |
46 | | -if (mapSrc) { |
| 21 | +const mapSrc = "out/serwist/sw.js.map"; |
| 22 | +if (existsSync(mapSrc)) { |
47 | 23 | copyFileSync(mapSrc, "out/sw.js.map"); |
48 | 24 | console.log(`[copy-sw] ${mapSrc} -> out/sw.js.map`); |
49 | | -} else { |
50 | | - console.warn("[copy-sw] sw.js.map not found, skipping (non-fatal)"); |
51 | 25 | } |
0 commit comments