Skip to content

Server only packages leaking into client bundle via tanstack query #3401

Closed

Description

@ryan-callahan-edo

Which project does this relate to?

Start

Describe the bug

I am trying to get react query to fetch session info with better auth on the server. Drizzle is getting bundled and shipped to the browser even though it is behind a serverfn. Codepath for this in the linked repo is

  1. app/routes/__root.tsx beforeLoad
  2. lib/auth/query.ts authQueryOptions
  3. lib/auth/api.ts getAuth
  4. lib/auth/auth.ts auth
  5. lib/drizzle/db.ts db

Note: The stackblitz version of the repo is giving a totally different error for some reason.

Your Example Website or App

https://stackblitz.com/~/github.com/ryan-callahan-edo/tanstack-start-client-bundle-error

Steps to Reproduce the Bug or Issue

  1. clone https://github.com/ryan-callahan-edo/tanstack-start-client-bundle-error
  2. run bun i && bun run dev
  3. navigate to page
  4. see error in console
  5. observe error stopping all js from being executed
  6. observe drizzle bundles being shipped to the browser

Expected behavior

  1. clone https://github.com/ryan-callahan-edo/tanstack-start-client-bundle-error
  2. run bun i && bun run dev
  3. navigate to page
  4. see no error in console
  5. Page functions normally

Screenshots or Videos

Screen.Recording.2025-02-11.at.9.05.25.PM.mov

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 131

Additional context

No response

Activity

  1. ryan-callahan-edo commented on Feb 12, 2025

    @ryan-callahan-edo
    ContributorAuthor

    This seems to be a recurring issue for me every time I upgrade versions of tanstack start. Any guidance for debugging or getting around this?

  2. akawahuynh commented on Mar 5, 2025

    @akawahuynh

    No this is your fault, you just skipped the tsconfig.json configuration step:

    https://tanstack.com/start/latest/docs/framework/react/build-from-scratch#typescript-configuration

    which makes the flag verbatimModuleSyntax: true

    please remove this

  3. ryan-callahan-edo commented on Mar 5, 2025

    @ryan-callahan-edo
    ContributorAuthor

    Great catch, thank you! I ended up getting around it in my private repos by manually typing things with primitives rather than inferring types. Now I can go back to inference.

  4. ryanagillie commented on Mar 12, 2025

    @ryanagillie
    Contributor

    After doing some investigation, the reason why the setting needs to be disabled is because during the client / server separation step something like
    import { something, type Thing } from "./only-server-code"
    is being transformed into
    import { type Thing } from "./only-server-code"
    which is then being transformed by tsc / esbuild / rollup (they all follow the verbatimModuleSyntax flag) into
    import { } from "./only-server-code"
    which is causing the leak

    Disabling verbatimModuleSyntax removes that empty import, but the downside is that if that import has any side-effects, none of them are ran.

  5. RyanCallahan312 commented on Mar 12, 2025

    @RyanCallahan312
    Contributor

    the downside is that if that import has any side-effects, none of them are ran.

    I purposefully avoid that pattern in my code, but I can see this being a really tricky "gotcha" to debug for someone in the future. Is it used commonly enough where it's worth calling out or linking to this issue in the docs? https://tanstack.com/start/latest/docs/framework/react/build-from-scratch#typescript-configuration

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