Skip to content

Commit 771e9f3

Browse files
committed
Enhance PatchedHonoServerPlugin with debugging logs and improved SPA route handling
1 parent f872fd3 commit 771e9f3

File tree

1 file changed

+84
-11
lines changed

1 file changed

+84
-11
lines changed

apps/console/objectstack.config.ts

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class PatchedMSWPlugin extends MSWPlugin {
3434
class PatchedHonoServerPlugin extends HonoServerPlugin {
3535
constructor(...args: any[]) {
3636
super(...args);
37+
console.log('DEBUG: PatchedHonoServerPlugin instantiated');
3738
// @ts-ignore
3839
this.init = this.init.bind(this);
3940

@@ -44,24 +45,45 @@ class PatchedHonoServerPlugin extends HonoServerPlugin {
4445
// Override start with custom logic
4546
// @ts-ignore
4647
this.start = async (ctx: any) => {
47-
// Call original start
48+
// Call original start first to ensure server is created and listening
4849
if (originalStart) {
4950
await originalStart(ctx);
5051
}
51-
52-
// SPA Fallback: Serve index.html for unknown routes (excluding /api)
52+
53+
console.log('DEBUG: Attempting to register SPA routes...');
54+
console.log('DEBUG: Plugin keys:', Object.keys(this));
5355
// @ts-ignore
54-
const app = this.server.getRawApp();
56+
if (this.app) console.log('DEBUG: Found this.app');
5557
// @ts-ignore
56-
const staticRoot = this.options.staticRoot;
58+
if (this.server) console.log('DEBUG: Found this.server');
59+
60+
// Try to find the app from various sources
61+
// @ts-ignore
62+
let app = this.server?.getRawApp?.(); // If this.server exists
5763

58-
if (staticRoot) {
64+
// If not found, try to get from service
65+
if (!app && ctx.getService) {
66+
const httpService = ctx.getService('http-server') || ctx.getService('http.server');
67+
if (httpService) {
68+
// console.log('DEBUG: Found http-server service keys:', Object.keys(httpService));
69+
if (httpService.getRawApp) app = httpService.getRawApp();
70+
else if (httpService.app) app = httpService.app;
71+
}
72+
}
73+
74+
// @ts-ignore
75+
const staticRoot = this.options?.staticRoot || './dist';
76+
77+
if (app && staticRoot) {
5978
const fs = require('fs');
6079
const path = require('path');
6180

81+
console.log('DEBUG: Registering SPA routes for ' + staticRoot);
82+
6283
// Manual Asset Handler to ensure assets are served
6384
app.get('/console/assets/*', async (c: any) => {
6485
const filePath = path.join(path.resolve(staticRoot), c.req.path.replace('/console', ''));
86+
// console.log('DEBUG: Asset Request:', c.req.path, '->', filePath);
6587
if (fs.existsSync(filePath)) {
6688
const ext = path.extname(filePath);
6789
let contentType = 'application/octet-stream';
@@ -82,35 +104,86 @@ class PatchedHonoServerPlugin extends HonoServerPlugin {
82104
return c.redirect('/console/');
83105
});
84106

85-
// Register fallback after serveStatic (which is added in listen/originalStart)
107+
// Register fallback
86108
app.get('/console/*', async (c: any) => {
87109
// Ignore API calls -> let them 404
88110
if (c.req.path.startsWith('/api')) {
89111
return c.text('Not Found', 404);
90112
}
91113

92114
try {
93-
// Try to serve index.html
94-
// Ensure we resolve relative to CWD or config location
115+
// Debugging path resolution
95116
const indexPath = path.resolve(staticRoot, 'index.html');
117+
console.log('DEBUG: Request Path:', c.req.path);
118+
console.log('DEBUG: Serving index from:', indexPath);
119+
console.log('DEBUG: File exists?', fs.existsSync(indexPath));
120+
96121
if (fs.existsSync(indexPath)) {
97122
const indexContent = fs.readFileSync(indexPath, 'utf-8');
98123
return c.html(indexContent);
99124
}
100-
return c.text('SPA Index Not Found', 404);
125+
return c.text('SPA Index Not Found at ' + indexPath, 404);
101126
} catch (e: any) {
127+
console.error('DEBUG: Error serving index:', e);
102128
return c.text('Server Error: ' + e.message, 500);
103129
}
104130
});
105131
console.log('SPA Fallback route registered for ' + staticRoot);
132+
} else {
133+
console.log("DEBUG: failed to register routes. app found:", !!app, "staticRoot:", staticRoot);
106134
}
107135
};
108136
}
109137
}
110138

111139
const FixedConsolePlugin = {
112140
...ConsolePluginConfig,
113-
init: () => {}
141+
init: () => {},
142+
start: async (ctx: any) => {
143+
const httpService = ctx.getService('http-server') || ctx.getService('http.server');
144+
if (httpService) {
145+
const app = httpService.app || httpService.getRawApp?.();
146+
147+
if (app) {
148+
const fs = require('fs');
149+
const path = require('path');
150+
const staticRoot = './dist';
151+
152+
// Manual Asset Handler
153+
app.get('/console/assets/*', async (c: any) => {
154+
const filePath = path.join(path.resolve(staticRoot), c.req.path.replace('/console', ''));
155+
if (fs.existsSync(filePath)) {
156+
const ext = path.extname(filePath);
157+
let contentType = 'application/octet-stream';
158+
if (ext === '.css') contentType = 'text/css';
159+
else if (ext === '.js') contentType = 'application/javascript';
160+
else if (ext === '.json') contentType = 'application/json';
161+
else if (ext === '.png') contentType = 'image/png';
162+
else if (ext === '.svg') contentType = 'image/svg+xml';
163+
164+
const content = fs.readFileSync(filePath);
165+
return c.body(content, 200, { 'Content-Type': contentType });
166+
}
167+
return c.text('Asset Not Found', 404);
168+
});
169+
170+
// Server-side Redirect
171+
app.get('/', (c: any) => c.redirect('/console/'));
172+
app.get('/console', (c: any) => c.redirect('/console/'));
173+
174+
// SPA Fallback
175+
app.get('/console/*', async (c: any) => {
176+
if (c.req.path.startsWith('/api')) return c.text('Not Found', 404);
177+
const indexPath = path.resolve(staticRoot, 'index.html');
178+
if (fs.existsSync(indexPath)) {
179+
return c.html(fs.readFileSync(indexPath, 'utf-8'));
180+
}
181+
return c.text('SPA Index Not Found', 404);
182+
});
183+
console.log('SPA routes registered for Console');
184+
}
185+
}
186+
}
114187
};
115188

116189
// Workaround: Override the built-in api-registry plugin which fails due to async service issue

0 commit comments

Comments
 (0)