Skip to content

Commit

Permalink
Support async html transformer function
Browse files Browse the repository at this point in the history
Having an async function to transform the html allows for more complex transformations, such as inserting data fetched from a database
  • Loading branch information
peterlama committed Dec 30, 2023
1 parent 1189de8 commit 89caf9f
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>),
};

type ConfigurationOptions = Partial<typeof Config>;
Expand All @@ -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;
}

Expand Down Expand Up @@ -224,8 +224,16 @@ 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();
}
}
});
}
Expand All @@ -234,14 +242,22 @@ async function injectIndexMiddleware(app: core.Express) {
const distPath = await getDistPath();
const config = await getViteConfig();

app.use(config.base, (req, res, next) => {
app.use(config.base, async (req, res, next) => {
if (isIgnoredPath(req.path, req)) return next();

const indexPath = findClosestIndexToRoot(req.path, distPath);
if (indexPath === undefined) return next();

const html = fs.readFileSync(indexPath, "utf8");
res.send(getTransformedHTML(html, req));
let html = fs.readFileSync(indexPath, "utf8");

try {
html = await getTransformedHTML(html, req);
res.send(html);
} catch (e) {
console.error(e);
res.status(500);
return next();
}
});
}

Expand All @@ -263,7 +279,7 @@ async function startServer(server: http.Server | https.Server) {
);

server.on("close", async () => {
await vite.close()
await vite.close();
server.emit("vite:close");
});

Expand Down

0 comments on commit 89caf9f

Please sign in to comment.