diff --git a/src/app.ts b/src/app.ts index 04507f40..93e541f0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -243,7 +243,7 @@ export class App { fns = middlewares; } - const segment = getOrCreateSegment(this.#root, path, false); + const segment = getOrCreateSegment(this.#root, path, true); segment.middlewares.push(...fns); return this; diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index 52fd6089..400848da 100644 --- a/src/plugins/fs_routes/mod_test.tsx +++ b/src/plugins/fs_routes/mod_test.tsx @@ -1512,3 +1512,37 @@ Deno.test("fsRoutes - warn on _layout handler", async () => { expect.any(String), ); }); + +// Middleware issue in 2.0.0-alpha.40 +Deno.test("fsRoutes - call correct middleware", async () => { + const server = await createServer<{ text: string }>({ + "routes/_middleware.ts": { + handler: async function middleware(ctx) { + ctx.state.text = "_middleware"; + return await ctx.next(); + }, + }, + "routes/index.ts": { + default: async (ctx) => await new Response(ctx.state.text), + }, + "routes/admin/_middleware.ts": { + handler: async function adminMiddleware(ctx) { + ctx.state.text += "admin/_middleware"; + return await ctx.next(); + }, + }, + "routes/foo/index.ts": { + handler: (ctx) => { + return new Response(ctx.state.text); + }, + }, + }); + + let res = await server.get("/"); + let text = await res.text(); + expect(text).toEqual("_middleware"); + + res = await server.get("/foo"); + text = await res.text(); + expect(text).toEqual("_middleware"); +});