Skip to content

Commit b5fafe4

Browse files
committed
fix: add SPA fallback route to PatchedHonoServerPlugin for handling unknown routes
1 parent cd60499 commit b5fafe4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

apps/console/objectstack.config.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,45 @@ class PatchedHonoServerPlugin extends HonoServerPlugin {
3434
// @ts-ignore
3535
this.start = this.start?.bind(this);
3636
}
37+
38+
async start(ctx: any) {
39+
// @ts-ignore
40+
await super.start(ctx);
41+
42+
// SPA Fallback: Serve index.html for unknown routes (excluding /api)
43+
// @ts-ignore
44+
const app = this.server.getRawApp();
45+
// @ts-ignore
46+
const staticRoot = this.options.staticRoot;
47+
48+
if (staticRoot) {
49+
const fs = require('fs');
50+
const path = require('path');
51+
52+
// Register fallback after serveStatic (which is added in listen/super.start)
53+
app.get('*', async (c: any) => {
54+
// Ignore API calls -> let them 404
55+
if (c.req.path.startsWith('/api') || c.req.path.startsWith('/assets')) {
56+
// return c.notFound(); // Hono's c.notFound() isn't standard in all versions, let's use status
57+
return c.text('Not Found', 404);
58+
}
59+
60+
try {
61+
// Try to serve index.html
62+
// Ensure we resolve relative to CWD or config location
63+
const indexPath = path.resolve(staticRoot, 'index.html');
64+
if (fs.existsSync(indexPath)) {
65+
const indexContent = fs.readFileSync(indexPath, 'utf-8');
66+
return c.html(indexContent);
67+
}
68+
return c.text('SPA Index Not Found', 404);
69+
} catch (e: any) {
70+
return c.text('Server Error: ' + e.message, 500);
71+
}
72+
});
73+
console.log('SPA Fallback route registered for ' + staticRoot);
74+
}
75+
}
3776
}
3877

3978
import ConsolePluginConfig from './plugin.js';

0 commit comments

Comments
 (0)