-
Notifications
You must be signed in to change notification settings - Fork 340
NextJS dynamicIO - ClerkProvider causing build failure when dynamic routes are used #4921
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
Comments
Hey @andrewkucz thanks for reaching out. Supporting experimental More context: |
thanks @panteliselef - this does resolve the build problem! I thought adding a top level loading.tsx was the same as wrapping in Suspense but I guess I misunderstood some of Next's intricacies haha Appreciate it though this will definitely unblock me for now. |
I'm a newcomer to Next.js and Clerk, so I have a few questions on this particular topic. Perhaps I've misunderstood how dynamic I/O and the overall Next.js architecture work. Since we typically wrap the entire application in the root layout using the ClerkProvider, wouldn't placing everything within a Suspense component make the entire application dynamic? I'm also curious about the future impact, especially when Next.js releases the stable version of its new caching system. |
Hello @SennaSanzo, dynamicIO is quite fresh and we are experimenting with it internally. Having said that
With the launch of this feature, we had to put some async logic when your application is running on development mode. This is why recommend you do the following. // This will not delay TTB
<Suspense>
<ClerkProvider>{}</ClerkProvider>
</Suspense> In practise, if you have already set your keys inside On the other hand if you explicitly use the // This will delay TTB, as it makes the code dynamic
<Suspense>
<ClerkProvider dynamic>{}</ClerkProvider>
</Suspense> |
Thank you so much for your response and clarification. The strange thing is that even though I'm already using the Clerk API keys and have registered them in my .env file, Next.js still complains that the ClerkProvider is trying to access asynchronous resources. Additionally, the project I'm working on includes next-intl for localization and uses dynamic routing (e.g. /[locale]/rest-of-the-route). Therefore, I'm not sure which part of the code is triggering the Next.js error. However, since this is an experimental feature, I'll have to stick with the approach for now and wait to see what happens in the near future. For context here is what i'm doing in my root layout : export default async function RootLayout({
children,
}:
Readonly<{
children: React.ReactNode;
}>) {
const locale = await getLocale();
return (
//Needs to be there if not I got Next.js complaining about the Suspense or use cache stuff
<Suspense>
<ClerkProvider signInForceRedirectUrl={`/${locale}/member`} signUpForceRedirectUrl={`/${locale}/member`}>
<html lang={locale}>
<body className="antialiased relative landing-bg text-sm">
<main>
<NextIntlClientProvider>{children}</NextIntlClientProvider>
</main>
</body>
</html>
</ClerkProvider>
</Suspense>
);
} And here is the the getLocale() function using the unstable_rootParams feature normally allowing to get the root params when dynamicIO is enabled. import { Locale, routing } from "@/i18n/routing";
import { unstable_rootParams as rootParams } from "next/server";
export async function getLocale() {
const { locale } = await rootParams();
console.log("`locale` from `rootParams`: ", locale);
if (!isLocale(locale)) {
console.log(`Using default locale '${routing.defaultLocale}' as fallback`);
return routing.defaultLocale;
}
return locale;
}
function isLocale(locale: unknown): locale is Locale {
return typeof locale === "string" && routing.locales.includes(locale as Locale);
} If you have any clue about why I'm still getting the Next.js error about use cache or suspense tag it would be very helpful. Thanks ! |
Was wondering if any one looked into this issue. Using clerk/nextjs v6.12.11-canary.v20250326115904 and nextjs v15.3.0-canary.23 the only way to avoid the Additionally, even if my ClerkProvider does not wrap the children in my root layout, it is having the effect of making all pages dyanmic rather than static.
Thanks in advance for your help! |
@bepstein9893 can you create a minimal reproduction and share the repo url with us ? |
Thanks @panteliselef Please see this example repo here: clerk-nextjs-err-example At a high level, it appears that the presence of ClerkProvider in a Next15 app using dynamicIO results in dynamic pages, even without wrapping the children in the ClerkProvider or providing ClerkProvider with the It's very possible I am doing something wrong so I appreciate you looking into this. Using the code below, we see that the home page renders dynamically, though it is not wrapped in the clerk provider. If we simply comment out the clerk provider, we see that the home page renders statically. |
@bepstein9893 I understand the confusion here. If you run Once you build you can clearly see that the |
Thanks so much @panteliselef! And is it okay not to wrap your app in the ClerkProvider? I prefer not to wrap the entire app in a Suspense if unnecessary. |
@bepstein9893 You should be able to remove suspense if it does not give you trouble for development. For production is definitely a noop if |
Preliminary Checks
I have reviewed the documentation: https://clerk.com/docs
I have searched for existing issues: https://github.com/clerk/javascript/issues
I have not already reached out to Clerk support via email or Discord (if you have, no need to open an issue here)
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
https://github.com/andrewkucz/clerk-dynamic-io-issue
Publishable key
pk_test_c3VwcmVtZS1kb2xwaGluLTI2LmNsZXJrLmFjY291bnRzLmRldiQ
Description
Steps to reproduce:
Expected behavior:
Successful build
Actual behavior:
Hello,
I know dynamicIO is still in canary so I may be early and this may be low prio but couldn't find any reports of this and was frustrated so made a minimal repro to figure out the source lol. I am getting the above error when using dynamic routes with a ClerkProvider despite adding
loading.tsx
's everywhere and many different combinations of"use cache;"
, cacheLifes, and Suspense.I was able to narrow down the issue to the combination of the use of
ClerkProvider
with the use of a dynamic route (in this casesrc/app/test-route/[dynamic]/page.tsx
). Having both seems to cause the build to fail in the way above, even ifauth()
is not called anywhere and the middleware is not in place (the issue still occurs when both are present)If you delete the
ClerkProvider
from the main layout.tsx file, the build will work, OR if you delete thesrc/app/test-route/[dynamic]
folder so that there is no dynamic routes then the build will also pass. I also tried thedynamic
prop to no avail.Thanks!
Environment
The text was updated successfully, but these errors were encountered: