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, : undefined,
}, },
resolve: { plugins: [
alias: isSsrBuild {
? [] name: "database-context-alias",
: [ enforce: "pre" as const,
{ resolveId(id, _importer, options) {
find: "~/database/context", if (id === "~/database/context") {
replacement: path.resolve( return options?.ssr
__dirname, ? path.resolve(__dirname, "database/context.ts")
"database/context.browser-stub.ts" : path.resolve(__dirname, "database/context.browser-stub.ts");
), }
}, },
], },
}, tailwindcss(),
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()], reactRouter(),
tsconfigPaths(),
],
server: { server: {
allowedHosts: true, allowedHosts: true,
}, },