Skip to content

Start: SSR context loss due to dependency externalization #4409

Open
@wobsoriano

Description

@wobsoriano

Which project does this relate to?

Start

Describe the bug

Packages installed in node_modules rely on sharing a server context with the main application, such as using AsyncLocalStorage for request-scoped data, auth, or other per-request state. If Vite externalizes these packages during SSR (its default behavior), they may be loaded in a separate context, causing them to lose access to the shared server state.

This can result in runtime errors like:

Error: No HTTPEvent found in AsyncLocalStorage. Make sure you are using the function within the server runtime.
    at getEvent (node_modules/@tanstack/start-server-core/dist/esm/h3.js:30:11)
    ...

Temporary Workaround:

Add the affected package to the ssr.noExternal option in your vite.config.ts file:

export default defineConfig({
  // ...other config
  ssr: {
    noExternal: ['@clerk/tanstack-react-start'],
  },
});

Your Example Website or App

https://codesandbox.io/p/devbox/mdxyzw

Steps to Reproduce the Bug or Issue

  1. Create a fresh devinxi based TanStack Start app
  2. Install Clerk SDK with devinxi support (npm i @clerk/tanstack-react-start@0.17.0-snapshot.v20250611045520 --save-exact)
  3. Run npm run dev
  4. Check console

or visit the codesandbox link

Expected behavior

Packages that depend on shared server context should function correctly during SSR, with no loss of context or unexpected errors, even if they are installed in node_modules.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions