Skip to content

Commit e8b41c9

Browse files
authored
Implement URL rewriting middleware for .html files
Added middleware to rewrite URLs for .html files.
1 parent 222d459 commit e8b41c9

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

packages/demo/public/proxy.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
import express from "express";
22
import fetch from "node-fetch";
3+
import fs from "fs";
4+
import path from "path";
35

46
const app = express();
57
const port = process.env.PORT || 8000;
68

79
console.log("ENV PORT:", process.env.PORT);
810

11+
// Middleware to rewrite URLs - add .html if needed (like GitHub Pages)
12+
app.use((req, res, next) => {
13+
// Skip if it's the proxy endpoint, health check, or already has an extension
14+
if (req.path === '/proxy' || req.path === '/health' || req.path.includes('.')) {
15+
return next();
16+
}
17+
18+
// If path doesn't end with .html, try to serve the .html version
19+
if (!req.path.endsWith('.html') && !req.path.endsWith('/')) {
20+
const htmlPath = req.path + '.html';
21+
22+
if (fs.existsSync(path.join('.', htmlPath))) {
23+
req.url = htmlPath;
24+
}
25+
}
26+
27+
next();
28+
});
29+
930
// Serve static files (all your gh-pages HTML, CSS, JS, assets at repo root)
1031
app.use(express.static('.'));
1132

0 commit comments

Comments
 (0)