Fix ~/database/context resolving to browser stub during dev SSR

`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:05:36 -08:00
parent 3323522782
commit 61c6c3498b

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,
},