Skip to content

Commit c7c1d10

Browse files
committed
fix: koa not serving app for non-root url
1 parent 88fc61d commit c7c1d10

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

meteor/server/api/rest/koa.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare module 'http' {
1818
}
1919

2020
const rootRouter = new KoaRouter()
21+
const boundRouterPaths: string[] = []
2122

2223
Meteor.startup(() => {
2324
const koaApp = new Koa()
@@ -51,7 +52,8 @@ Meteor.startup(() => {
5152

5253
// serve the webui through koa
5354
// This is to avoid meteor injecting anything into the served html
54-
koaApp.use(staticServe(public_dir))
55+
const webuiServer = staticServe(public_dir)
56+
koaApp.use(webuiServer)
5557
logger.debug(`Serving static files from ${public_dir}`)
5658

5759
// Serve the meteor runtime config
@@ -66,9 +68,31 @@ Meteor.startup(() => {
6668
})
6769

6870
koaApp.use(rootRouter.routes()).use(rootRouter.allowedMethods())
71+
72+
koaApp.use(async (ctx, next) => {
73+
if (ctx.method !== 'GET') return next()
74+
75+
// Don't use the fallback for certain paths
76+
if (ctx.path.startsWith('/assets/')) return next()
77+
78+
// Don't use the fallback for anything handled by another router
79+
// This does not feel efficient, but koa doesn't appear to have any shared state between the router handlers
80+
for (const bindPath of boundRouterPaths) {
81+
if (ctx.path.startsWith(bindPath)) return next()
82+
}
83+
84+
// fallback to the root file
85+
ctx.path = '/'
86+
return webuiServer(ctx, next)
87+
})
6988
})
7089

7190
export function bindKoaRouter(koaRouter: KoaRouter, bindPath: string): void {
91+
// Track this path as having a router
92+
let bindPathFull = bindPath
93+
if (!bindPathFull.endsWith('/')) bindPathFull += '/'
94+
boundRouterPaths.push(bindPathFull)
95+
7296
rootRouter.use(bindPath, koaRouter.routes()).use(bindPath, koaRouter.allowedMethods())
7397
}
7498

0 commit comments

Comments
 (0)