diff --git a/src/main.ts b/src/main.ts index ecec391..5038cb4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,7 +32,7 @@ const Config = { | ((path: string, req: express.Request) => boolean), transformer: undefined as | undefined - | ((html: string, req: express.Request) => string), + | ((html: string, req: express.Request) => string | Promise), }; type ConfigurationOptions = Partial; @@ -50,7 +50,7 @@ function isStaticFilePath(path: string) { return path.match(/(\.\w+$)|@vite|@id|@react-refresh/); } -function getTransformedHTML(html: string, req: express.Request) { +async function getTransformedHTML(html: string, req: express.Request) { return Config.transformer ? Config.transformer(html, req) : html; } @@ -224,8 +224,15 @@ async function injectViteIndexMiddleware( if (indexPath === undefined) return next(); const template = fs.readFileSync(indexPath, "utf8"); - const html = await server.transformIndexHtml(req.originalUrl, template); - res.send(getTransformedHTML(html, req)); + let html = await server.transformIndexHtml(req.originalUrl, template); + try { + html = await getTransformedHTML(html, req); + res.send(html); + } catch (e) { + console.error(e); + res.status(500); + return next(); + } } }); }