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