ReviewOS

stacks/stx

SSG: dynamic route params not injected client-side, `useRoute().params` is empty on `[param]` pages in production builds

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

Summary

When building a site with bun run build (SSG), pages generated from dynamic routes (e.g. pages/trail/[id].stx → dist/trail/2.html) have the correct server-side params available during template processing, but the client-side runtime receives no route params. useRoute().params returns {} and window.stx._rp / window.__stx_rp are never set on the page.

The dev server (bun-plugin-stx/serve.ts) correctly injects params into the output:

// packages/bun-plugin/src/serve.ts:668
const paramsScript = `<script>if(window.stx)window.stx._rp=${JSON.stringify(paramsObj)};else window.__stx_rp=${JSON.stringify(paramsObj)};</script>`
if (output.includes('</head>')) {
  output = output.replace('</head>', `${paramsScript}\n</head>`)
} else {
  output = `${paramsScript}\n${output}`
}

packages/stx/src/ssg.ts:renderPage() has no equivalent injection step.

Impact

Any client-side code that relies on useRoute().params, useRouteParam('id'), or window.stx._rp breaks in SSG output. The page template processes correctly server-side (because ssg.ts passes params into the context) but any client reactive logic depending on the route param silently falls back to defaults.

Concretely, a page like this works in dev but renders the same trail for every id in production:

<script client>
  const route = useRoute()
  const trailId = derived(() => Number(route.params?.id ?? 1))
  const trail = derived(() => tb.trails().find(t => t.id === trailId()))
</script>
<h1 x-text="trail.name"></h1>

In dev: /trail/2 → "Riverside Loop Trail" ✓ In SSG: /trail/2.html → "Eagle Peak Summit" (id=1 fallback) ✗

Reproduction

  1. Create a page at resources/views/trail/[id].stx that reads useRoute().params.id or window.stx._rp
  2. Export getStaticPaths returning [{ params: { id: '1' } }, { params: { id: '2' } }]
  3. Run bun run build
  4. Inspect dist/trail/2.html — no window.stx._rp script anywhere
  5. Open the file in a browser — client script sees window.stx._rp === undefined

Proposed fix

Mirror the dev-server implementation in ssg.ts:renderPage after processDirectives and before injectRouterScript:

// packages/stx/src/ssg.ts (inside renderPage, after processDirectives)
if (Object.keys(params).length > 0) {
  const paramsScript = `<script>if(window.stx)window.stx._rp=${JSON.stringify(params)};else window.__stx_rp=${JSON.stringify(params)};</script>`
  if (html.includes('</head>')) {
    html = html.replace('</head>', `${paramsScript}\n</head>`)
  } else {
    html = `${paramsScript}\n${html}`
  }
}

html = await injectRouterScript(html)

The params arg is already plumbed into renderPage (line 573), so no new data flow is required.

While reviewing the dev/SSG code paths, I noticed packages/bun-plugin/src/serve.ts:processTemplateDynamic was also missing autoShell: true, ssr, and app keys in its config object versus the static processTemplate path — this caused @include partials (stores, nav) to not emit their client scripts on dynamic routes in dev. Fixed locally via the bun-link setup; happy to open a separate PR for that too if useful.

Environment

  • @stacksjs/stx@0.2.10
  • bun-plugin-stx@0.2.10
  • Bun 1.3.10, macOS arm64
23 days ago

Fixed on main. packages/stx/src/ssg.ts:629 now injects the params script the same way the dev server does:

const paramsScript = `<script>if(window.stx)window.stx._rp=${JSON.stringify(params)};else window.__stx_rp=${JSON.stringify(params)};</script>`
if (html.includes('</head>')) {
  html = html.replace('</head>', `${paramsScript}\n</head>`)
} else {
  html = `${paramsScript}\n${html}`
}

So a build of pages/trail/[id].stx now produces dist/trail/2.html with window.stx._rp = {"id":"2"} injected, and useRoute().params.id resolves correctly client-side in production.

Sign in to comment on this issue.