mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(core): load dist files when needed (#123)
This commit is contained in:
parent
61ac65aeb5
commit
1c3338f657
1 changed files with 11 additions and 10 deletions
|
@ -1,4 +1,4 @@
|
|||
import fs from 'fs';
|
||||
import fs from 'fs/promises';
|
||||
|
||||
import { MiddlewareType } from 'koa';
|
||||
import proxy from 'koa-proxies';
|
||||
|
@ -8,7 +8,6 @@ import serveStatic from 'koa-static';
|
|||
import { isProduction, mountedApps } from '@/env/consts';
|
||||
|
||||
const PATH_TO_UI_DIST = '../ui/build/public';
|
||||
const uiDistFiles = fs.readdirSync(PATH_TO_UI_DIST);
|
||||
|
||||
export default function koaUIProxy<
|
||||
StateT,
|
||||
|
@ -17,12 +16,13 @@ export default function koaUIProxy<
|
|||
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;
|
||||
|
||||
const developmentProxy: Middleware = proxy('*', {
|
||||
target: 'http://localhost:5000',
|
||||
changeOrigin: true,
|
||||
logs: true,
|
||||
});
|
||||
const staticProxy: Middleware = serveStatic(PATH_TO_UI_DIST);
|
||||
const uiProxy: Middleware = isProduction
|
||||
? serveStatic(PATH_TO_UI_DIST)
|
||||
: proxy('*', {
|
||||
target: 'http://localhost:5000',
|
||||
changeOrigin: true,
|
||||
logs: true,
|
||||
});
|
||||
|
||||
return async (ctx, next) => {
|
||||
// Route has been handled by one of mounted apps
|
||||
|
@ -31,13 +31,14 @@ export default function koaUIProxy<
|
|||
}
|
||||
|
||||
if (!isProduction) {
|
||||
return developmentProxy(ctx, next);
|
||||
return uiProxy(ctx, next);
|
||||
}
|
||||
|
||||
const uiDistFiles = await fs.readdir(PATH_TO_UI_DIST);
|
||||
if (!uiDistFiles.some((file) => ctx.request.path.startsWith(`/${file}`))) {
|
||||
ctx.request.path = '/';
|
||||
}
|
||||
|
||||
return staticProxy(ctx, next);
|
||||
return uiProxy(ctx, next);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue