Skip to content

fix(elements): Use window.location to navigate for absolute URLs#3934

Closed
dstaley wants to merge 1 commit intomainfrom
ds.feat/elements-absolute-url-redirect
Closed

fix(elements): Use window.location to navigate for absolute URLs#3934
dstaley wants to merge 1 commit intomainfrom
ds.feat/elements-absolute-url-redirect

Conversation

@dstaley
Copy link
Member

@dstaley dstaley commented Aug 12, 2024

Description

Adds support for navigating to absolute URLs via context.router.push. This allows redirect_url to be an absolute URL.

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link

changeset-bot bot commented Aug 12, 2024

🦋 Changeset detected

Latest commit: 8828f25

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/elements Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +25 to +31
push: (path: string) => {
if (isAbsoluteUrl(path)) {
window.location.href = path;
} else {
router.push(path);
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe i am missing some context here, but is this different from this windowNavigate within clerk-js

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I believe the nextjs router's push will respect the absolute path and it will perform the navigation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I totally thought the same thing about push, but in my testing it did not (at least with cross-origin domains, not sure about same-origin). I know the <Link> component handles cross-origin absolute URLs though, so I think that's where I got confused.

We can definitely bring windowNavigate over into Elements for the additional functionality. Good call!

Copy link
Contributor

Choose a reason for hiding this comment

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

I tested with App router router.push('https://google.com') and seems to work. i've not tested with absolute url and same origin tho.

If you see something different, it may be an inconsistency across nextjs versions. so having the above logic will be required in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're absolutely right. In Elements we have two routers, ClerkRouter and FrameworkRouter, which ClerkRouter wraps. I was calling ClerkRouter.push(absoluteUrl), which doesn't work because it transforms the URL into /?redirect_url=absoluteUrl, and then calls FrameworkRouter.push("/?redirect_url=absoluteUrl"), which doesn't work. Calling FrameworkRouter.push(absoluteUrl) totally works as expected.

I'll close this PR in favor of a new one that reworks ClerkRouter to prevent the transformation of absolute URLs.

@dstaley
Copy link
Member Author

dstaley commented Aug 13, 2024

Closing in favor of #3947

@dstaley dstaley closed this Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants