Skip to content

fs-routes: common-prefix stripping can misdetect the routes root and shift all routes #139

Description

@uhyo

Problem

modulesToRouteFiles in packages/static/src/fs-routes/tree.ts infers the routes root as the longest common directory prefix of the import.meta.glob keys. When every page happens to live under one shared subdirectory, that subdirectory is incorrectly treated as part of the routes root and stripped, shifting all routes up one level.

Reproductions (verified against modulesToRouteFiles directly):

  • {"./pages/blog/page.tsx", "./pages/blog/post/page.tsx"} → file paths page.tsx, post/page.tsx → routes / and /post instead of /blog and /blog/post.
  • A single nested page {"./pages/docs/page.tsx"}page.tsx → route / instead of /docs.

The existing unit tests only cover globs that include a file at the routes root, which is why this isn't caught.

Suggested fix

The plugin already knows the exact routes directory: globBase computed in packages/static/src/plugin/index.ts (the same value used to emit the glob pattern in the virtual:funstack/entries module). Pass it through to createFsRoutesEntries and strip it deterministically. For direct users of createFsRoutesEntries, accept an optional base option and keep the common-prefix heuristic only as a fallback.

Should land together with unit tests for the two cases above.

Found during a framework audit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions