fix: virtual prefix proxy path for vite middleware to work

This commit is contained in:
Alexandre Bodin 2025-01-07 16:04:48 +01:00
parent 110b429be2
commit c2680dea29

View File

@ -57,10 +57,21 @@ const watch = async (ctx: BuildContext): Promise<ViteWatcher> => {
const viteMiddlewares: Core.MiddlewareHandler = (koaCtx, next) => {
return new Promise((resolve, reject) => {
const prefix = ctx.basePath.replace(ctx.adminPath, '').replace(/\/+$/, '');
const originalPath = koaCtx.path;
if (!koaCtx.path.startsWith(prefix)) {
koaCtx.path = `${prefix}${koaCtx.path}`;
}
vite.middlewares(koaCtx.req, koaCtx.res, (err: unknown) => {
if (err) {
reject(err);
} else {
if (!koaCtx.res.headersSent) {
koaCtx.path = originalPath;
}
resolve(next());
}
});