Skip to content

Conversation

hugotiger
Copy link
Contributor

@hugotiger hugotiger commented Sep 5, 2025

Fixes #2011

This PR introduces a workaround for vercel/next.js#73085.

In Next.js, using usePathname in combination with rewrites and middleware can trigger hydration errors. These only occur in production builds, and while they are documented, the information is somewhat hidden:

CleanShot 2025-09-08 at 19 48 12@2x

This issue was partially addressed in #1571. However, I noticed that when localePrefix.mode is set to 'always', the workaround was not applied. Instead of adding an extra condition for 'always', I flipped the check to localePrefix.mode !== 'never'. As far as I can tell this should work fine, but let me know if you see any risks/issues with this.

Copy link

vercel bot commented Sep 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
next-intl-docs Ready Ready Preview Comment Sep 9, 2025 7:26am
next-intl-example-app-router Ready Ready Preview Comment Sep 9, 2025 7:26am
next-intl-example-app-router-without-i18n-routing Ready Ready Preview Comment Sep 9, 2025 7:26am

Copy link

vercel bot commented Sep 5, 2025

@hugotiger is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

@hugotiger hugotiger changed the title [WIP]: Fix usePathname hydratione error with custom locale prefix [WIP]: Fix usePathname hydration error with custom locale prefix Sep 5, 2025
@hugotiger hugotiger force-pushed the fix/use-pathname-locale-prefix-hydration-error branch from 56d5493 to 5e2a248 Compare September 8, 2025 17:35
@hugotiger hugotiger changed the title [WIP]: Fix usePathname hydration error with custom locale prefix Fix usePathname hydration error with custom locale prefix Sep 8, 2025
@hugotiger hugotiger changed the title Fix usePathname hydration error with custom locale prefix Fix usePathname hydration error with custom locale prefixes Sep 8, 2025
@hugotiger hugotiger changed the title Fix usePathname hydration error with custom locale prefixes fix: Fix usePathname hydration error with custom locale prefixes Sep 8, 2025
@hugotiger hugotiger changed the title fix: Fix usePathname hydration error with custom locale prefixes fix: Fix usePathname hydration error with custom prefixes, localePrefix: 'always' and static rendering Sep 8, 2025
@hugotiger hugotiger changed the title fix: Fix usePathname hydration error with custom prefixes, localePrefix: 'always' and static rendering fix: Fix usePathname hydration error with custom prefixes, localePrefix: 'always' and static rendering Sep 8, 2025
@hugotiger hugotiger marked this pull request as ready for review September 8, 2025 18:04
@hugotiger
Copy link
Contributor Author

@amannn I believe this PR should resolve the issue! Would love your review when you have a chance 😄

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect fix, thanks a bunch for this @hugotiger! 🙌 I hope you found your way around in the code!

There's just a minor simplification I think we could apply here, let me know if this looks ok to you!

Comment on lines 48 to 51
} else if (
config.localePrefix.mode === 'as-needed' &&
config.localePrefix.mode !== 'never' &&
config.localePrefix.prefixes
) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a chance for a minor simplification here now:

Suggested change
} else if (
config.localePrefix.mode === 'as-needed' &&
config.localePrefix.mode !== 'never' &&
config.localePrefix.prefixes
) {
} else if ('prefixes' in config.localePrefix) {

Just to minify a tiny bit better :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes that sounds reasonable! But I suppose we still want to check if prefixes is truthy? With the in operator it'll evaluate to true even when someone passes undefined as the value.

So this for example would still run the branch of logic:

localePrefix: {
  mode: 'always',
  prefixes: undefined,
}

So should we still check config.locale.prefixes or do you think just checking for the key is fine?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I think you're right there. It could happen for example if someone conditionally assigns prefixes based on an environment parameter.

Checking config.locale.prefixes is something TypeScript isn't too happy about though, since we have type-safety for disallowing prefixes when mode: 'never' is used.

I'd say we leave it like you proposed it originally! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's what I was thinking!

Oh yes that's true! I'll leave it as is then :)

@amannn amannn merged commit bc9cb62 into amannn:main Sep 9, 2025
6 checks passed
@amannn amannn changed the title fix: Fix usePathname hydration error with custom prefixes, localePrefix: 'always' and static rendering fix: Avoid usePathname inconsistency in Next.js leading to a hydration error with custom prefixes, localePrefix: 'always' and static rendering Sep 9, 2025
@hugotiger hugotiger deleted the fix/use-pathname-locale-prefix-hydration-error branch September 9, 2025 08:41
@amannn
Copy link
Owner

amannn commented Sep 10, 2025

Hey @hugotiger, many thanks again for your PR here!

I wasn't quite sure where to reach out easiest, so I'll just post it here 😄

I’m planning to add a few company logos to the next-intl website to highlight adoption, and I was wondering if it would be okay to include Qasa's logo. Of course, I can remove it at any time if you’d prefer not to be listed. Totally fine either way—just let me know!

It would mean a lot and really helps to show the impact next-intl is having in the developer community.

Many thanks in any case!

(I'm also available at [email protected] btw.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

usePathname causing hydration errors with locale prefixes and static rendering
2 participants