fix: exclude database/context from browser bundle to fix Docker build (#2)

database/context.ts uses AsyncLocalStorage from node:async_hooks which
is a Node.js-only API. Vite externalizes it for browser builds but the
resulting error "AsyncLocalStorage is not exported by __vite-browser-external"
caused npm run build to fail.

Fix: add a Vite resolve.alias for the browser (non-SSR) build that maps
~/database/context to a browser-safe stub. The stub is never called at
runtime since database() is only invoked in server-side loaders, but it
allows the client bundle to build without errors.

https://claude.ai/code/session_01Fjf9WFqNnuHmmC5yTedL7G

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-02-19 11:54:32 -08:00 committed by GitHub
parent 3210ab265f
commit 740ffbb411
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View file

@ -0,0 +1,9 @@
// Browser-safe stub for database/context
// The real implementation uses Node.js AsyncLocalStorage which is server-only.
// This stub is used during the client build to avoid bundling server-only code.
export const DatabaseContext = null as any;
export function database(): never {
throw new Error("database() can only be called on the server");
}

View file

@ -1,8 +1,12 @@
import { reactRouter } from "@react-router/dev/vite";
import tailwindcss from "@tailwindcss/vite";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default defineConfig(({ isSsrBuild }) => ({
build: {
rollupOptions: isSsrBuild
@ -11,6 +15,19 @@ export default defineConfig(({ isSsrBuild }) => ({
}
: undefined,
},
resolve: {
alias: isSsrBuild
? []
: [
{
find: "~/database/context",
replacement: path.resolve(
__dirname,
"database/context.browser-stub.ts"
),
},
],
},
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
server: {
allowedHosts: true,