Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: 7.22.0 next build error TRPCClientError: Dynamic server usage #1599

Closed
player0x3 opened this issue Oct 16, 2023 · 30 comments · Fixed by #1670
Closed

bug: 7.22.0 next build error TRPCClientError: Dynamic server usage #1599

player0x3 opened this issue Oct 16, 2023 · 30 comments · Fixed by #1670
Labels
🐞 confirmed bug Something isn't working

Comments

@player0x3
Copy link

player0x3 commented Oct 16, 2023

Provide environment information

"ct3aMetadata": {
"initVersion": "7.22.0"
},

Describe the bug

image

Reproduction repo

.

To reproduce

when:

  • install: pnpm create t3-app@latest
  • use nextjs appdir option
  • build: pnpm build

Additional information

No response

@player0x3 player0x3 changed the title bug: 7.22.0 next build error:TRPCClientError: Dynamic server usage bug: 7.22.0 next build error TRPCClientError: Dynamic server usage Oct 16, 2023
@beaumccartney
Copy link

same deal here
image

@beaumccartney
Copy link

#1567 (comment)

this fix got rid of the issue for me - should it ship with by default?

@player0x3
Copy link
Author

#1567 (comment)

this fix got rid of the issue for me - should it ship with by default?

The version released for this Merged is 7.22.0, which is what I'm using, and when my nextjs selects appdir and package management uses pnpm, the problem must exist, I've tried it over and over again a few times.

@dBianchii
Copy link

Same happening here!

@c-ehrlich c-ehrlich added 🐞 confirmed bug Something isn't working and removed 🐞❔ unconfirmed bug labels Oct 18, 2023
@c-ehrlich
Copy link
Member

Thanks for the issue. Can reproduce. Weirdly enough this issue exists for the hello query but not for the query that gets all posts. force-dynamic works as a band-aid, but feels less than optimal. Will research further.

@dBianchii
Copy link

I was trying to investigate this and I'm reading this page but I'm not understanding anything about it. If someone understands this in some way and would like to explain in more layman's terms, I would appreciate it.
I believe the error is coming from here:

heads.set("x-trpc-source", "rsc");

@markomitranic
Copy link

markomitranic commented Oct 19, 2023

Update: was spreading misinformation, still no real solution. Edited with comments.

@c-ehrlich While I have no idea why it happens, like you said, here is some info from my limited testing, that I hope proves useful. :)

I can see that using TRPCReactProvider on a layout level produces a DYNAMIC_SERVER_USAGE error as it is unable to produce the entire page statically due to a use of headers.

When I instead move the provider elsewhere, and only have it wrap the actual components that wish to access ~/trpc/react, the error disappears as it is now limiting the dynamicness only to a certain component. It ain't pretty, as it requires us to wrap components. In this scenario I didn't have to resort to forcing dynamic, it seems to be able to decide on its own.

What I do for now is move the TRPCReactProvider into page.tsx, and have it only in pages that actually have components that use it. Feels like an ok solution for now. Not an ok solution, as it forces the entire page into a λ state instead of a SSG.

Now, not saying this is a solution but leaving this here hoping it is helpful to someone, until we find a better one...

All the while, ~/trpc/server works just fine without a need of a provider, and produces no errors. :) No idea what happened there, it most definitely produces errors during build, due to using headers in the server render phase. The file server.ts uses next/headers const heads = new Map(headers()); which causes the error we see. Replacing it with new Headers() helps pass the error, but fails to actually fetch data due to ECONNREFUSED while fetching

--

P.S. Not sure how wrapping each element in TRPCReactProvider affects memory and that stuff, so there is also a secondary option instead of having to throw a kazzilion TRPCReactProvider around your components. We did this pattern very often when it comes to shared state, contexts and providers - conditionally wrap the layout ONLY when is client, otherwise print children as-is.

// layout.tsx
    <html lang="en">
      <body>
        <main>
          <WrapOnlyClient
            wrapper={
              <TRPCReactProvider headers={headers()}>
                {children}
              </TRPCReactProvider>
            }
          >
            {children}
          </WrapOnlyClient>
        </main>
      </body>
    </html>

In this scenario, the page will SSR as-is, and will have no provider during SSR, but will actually have a provider in the client, allowing the client components to do their work. The major issue with this is that you then also must ensure that you don't mount (only) the offending client components during SSR, effectively making them client-only or mounted-only.

// some/page.tsx
<OnlyClient><ContainsTrpcCalls /></OnlyClient>

@itarutomy97
Copy link

itarutomy97 commented Oct 23, 2023

The following wrapper (WrapOnlyClient) does not work in my repo...
It seems that writing "force-dynamic" in layout.tsx deletes the error but in docker composing the error occurs.

Finally, I commented out the "hello" on app/page.tsx to delete the TRPCClientError...

// layout.tsx



<WrapOnlyClient
wrapper={

{children}

}
>
{children}



@jonbergan
Copy link

jonbergan commented Oct 28, 2023

Experienced this issue today also when trying to build a new app with the latest T3 + App Router. 'force-dynamic' seemed to resolve the issue for me but certainly doesn't feel ideal. Keen to see the proper solution for this come through.

@TCGAUD
Copy link

TCGAUD commented Oct 28, 2023

Same issue here

@juliusmarminge
Copy link
Member

juliusmarminge commented Oct 29, 2023

Idk what @KATT thinks but i'd say this issue should be tracked in trpc as it's something we should look into from there on how to properly recommend using trpc/react-query and forwarding headers correctly?

@ImBIOS
Copy link

ImBIOS commented Oct 30, 2023

I only found these in the example bundled with t3, I never found it in any of my other projects.

I experimented by splitting all await into its independent components and then wrapping it with Suspense, the error went into the abyss in no time.

I suspect, it's Next.js trying to make all RSC static by default and only parts of its page are interactable which is dynamic. Whenever Next.js detects headers() or cookies() passed to the component, it normally catches it and makes the page dynamic, this error means, the error isn't watched properly for some reason.

@simonri
Copy link

simonri commented Oct 30, 2023

I only found these in the example bundled with t3, I never found it in any of my other projects.

I experimented by splitting all await into its independent components and then wrapping it with Suspense, the error went into the abyss in no time.

I suspect, it's Next.js trying to make all RSC static by default and only parts of its page are interactable which is dynamic. Whenever Next.js detects headers() or cookies() passed to the component, it normally catches it and makes the page dynamic, this error means, the error isn't watched properly for some reason.

Wrapping the components in Suspense worked as a temporary solution for me as well.

@benfavre
Copy link

Switching from node 18 to 20 fixes this for me

@jonbergan
Copy link

That seems random since it doesn’t appear to be a node issue rather Next throwing a valid error..?

@simonri
Copy link

simonri commented Oct 31, 2023

Switching from node 18 to 20 fixes this for me

Doesn't work for me

@LudoLogical
Copy link

+1 on this bug report, I'm running node v20.9.0
The issue is also present when deploying to Vercel

@liuhe2020
Copy link

liuhe2020 commented Nov 6, 2023

Same issue for me, and the funny thing is it doesn't happen to all pages. Some pages are completely fine and the setup is exactly the same, just calling for different data from the db. The website still deploys on Vercel and works as expect. This is the only time I see red in deploy log and it still manages to complete the build.

@henokyehulu
Copy link

Encounterd this issue today. Any solutions devs?

@danieljpgo
Copy link

danieljpgo commented Nov 8, 2023

This also happens on my side, it is impossible to generate static pages, it is limiting everything to SSR.

@Andrii-Antoniuk
Copy link

Same bug today for me
"initVersion": "7.23.2"

@luthfimasruri
Copy link

Today, I encountered this issue. Any updates on the fix?

Screenshot 2023-11-19 at 14 40 26

@Lucasvo1
Copy link

Having the same issue.

@keelangibb
Copy link

So, this is not causing a build issue anymore, but it is forcing all routes to become force-dynamic because of the use of headers and cookies I think. Even without using trpc on a barebones page.tsx. Anyone know of a workaround? "initVersion": "7.24.0"

@LudoLogical
Copy link

@keelangibb might be worth opening a new issue over and referencing this one in that one? Technically, this issue is resolved and should be closed, but obviously the behavior you're experiencing is problematic.

@rydonahue1
Copy link

e? Technically, this issue is resolved and should be closed, but obviously the behavior you're experiencing is problematic.

Just did a pull of the repo today and was still getting the bug mentioned by OP, in just the base T3 app. I have been reading a ton of these threads and managed to find a few work arounds, but they all seem like a rather quick fix Band-Aid than an actual solution.

Is there any recommendation by the T3 team on how to bypass this error or is it safe to ignore because the app still technically builds?

@Lukem121
Copy link
Contributor

Also getting Dynamic server usage: Page couldn't be rendered statically because it used headers.

@bored-yuns
Copy link

So how exactly do you fix this?

@andrewmartin
Copy link

Curious if this is something we can fix, do we need headers() in the TRPC context?

@gorkamolero
Copy link

This seems to be a solution:

https://github.com/orgs/vercel/discussions/4696#discussioncomment-7494345

import { api } from '@/trpc/server';
import { unstable_noStore as noStore } from 'next/cache';

async function getIdeas() {
  noStore();
  const ideas = await api.ideas.getAll.query();
  return ideas;
}

banushi-a added a commit to sandboxnu/cooper that referenced this issue Mar 31, 2024
* feat: begin creating Role Review Cards

* feat: assembled Role Page

Made role review cards update based on db, put together components for roles page, made search bar
slightly more responsive, updated header logo sizing, edited logo font per design

* feat: create header layout component to standardize header placement

* fix: removed async from exports (not needed)

* fix: made search filter (whole bar) more responsive"

* fix: edit roles/page to prevent tRPC error.t3-oss/create-t3-app#1599

* docs: add TODO and FIXME tags

* refactor: implemented changes suggested in PR. Started to work on style changes for header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 confirmed bug Something isn't working
Projects
None yet