Skip to content
Open
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
10 changes: 5 additions & 5 deletions packages/fresh/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function applyCommandsInner<State>(
case CommandType.Middleware: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
mergePath(basePath, cmd.pattern, true),
cmd.includeLastSegment,
);
segment.middlewares.push(...cmd.fns);
Expand All @@ -250,7 +250,7 @@ function applyCommandsInner<State>(
case CommandType.Error: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
mergePath(basePath, cmd.pattern, true),
cmd.includeLastSegment,
);
segment.errorRoute = cmd.item;
Expand All @@ -263,7 +263,7 @@ function applyCommandsInner<State>(
case CommandType.Layout: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
mergePath(basePath, cmd.pattern, true),
cmd.includeLastSegment,
);
segment.layout = {
Expand All @@ -277,8 +277,8 @@ function applyCommandsInner<State>(
const { pattern, route, config } = cmd;
const segment = getOrCreateSegment(
root,
pattern,
cmd.includeLastSegment,
mergePath(basePath, pattern, true),
cmd.includeLastSegment || basePath !== "",
);
const fns = segmentToMiddlewares(segment);

Expand Down
35 changes: 35 additions & 0 deletions packages/fresh/src/fs_routes_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,41 @@ Deno.test("fsRoutes - middleware", async () => {
expect(await res.text()).toEqual("ok");
});

Deno.test("fsRoutes - middleware is scoped to each mount", async () => {
let calls = 0;
const files: Record<string, string | FreshFsMod<unknown>> = {
"routes/index.ts": { handler: () => new Response("ok") },
"routes/_middleware.ts": {
handler: (ctx: Context<unknown>) => {
calls++;
return ctx.next();
},
},
};
const fs = createFakeFs(files);
const routeDir = path.join(fs.cwd(), "routes");
const rawFiles = await crawlRouteDir(fs, routeDir, [], () => {});
const fsFiles = rawFiles.map((file) => ({
...file,
// deno-lint-ignore no-explicit-any
mod: files[file.filePath] as any,
}));
const app = new App<unknown>()
.fsRoutes("/a/:id")
.fsRoutes("/b/:id")
.fsRoutes();
setBuildCache(app, new MockBuildCache(fsFiles, "development"), "development");
const server = new FakeServer(app.handler());

for (const path of ["/", "/a/foo", "/b/foo"]) {
calls = 0;
const res = await server.get(path);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
expect(calls).toEqual(1);
}
});

Deno.test("fsRoutes - nested middlewares", async () => {
const server = await createServer<{ text: string }>({
"routes/_middleware.ts": {
Expand Down