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 { MiddlewareType } from 'koa';
|
||||||
import proxy from 'koa-proxies';
|
import proxy from 'koa-proxies';
|
||||||
|
@ -8,7 +8,6 @@ import serveStatic from 'koa-static';
|
||||||
import { isProduction, mountedApps } from '@/env/consts';
|
import { isProduction, mountedApps } from '@/env/consts';
|
||||||
|
|
||||||
const PATH_TO_UI_DIST = '../ui/build/public';
|
const PATH_TO_UI_DIST = '../ui/build/public';
|
||||||
const uiDistFiles = fs.readdirSync(PATH_TO_UI_DIST);
|
|
||||||
|
|
||||||
export default function koaUIProxy<
|
export default function koaUIProxy<
|
||||||
StateT,
|
StateT,
|
||||||
|
@ -17,12 +16,13 @@ export default function koaUIProxy<
|
||||||
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||||
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;
|
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;
|
||||||
|
|
||||||
const developmentProxy: Middleware = proxy('*', {
|
const uiProxy: Middleware = isProduction
|
||||||
target: 'http://localhost:5000',
|
? serveStatic(PATH_TO_UI_DIST)
|
||||||
changeOrigin: true,
|
: proxy('*', {
|
||||||
logs: true,
|
target: 'http://localhost:5000',
|
||||||
});
|
changeOrigin: true,
|
||||||
const staticProxy: Middleware = serveStatic(PATH_TO_UI_DIST);
|
logs: true,
|
||||||
|
});
|
||||||
|
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
// Route has been handled by one of mounted apps
|
// Route has been handled by one of mounted apps
|
||||||
|
@ -31,13 +31,14 @@ export default function koaUIProxy<
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isProduction) {
|
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}`))) {
|
if (!uiDistFiles.some((file) => ctx.request.path.startsWith(`/${file}`))) {
|
||||||
ctx.request.path = '/';
|
ctx.request.path = '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
return staticProxy(ctx, next);
|
return uiProxy(ctx, next);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue