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

fix: Handle external URLs passed to redirect and permanentRedirect #1084

Merged
merged 5 commits into from
May 23, 2024

Conversation

mhogeveen
Copy link
Contributor

This PR addresses an issue where absolute URL's used as pathnames in the redirect and permanentRedirect functions returned from createSharedPathnamesNavigation are localized.

Current behaviour:

const { redirect, permanentRedirect } =
  createSharedPathnamesNavigation({
    locales: ['en-GB'],
    localePrefix: 'as-needed',
  })

redirect('https://example.com')
// will result in a redirect to '/en-GBhttps://example.com'

permanentRedirect('https://example.com')
// will result in a redirect to '/en-GBhttps://example.com'

Expected behaviour:

const { redirect, permanentRedirect } =
  createSharedPathnamesNavigation({
    locales: ['en-GB'],
    localePrefix: 'as-needed',
  })

redirect('https://example.com')
// will result in a redirect to 'https://example.com'

permanentRedirect('https://example.com')
// will result in a redirect to 'https://example.com'

Proposed solution:

Include a check whether to localize the given pathname based on the path starting with a / in:

  • packages/next-intl/src/navigation/shared/baseRedirect.tsx
  • packages/next-intl/src/navigation/shared/basePermanentRedirect.tsx

If there are any questions, feedback, and or further improvements required please let me know 😄

Copy link

vercel bot commented May 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 23, 2024 6:38am
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 23, 2024 6:38am

Copy link

vercel bot commented May 22, 2024

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

A member of the Team first needs to authorize it.

@amannn
Copy link
Owner

amannn commented May 22, 2024

That sounds reasonable, thanks for the proposal!

Just some minor remarks:

  1. We had a similar PR for Link: feat: Improvements for localized Link #257. Can we make sure that we share the detection wether a link is external or not?
  2. Could you add some tests for the new behavior?
  3. If you don't mind, could you update useRouter to use this logic too?

Thanks a lot!

@mhogeveen
Copy link
Contributor Author

I've just updated the PR to use the same external link detection used in the ClientLink component and I've added a couple of tests for the baseRedirect and basePermanentRedirect functions. That covers point 1 & 2. Point 3 however already seems to be the case:

return localizeHref(href, nextLocale || locale, locale, curPathname);

export function localizeHref(
href: UrlObject | string,
locale: string,
curLocale: string = locale,
curPathname: string
) {
if (!isLocalHref(href) || isRelativeHref(href)) {
return href;
}
const isSwitchingLocale = locale !== curLocale;
const isPathnamePrefixed =
locale == null || hasPathnamePrefixed(locale, curPathname);
const shouldPrefix = isSwitchingLocale || isPathnamePrefixed;
if (shouldPrefix && locale != null) {
return prefixHref(href, locale);
}
return href;
}

If I'm missing something let me know!

 - Bump size
 - Fix formatting
 - Add tests for `localePrefix: 'always'`
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.

Fantastic PR, thanks a lot @mhogeveen! 🙌

I've added another commit with minor cleanup stuff, hope you don't mind.

Thanks also for checking useRouter! You're right, all the necessary handling was already in place there, only the redirect functions were missing.

@amannn amannn changed the title fix: Redirect external pathnames without localizing fix: Handle external URLs passed to redirect and permanentRedirect May 23, 2024
@amannn amannn merged commit 16b55e2 into amannn:main May 23, 2024
6 checks passed
@mhogeveen
Copy link
Contributor Author

Thanks for the quick response @amannn. It's much appreciated!

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.

None yet

2 participants