ReviewOS

stacks/stx

bun-plugin-stx: nested dynamic routes (`[id]/profile/index.stx`) 404 because the regex includes a literal `/index`

#1696
Closed glennmichael123 opened this 23 days ago · 1 comment
23 days ago

Summary

bun-plugin-stx's dev-server route resolver builds the URL-matching regex by replacing [name] segments and the directory separators of the file path — but does NOT strip the trailing /index from index.stx files. As a result, every nested dynamic route (anything matching [param]/.../index.stx) returns 404 on hard reload, because the URL the user types never carries /index at the end.

Single-segment dynamic routes ([id].stx) work fine because their file path has no trailing /index to strip.

Reproduction

File tree:

resources/views/
  judges/
    [id].stx                     ← /judges/:id        works
    [id]/
      profile/
        index.stx                ← /judges/:id/profile → 404
      reviews/
        index.stx                ← /judges/:id/reviews → 404
      cases/
        index.stx                ← /judges/:id/cases  → 404
  court-houses/
    [id]/
      profile/
        index.stx                ← /court-houses/:id/profile → 404

With bun-plugin-stx serving:

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:4000/judges/35
200

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:4000/judges/35/profile
404                # ← all six leaf-routes 404

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:4000/judges/35/profile/index
200                # ← only matches with explicit `/index`

Root cause

In packages/bun-plugin-stx/src/serve.ts (or dist/serve.js), the dynamic-route loop builds the regex like this:

const fileRouteBase = relativeFilePath.replace(/\.(stx|md|html)$/, '')
const routePattern  = fileRouteBase
  .replace(/\[([^\]]+)\]/g, '([^/]+)')
  .replace(/\//g, '\\/')
const regexPatterns = [new RegExp(`^${routePattern}$`)]

For resources/views/judges/[id]/profile/index.stx:

  • fileRouteBase = judges/[id]/profile/index
  • routePattern = judges/([^/]+)\/profile\/index
  • regex = ^judges/([^/]+)\/profile\/index$

That regex only matches judges/35/profile/index, never judges/35/profile. The static-route resolver upstream tries to match judges/35/profile/index.stx against possibleFiles but the file path on disk is judges/[id]/profile/index.stx (with literal brackets), so it falls through to the dynamic loop — which fails for the reason above.

The pages/-prefixed branch a few lines down has the exact same bug.

Why it's bad

  • Every nested dynamic route 404s on hard reload, on every install. This isn't a project-specific config issue.
  • The single-segment case ([id].stx) works because there's no /index suffix, so users don't catch it until they refactor a route into a subdirectory.
  • Symptom is "I just navigated there and now the link doesn't work on refresh" — extremely confusing because SPA navigation does work (the router does its own resolution).

Suggested fix

Add a /index-stripped alternate to regexPatterns:

const fileRouteBase = relativeFilePath.replace(/\.(stx|md|html)$/, '')
const routePattern  = fileRouteBase
  .replace(/\[([^\]]+)\]/g, '([^/]+)')
  .replace(/\//g, '\\/')
const regexPatterns: RegExp[] = [new RegExp(`^${routePattern}$`)]

// Also match without the trailing `/index` so `[id]/profile/index.stx`
// resolves `/foo/:id/profile`, not just `/foo/:id/profile/index`.
const fileRouteNoIndex = fileRouteBase.replace(/\/index$/, '')
if (fileRouteNoIndex !== fileRouteBase) {
  const noIndexPattern = fileRouteNoIndex
    .replace(/\[([^\]]+)\]/g, '([^/]+)')
    .replace(/\//g, '\\/')
  regexPatterns.push(new RegExp(`^${noIndexPattern}$`))
}

Same fix needs replicating in the pages/-prefixed branch (fileRouteBase.startsWith('pages/')).

Local patch I'm running

       const fileRouteBase = relativeFilePath.replace(/\.(stx|md|html)$/, "");
       const routePattern = fileRouteBase.replace(/\[([^\]]+)\]/g, "([^/]+)").replace(/\//g, "\\/");
       const regexPatterns = [
         new RegExp(`^${routePattern}$`)
       ];
+      // Also match without the trailing `/index` so `[id]/profile/index.stx`
+      // resolves `/judges/35/profile` (not just `/judges/35/profile/index`).
+      const fileRouteNoIndex = fileRouteBase.replace(/\/index$/, "");
+      if (fileRouteNoIndex !== fileRouteBase) {
+        const noIndexPattern = fileRouteNoIndex.replace(/\[([^\]]+)\]/g, "([^/]+)").replace(/\//g, "\\/");
+        regexPatterns.push(new RegExp(`^${noIndexPattern}$`));
+      }
       if (fileRouteBase.startsWith("pages/")) {
         const prettyPattern = fileRouteBase.slice(6).replace(/\[([^\]]+)\]/g, "([^/]+)").replace(/\//g, "\\/");
         regexPatterns.push(new RegExp(`^${prettyPattern}$`));
+        const prettyNoIndex = fileRouteBase.slice(6).replace(/\/index$/, "");
+        if (prettyNoIndex !== fileRouteBase.slice(6)) {
+          const prettyNoIndexPattern = prettyNoIndex.replace(/\[([^\]]+)\]/g, "([^/]+)").replace(/\//g, "\\/");
+          regexPatterns.push(new RegExp(`^${prettyNoIndexPattern}$`));
+        }
+      }

(Patched against pantry/bun-plugin-stx/dist/serve.js ~ line 9334.)

Environment

  • bun-plugin-stx@0.2.64
  • Layout extends layouts/default
  • Bun dev server (./buddy dev shipping bun-plugin-stx serve)

🤖 Filed via Claude Code

23 days ago

Already fixed in commit 1899048537 (fix: address seven reported issues across stx, bun-plugin, components) — shipped in bun-plugin-stx@0.2.65. Regression tests live in packages/bun-plugin/test/issue-fixes.test.ts (6 cases under #1696 — nested dynamic [id]/segment/index.stx matches without /index). Issue stayed open because the commit message referenced #1696 but didn't include the auto-close phrase. Consumers on 0.2.64 should bump to 0.2.65 to pick up the fix.

Sign in to comment on this issue.