From 8b480a0b9ab9f3c41523d92b2552eb59cf60f6ea Mon Sep 17 00:00:00 2001 From: Chris Parsons <438676+chrisparsons83@users.noreply.github.com> Date: Fri, 20 Feb 2026 16:10:22 -0800 Subject: [PATCH] Fix ~/database/context resolving to browser stub during dev SSR (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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 --- vite.config.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 5855c96..9bda7cd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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, },