Skip to content

Commit db57d1e

Browse files
committed
adding a postbuild step to allow a trailing slash to resolve index pages
1 parent 678944f commit db57d1e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"docusaurus": "docusaurus",
88
"start": "npm run prebuild && docusaurus start",
99
"dev": "npm run prebuild && docusaurus start",
10-
"build": "npm run prebuild && docusaurus build",
10+
"build": "npm run prebuild && docusaurus build && node scripts/postbuild.js",
1111
"version": "node scripts/cut-version.js",
1212
"swizzle": "docusaurus swizzle",
1313
"deploy": "docusaurus deploy",

scripts/postbuild.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const path = require('node:path');
2+
const fs = require('node:fs/promises');
3+
4+
// Copy /myPath.html to /myPath/index.html
5+
// This ensures both URL patterns work: /myPath and /myPath/
6+
async function generateIndexHtmlFiles(outDir) {
7+
console.log('Post-build: Creating index.html files from hoisted pages...');
8+
9+
// Walk through all directories recursively
10+
async function* walkDirs(dir) {
11+
const dirents = await fs.readdir(dir, { withFileTypes: true });
12+
for (const dirent of dirents) {
13+
const res = path.resolve(dir, dirent.name);
14+
if (dirent.isDirectory()) {
15+
yield res;
16+
yield* walkDirs(res);
17+
}
18+
}
19+
}
20+
21+
const processedFiles = [];
22+
23+
for await (const dirPath of walkDirs(outDir)) {
24+
// Check if there's already an index.html in this directory
25+
const indexPath = path.join(dirPath, 'index.html');
26+
try {
27+
await fs.stat(indexPath);
28+
// index.html exists, skip this directory
29+
continue;
30+
} catch {
31+
// No index.html, continue checking
32+
}
33+
34+
// Check if there's a sibling HTML file with the same name as the directory
35+
const dirName = path.basename(dirPath);
36+
const siblingHtmlPath = path.join(path.dirname(dirPath), `${dirName}.html`);
37+
38+
try {
39+
await fs.stat(siblingHtmlPath);
40+
// Sibling HTML file exists, copy it as index.html
41+
await fs.copyFile(siblingHtmlPath, indexPath);
42+
processedFiles.push(`${dirName}.html → ${dirName}/index.html`);
43+
} catch {
44+
// No sibling HTML file, skip
45+
}
46+
}
47+
48+
if (processedFiles.length > 0) {
49+
console.log(`Post-build: Created ${processedFiles.length} index.html files`);
50+
// Uncomment to see details:
51+
// processedFiles.forEach(f => console.log(` - ${f}`));
52+
} else {
53+
console.log('Post-build: No index.html files needed');
54+
}
55+
}
56+
57+
// Run the post-processing
58+
const buildDir = path.join(__dirname, '..', 'build');
59+
generateIndexHtmlFiles(buildDir).catch(console.error);

0 commit comments

Comments
 (0)