ClerkProvider Should Not Force All Children to Render Dynamically #1764
Replies: 12 comments 21 replies
-
I just ran into this issue as well, I have my Also, about the solution of moving the |
Beta Was this translation helpful? Give feedback.
-
We just ran into this as well. This is a major blocker for us. We've got pages that have to be statically rendered at build time to make those pages usable. We're so far down the road on our clerk integration that I'm not sure what to do. Seems like there should be a way to authenticate a client-only component without forcing the page to be dynamically rendered. Disabling an entire feature branch of optimizations in next.js doesn't seem like a viable long term solution. |
Beta Was this translation helpful? Give feedback.
-
Similar boat here, this severely impacts my app's performance due to server-side rendering being forced. |
Beta Was this translation helpful? Give feedback.
-
I think this limitation poses a significant challenge as it leads to increased operational costs. The inability to cache the home page (or any other mostly static page) content due to the mandatory wrapping of the entire app in the |
Beta Was this translation helpful? Give feedback.
-
On another note, I am wondering if the upcoming Partial Prerendering in Next.js and React actually fixes the issue. With that update, it is possible to slap on a Suspense boundary outside a component that uses dynamic functions ( |
Beta Was this translation helpful? Give feedback.
-
Hi y'all, as mentioned in the OP currently the With that being said, there are strategies that you can currently use to workaround this:
We totally understand the current behavior is not ideal and in many cases unexpected as a user. We're also aware of the upcoming PPR feature, which will also be impacted by this. We'll continue to share updates as our explorations progress. Thanks for sharing your thoughts here. 🙏 |
Beta Was this translation helpful? Give feedback.
-
hello everybody, I've run into the same issue. Here is my solution:
|
Beta Was this translation helpful? Give feedback.
-
Hi all, as of You can opt-out of this by placing We'll update the docs to call this out, thanks for the feedback! |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing this issue using getStaticProps on pages router. When wrapping the app with ClerkProvider the static props are entirely stripped from being passed into the page - seems like the provider breaks basic usage of a pretty integral part of the pages router. Oddly this doesn't happen on dev server and only happens when visiting the page through a , but works fine if the url is visited directly. For this page I cannot use dynamic rendering as we're hitting an external api, which is not viable to be pulled on every request. My fix for this is just to exclude the page from middleware all together. I would assume this breaks the @clerk/next auth but it doesn't. Would like to see this forced dynamic behaviour removed if possible |
Beta Was this translation helpful? Give feedback.
-
Any update guys ? |
Beta Was this translation helpful? Give feedback.
-
This seems to break the Next.js which is SSR by default now meaning the existing tutorial for Next.js is no longer functional, I'm using clerk/nextjs 5.16, and next 14.2.1. |
Beta Was this translation helpful? Give feedback.
-
@BRKalow There seems to be no update to the docs regarding this - I am lucky to have found the info here, or else I was not going to continue using Clerk. I'd add it asap and maybe have a deeper explanation regarding how rendering is impacted by other things. PPR seems like it will help solve the exact issue people are having since we can still use server components that do dynamic things, but not cause the whole segment to be dynamically rendered (server static content immediately, stream in dynamic parts). However, I do still get a bit confused how this differs from existing suspense capabilities. |
Beta Was this translation helpful? Give feedback.
-
Preliminary Checks
I have reviewed the documentation: https://clerk.com/docs
I have searched for existing issues: https://github.com/clerkinc/javascript/issues
This issue is not a question, general help request, or anything other than a bug report directly related to Clerk. Please ask questions in our Discord community: https://clerk.com/discord.
Reproduction / Replay Link
Na
Publishable key
na
Description
Posting this as a bug, even though it is technically a feature, due to its severity.
Currently, the ClerkProvider forces all child routes to render dynamically.
The documentation states that you can move the provider further down the component tree, to address the issue.
Unfortunately, that is not a viable solution in many cases.
Typically, an app has several protected routes, where a common component (like a navbar) is shown on each route, and that component needs to know whether a user is logged in (e.g. to show certain links). The other components in a route might benefit massively from being statically (build time) rendered.
If certain data fetches are slow, not being able to do the fetch/render build-time, might break the application's usability entirely.
In other words, taking away static rendering across all provider children will drastically impact the performance of the application and in certain cases render it useless.
ClerkProvider states that it needs dynamic routes because it uses headers() (and/or cookies()), which are built-in next functions that opt the entire route they appear in into dynamic rendering.
It is my understanding, and I might be wrong, that those functions can only be used in server components. They read a http request's headers or cookies. As such, they do not make sense, client side.
So, given how those functions work, why would we need to dynamically render all children of the ClerkProvider, if none of those children use server side auth functionality.
As an example, if the navbar mentioned above is a client component, and middleware is used to protect routes, I do not see why headers() or cookies() are needed at all.
To test this, I added the Next.js Route Segment Config option to the top of the top-most layout:
export const dynamic='force-static'
. This turned the full app back to static rendering, even though it used the ClerkProvider around the top-level layout, has a navbar client component that checks if user is logged in, and has all routes protected with middleware. It still works, both the auth and the app.So, my questions are:
Could you keep static rendering as the default, just as Next does, and only opt routes that need headers() into dynamic rendering?
What are the consequences of my solution? Will it work as intended, in the example I gave (where no server side auth functions are used)?
Environment
Beta Was this translation helpful? Give feedback.
All reactions