Skip to content

Start: SSR context loss due to dependency externalization #4409

Closed

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

Activity

  1. solace commented on Jun 13, 2025

    @solace

    @wobsoriano Out of interest, even with the noExternal, the SignedIn and SignedOut work, but the UserButton, and SignInButton don't render, and SignOutButton renders but does nothing. Is this expected at the moment while this issue is being sorted out?

  2. solace commented on Jun 15, 2025

    @solace

    @wobsoriano Out of interest, even with the noExternal, the SignedIn and SignedOut work, but the UserButton, and SignInButton don't render, and SignOutButton renders but does nothing. Is this expected at the moment while this issue is being sorted out?

    Started from scratch and added everything back in one at a time. Looks like it was lingui integration based on their tanstack starter that was causing Clerk to fail.

  3. depsimon commented on Jun 18, 2025

    @depsimon

    @solace I just updated the lingui example here, let me know if you have any issues with it

  4. vcheeze commented on Jun 19, 2025

    @vcheeze

    @depsimon I just tried out Tanstack Start for the first time today using the start-clerk-basic example, and I encountered the same error @wobsoriano mentioned without modifying anything in the example. I believe you'll see the same error by just going to the example's StackBlitz link.

    What is the recommended fix for this? Is it to apply the ssr. noExternal config, or is a more drastic change required or better?

  5. schiller-manuel commented on Jun 20, 2025

    @schiller-manuel
    Collaborator

    @vcheeze this is just caused by stackblitz not support async local storage.

  6. thereis commented on Jun 20, 2025

    @thereis

    I got this error as well in localhost while trying to use with orpc, this happens when I import getWebRequest.

    import { getWebRequest } from '@tanstack/react-start/server';
  7. schiller-manuel commented on Jun 20, 2025

    @schiller-manuel
    Collaborator

    @thereis can you please share a complete reproducer?

  8. vcheeze commented on Jun 21, 2025

    @vcheeze

    @schiller-manuel same here - I just ran the command to create a new project using the template, and got the same error on localhost.

  9. mags-sully commented on Jun 22, 2025

    @mags-sully

    The ssr noexternal workaround for clerk fixes my local dev issue as well

  10. tomleadbetter commented on Jun 24, 2025

    @tomleadbetter

    We have a project which was based on this starter: https://clerk.com/docs/quickstarts/tanstack-react-start
    We have issues with FOUC and upgrading to newer versions of tanstack.
    The tanstack Clerk starter is quite different (vite, no vinxi).

    I understand this is beta but is there a timeline for aligning the starter kits? Or getting the Clerk one using the latest Tanstack version?

  11. vcheeze commented on Jun 24, 2025

    @vcheeze

    We have a project which was based on this starter: https://clerk.com/docs/quickstarts/tanstack-react-start
    We have issues with FOUC and upgrading to newer versions of tanstack.
    The tanstack Clerk starter is quite different (vite, no vinxi).

    I understand this is beta but is there a timeline for aligning the starter kits? Or getting the Clerk one using the latest Tanstack version?

    @tomleadbetter I believe the plan was always to move from vinxi to vite, so I believe the Clerk starter is newer and more conformed to the potential official version.

  12. tomleadbetter commented on Jun 24, 2025

    @tomleadbetter

    Thanks @vcheeze
    I'll start with a clean project and see what happens.

  13. tomleadbetter commented on Jun 24, 2025

    @tomleadbetter
  14. depsimon commented on Jun 24, 2025

    @depsimon

    It's still in a branch I believe.

  15. tomleadbetter commented on Jun 24, 2025

    @tomleadbetter

    It's still in a branch I believe.

    Thanks @depsimon
    I'll keep an eye on the situation then rather than dive into it. Looks like a few moving parts.

  16. wobsoriano commented on Jun 24, 2025

    @wobsoriano
    ContributorAuthor

    It's still in a branch I believe.

    Thanks @depsimon I'll keep an eye on the situation then rather than dive into it. Looks like a few moving parts.

    That branch is now merged 👍🏼

  17. vcheeze commented on Jun 24, 2025

    @vcheeze

    @depsimon I just tried out Tanstack Start for the first time today using the start-clerk-basic example, and I encountered the same error @wobsoriano mentioned without modifying anything in the example. I believe you'll see the same error by just going to the example's StackBlitz link.

    What is the recommended fix for this? Is it to apply the ssr. noExternal config, or is a more drastic change required or better?

    Thanks @wobsoriano. Any ideas about this issue in the Clerk example?

  18. wobsoriano commented on Jun 24, 2025

    @wobsoriano
    ContributorAuthor

    @depsimon I just tried out Tanstack Start for the first time today using the start-clerk-basic example, and I encountered the same error @wobsoriano mentioned without modifying anything in the example. I believe you'll see the same error by just going to the example's StackBlitz link.
    What is the recommended fix for this? Is it to apply the ssr. noExternal config, or is a more drastic change required or better?

    Thanks @wobsoriano. Any ideas about this issue in the Clerk example?

    I'm updating the starter here #4392

  19. kusiewicz commented on Jun 26, 2025

    @kusiewicz
    Contributor

    FYI guys the same problem occurs when I'm using @tanstack/react-forms.

    I get the same error, and stack trace points to tanstack-forms.

  20. Suver1 commented on Jul 6, 2025

    @Suver1

    I got the same error when following the @tanstack/react-forms SSR guide with TanStack Start, along with a few TS errors (see README) in this minimal example: https://stackblitz.com/~/github.com/Suver1/tanstack-start-with-form-bug

  21. Earnstein commented on Jul 21, 2025

    @Earnstein

    I got the same error when following the @tanstack/react-forms SSR guide with TanStack Start, along with a few TS errors (see README) in this minimal example: https://stackblitz.com/~/github.com/Suver1/tanstack-start-with-form-bug

    Encountered the same issue with the tanstack form SSR guide, curious to know if you’ve found a fix.

  22. added a commit that references this issue on Jul 21, 2025
    c9d7717
  23. MrBlue9 commented on Jul 27, 2025

    @MrBlue9

    I got the same error when following the @tanstack/react-forms SSR guide with TanStack Start, along with a few TS errors (see README) in this minimal example: https://stackblitz.com/~/github.com/Suver1/tanstack-start-with-form-bug

    Encountered the same issue with the tanstack form SSR guide, curious to know if you’ve found a fix.

    @Earnstein Did you get this fixed somehow? I'm kind of confused, as I get the same error message and this issue was closed as completed last week but I still get the error with everything updated to the latest version.

  24. schiller-manuel commented on Jul 27, 2025

    @schiller-manuel
    Collaborator

    @MrBlue9 can you please create a new GitHub issue including a complete reproducer?

  25. MrBlue9 commented on Jul 28, 2025

    @MrBlue9

    @schiller-manuel I tried setting up a minimal example on Stackblitz with the tanstack libraries I use for my project. Even before getting to set up a basic form to reproduce my issue I get the aforementioned error message about AsyncLocalStorage. I'm not sure if I made a mistake during set up or if there is an underlying issue in one of the libraries (I guess it's the former, otherwise there would probably be many more open issues about this): https://stackblitz.com/edit/vitejs-vite-o7sovvfn

  26. yusoofsh commented on Dec 17, 2025

    @yusoofsh

    For anyone end up here and using Tanstack Start, do:

    export default defineConfig({
      // ...other config
      ssr: {
        noExternal: ['@convex-dev/better-auth'],
      },
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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