fix: middlewares not being used with default GET handlers (#2807)
I noticed a small bug with the default GET handlers where middlewares were not being run. This pull requests fixes the bug and adds some unit tests for this behavior.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user