Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: middlewares not being used with default GET handlers #2807

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ export async function fsRoutes<State>(
isHandlerByMethod(handlers) &&
!Object.keys(handlers).includes("GET");
if (missingGetHandler) {
app.get(routePath, renderMiddleware(components, undefined));
const combined = middlewares.concat(
renderMiddleware(components, undefined),
);
app.get(routePath, ...combined);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/plugins/fs_routes/mod_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,47 @@ Deno.test("fsRoutes - registers default GET route for component without GET hand
);
});

Deno.test("fsRoutes - default GET route works with nested middleware", async () => {
const server = await createServer<{ text: string }>({
"routes/_middleware.ts": {
handler: (ctx) => {
ctx.state.text = "A";
return ctx.next();
},
},
"routes/foo/_middleware.ts": {
handler: (ctx) => {
ctx.state.text += "B";
return ctx.next();
},
},
"routes/foo/noGetHandler.tsx": {
default: (ctx) => {
return <h1>{ctx.state.text}</h1>;
},
handlers: {
POST: () => new Response("POST"),
},
},
});

const postRes = await server.post("/foo/noGetHandler");
expect(postRes.status).toEqual(200);
expect(postRes.headers.get("Content-Type")).toEqual(
"text/plain;charset=UTF-8",
);
expect(await postRes.text()).toEqual("POST");

const getRes = await server.get("/foo/noGetHandler");
expect(getRes.status).toEqual(200);
expect(getRes.headers.get("Content-Type")).toEqual(
"text/html; charset=utf-8",
);
expect(await getRes.text()).toContain(
"<h1>AB</h1>",
);
});

Deno.test("fsRoutes - default GET route doesn't override existing handler", async () => {
const server = await createServer<{ value: boolean }>({
"routes/withGetHandler.tsx": {
Expand Down
Loading