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(clerk-js): Add push/replace props & replace state when merging fragment into url #1304

Merged
merged 3 commits into from
Nov 28, 2023

Conversation

desiprisg
Copy link
Contributor

@desiprisg desiprisg commented Jun 6, 2023

Type of change

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

Packages affected

  • @clerk/clerk-js
  • @clerk/clerk-react
  • @clerk/nextjs
  • @clerk/remix
  • @clerk/types
  • @clerk/themes
  • @clerk/localizations
  • @clerk/clerk-expo
  • @clerk/backend
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/fastify
  • @clerk/chrome-extension
  • gatsby-plugin-clerk
  • build/tooling/chore

Description

  • npm test runs as expected.
  • npm run build runs as expected.

When using path routing and clicking on a sign-in/up link, we get rerouted to a hash based url which automatically gets converted to a path based url (that's happening inside PathRouter.tsx). After that, when the back button is pressed, we arrive at the hash-based url that got converted and not at the actual previous page we were on.

Solution: Add a routerReplace prop to <ClerkProvider/> and use that for merging the fragment into the url.

Edit: This PR also includes the new routerPush prop that replaces the navigate prop for v5.

Before:

Screen.Recording.2023-05-26.at.03.39.21.mov

After:

Screen.Recording.2023-05-26.at.04.38.49.mov

@changeset-bot
Copy link

changeset-bot bot commented Jun 6, 2023

🦋 Changeset detected

Latest commit: 7f1a039

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

This PR includes changesets to release 11 packages
Name Type
@clerk/nextjs Major
gatsby-plugin-clerk Major
@clerk/clerk-js Major
@clerk/remix Major
@clerk/types Major
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/backend Patch
@clerk/fastify Patch
@clerk/clerk-react Patch
@clerk/clerk-sdk-node 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

Copy link

@jit-ci jit-ci bot left a comment

Choose a reason for hiding this comment

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

✅ Great news! Jit hasn't found any security issues in your PR. Good Job! 🏆

@colinclerk
Copy link
Member

This is a good change, but there's a reasonable chance it will lead to errors. The issue is we can't predict if we're setting an appropriate state variable for the framework's router.

We probably won't see an issue unless we:

  • Call window.history.replaceState
  • Complete the first step of sign-up
  • Then click the back button, to return to the start step of the sign-up

The back button will fire the Window popstate event, which the framework's router will be listening to so it can update it's own internal state. At that point, it's possible the framework can get confused by our manual entry via window.history.replaceState and throw an error.

Passing window.history.state is a good countermeasure and I suspect it will work for navigations to the root of the component, but I'd still worry about navigations to the non-root (for example, /sign-in#/choose for afterSignOutOne).

This issue is why we don't call window.history.pushState directly, but instead use the framework's wrapper of pushState (e.g. router.push in Next.js).

The safer approach for this fix is to use the framework's wrapper around window.history.replaceState, too (e.g. router.replace in Next.js)

I've been thinking we should accept both push and replace variants to <ClerkProvider> in v5, but if there's urgency in v4 we can find a way.

@desiprisg
Copy link
Contributor Author

@colinclerk We'll do some more tests to make sure this works, if we're going to go ahead with it. I didn't think accepting a replace variant was an option right now as well.

@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch from 39e05ed to 2e8a973 Compare July 6, 2023 11:29
@desiprisg desiprisg marked this pull request as draft July 6, 2023 11:35
@desiprisg desiprisg marked this pull request as ready for review July 6, 2023 12:59
@dimkl dimkl assigned dimkl and desiprisg and unassigned dimkl Aug 9, 2023
@jescalan jescalan force-pushed the replace_state_on_fragment_merge branch from d664719 to 356d2d1 Compare September 6, 2023 15:17
@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch from 356d2d1 to c1fe07e Compare October 25, 2023 14:45
@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch 3 times, most recently from 5028633 to 20c4eb1 Compare November 1, 2023 14:21
@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch from 20c4eb1 to 6adc889 Compare November 1, 2023 14:39
@desiprisg desiprisg changed the title fix(clerk-js): Replace state when merging fragment into url fix(clerk-js): Add push/replace props & replace state when merging fragment into url Nov 1, 2023
packages/types/src/clerk.ts Outdated Show resolved Hide resolved
packages/remix/src/client/RemixClerkProvider.tsx Outdated Show resolved Hide resolved
packages/nextjs/src/app-beta/client/ClerkProvider.tsx Outdated Show resolved Hide resolved
packages/gatsby-plugin-clerk/src/GatsbyClerkProvider.tsx Outdated Show resolved Hide resolved
playground/vite-react-ts/src/App.tsx Outdated Show resolved Hide resolved
@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch 3 times, most recently from 08b376b to 265a880 Compare November 15, 2023 18:52
@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch from 265a880 to df3094c Compare November 15, 2023 18:59
.changeset/gold-islands-cover.md Outdated Show resolved Hide resolved
.changeset/stupid-suits-accept.md Outdated Show resolved Hide resolved
Copy link
Member

@BRKalow BRKalow left a comment

Choose a reason for hiding this comment

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

Implementation looks good, seems like CI is failing though.

@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch 3 times, most recently from 9e9b31f to feb73d6 Compare November 22, 2023 16:49
Copy link
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

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

Please also add the changes to the Upgrade v4 -> v5 Guide [DRAFT].

packages/clerk-js/src/core/clerk.ts Outdated Show resolved Hide resolved
package-lock.json Outdated Show resolved Hide resolved
@nikosdouvlis
Copy link
Member

@desiprisg let's please add at least one e2e test to cover this.

@desiprisg desiprisg force-pushed the replace_state_on_fragment_merge branch from feb73d6 to ed4ac78 Compare November 23, 2023 14:42
@dimkl dimkl force-pushed the replace_state_on_fragment_merge branch from ed4ac78 to 7f1a039 Compare November 28, 2023 12:28
@dimkl dimkl enabled auto-merge November 28, 2023 12:30
@dimkl dimkl added this pull request to the merge queue Nov 28, 2023
Merged via the queue into main with commit 9a1fe37 Nov 28, 2023
7 checks passed
@dimkl dimkl deleted the replace_state_on_fragment_merge branch November 28, 2023 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants