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
- Create a page at
resources/views/trail/[id].stxthat readsuseRoute().params.idorwindow.stx._rp - Export
getStaticPathsreturning[{ params: { id: '1' } }, { params: { id: '2' } }] - Run
bun run build - Inspect
dist/trail/2.html— nowindow.stx._rpscript anywhere - 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.
Related (optional, separate PR)
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.10bun-plugin-stx@0.2.10- Bun 1.3.10, macOS arm64