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:
parent
3323522782
commit
8b480a0b9a
1 changed files with 16 additions and 14 deletions
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue