Skip to content

Use route with path isn't matched when using file routes with an optional parameter #3545

@jaredparker

Description

@jaredparker

The following works correctly as I expect...

// main.ts

import { App } from "fresh";

export const app = new App();

app.use("/api", async (ctx) => {
  console.log("Matched Middleware"); // Triggered ✓ (GET: /api/1234/endpoint)
  return await ctx.next();
});

app.get("/api/:opt?/endpoint", () => {
  return new Response("Hello, app routing!");
});

However, when mimicking the same setup with .fsRoutes, it doesn't match the middleware.

// main.ts

import { App, staticFiles } from "fresh";

export const app = new App();

app.use(async (ctx) => {
  console.log("Matched Middleware"); // Not Triggered ✗ (GET: /api/1234/endpoint)
  return await ctx.next();
});

app.fsRoutes();
// routes/api/[[opt]]/endpoint.ts

import { define } from "../../../utils.ts";

export const handler = define.handlers({
  GET() {
    return new Response("Hello, file routing!");
  },
});

Please note that this issue doesn't occur if using .use() without a path (e.g. app.use(async (ctx) => {...});). Also, it seems the the issue only occurs with optional routes (e.g. it does correctly match against api/[param]/endpoint.ts).

It could be a misconfiguration on my part or a misunderstanding of file routing as I'm new to fresh, but this seems like an issue within Fresh itself.

To replicate run deno run -Ar jsr:@fresh/init and add the above snippets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions