Fix ~/database/context resolving to browser stub during dev SSR (#12)

`resolve.alias` with `isSsrBuild` only works at build time — during
`npm run dev`, `isSsrBuild` is always false, so `ssrLoadModule` was
loading the browser stub (DatabaseContext = null) instead of the real
AsyncLocalStorage implementation, causing a TypeError on every request.

Replace the alias with an `enforce: 'pre'` plugin that checks
`options.ssr` at resolve time, which Vite correctly sets for both dev
SSR module loading and production SSR builds.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-02-20 16:10:22 -08:00 committed by GitHub
parent 3323522782
commit 8b480a0b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,20 +15,22 @@ export default defineConfig(({ isSsrBuild }) => ({
}
: undefined,
},
resolve: {
alias: isSsrBuild
? []
: [
{
find: "~/database/context",
replacement: path.resolve(
__dirname,
"database/context.browser-stub.ts"
),
},
],
},
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
plugins: [
{
name: "database-context-alias",
enforce: "pre" as const,
resolveId(id, _importer, options) {
if (id === "~/database/context") {
return options?.ssr
? path.resolve(__dirname, "database/context.ts")
: path.resolve(__dirname, "database/context.browser-stub.ts");
}
},
},
tailwindcss(),
reactRouter(),
tsconfigPaths(),
],
server: {
allowedHosts: true,
},