-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-html-ext.mjs
More file actions
55 lines (50 loc) · 2.09 KB
/
Copy pathfix-html-ext.mjs
File metadata and controls
55 lines (50 loc) · 2.09 KB
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
#!/usr/bin/env node
// Post-build step: align exported filenames with the .html routes.
//
// Page routes carry the legacy Read the Docs .html suffix
// (/en/latest/howto/select.html). For such a route Next's static export
// writes:
//
// howto/select.html.html the page HTML (export appends ".html" blindly)
// howto/select.html.txt the full-page RSC payload for client navigation
// howto/select.html/ per-segment prefetch payloads (__next.*.txt)
//
// The page must be served from the exact path the segment directory
// occupies. A filesystem can't hold both, but object storage can (keys are
// flat), so: rename the page file to the key the URL names, and move the
// segment directories aside into out-segments/ — a deploy uploads both
// trees into the same prefix. On hosts serving out/ alone (local
// preview, GitHub Pages) the segment prefetch probes 404 and the client
// falls back to the full-page .txt payload; navigation still works.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const OUT_DIR = path.join(ROOT, 'out');
const SEGMENTS_DIR = path.join(ROOT, 'out-segments');
if (!fs.existsSync(OUT_DIR)) {
console.error('fix-html-ext: out/ not found — run next build first');
process.exit(1);
}
fs.rmSync(SEGMENTS_DIR, { recursive: true, force: true });
let renamed = 0;
let moved = 0;
(function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name.endsWith('.html')) {
const dest = path.join(SEGMENTS_DIR, path.relative(OUT_DIR, p));
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.renameSync(p, dest);
moved++;
} else {
walk(p);
}
} else if (entry.name.endsWith('.html.html')) {
fs.renameSync(p, p.slice(0, -'.html'.length));
renamed++;
}
}
})(OUT_DIR);
console.log(`fix-html-ext: renamed ${renamed} pages, moved ${moved} segment dirs to out-segments/`);