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

chore(nextjs): Improve experience when swapping keys on Keyless mode #4787

Conversation

panteliselef
Copy link
Member

@panteliselef panteliselef commented Dec 16, 2024

Description

In Keyless mode it is expected that the user will eventually set keys as environment variables. When they do that, the nextjs server will restart, but there seems to be a mismatch because first the rendering lifecycle runs with the new keys and then the keys are updated in the middleware. This causes an error to be thrown to the user which we can actually improve.

For the happy path: we avoid displaying that error by retrying to decrypt data with the dummy key.

For any other case: when swapping keys or going back to headless you will now see this (see picture). this does not only reply to keyless.

image

Checklist

  • pnpm test runs as expected.
  • pnpm 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:

@panteliselef panteliselef requested a review from a team December 16, 2024 15:27
@panteliselef panteliselef self-assigned this Dec 16, 2024
Copy link

changeset-bot bot commented Dec 16, 2024

🦋 Changeset detected

Latest commit: 9c2caa2

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

This PR includes changesets to release 1 package
Name Type
@clerk/nextjs 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

vercel bot commented Dec 16, 2024

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

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 16, 2024 6:51pm

Comment on lines +292 to +304
/**
* There is a great chance when running on Keyless mode that the above fails,
* because the keys hot-swapped and the Next.js dev server has not yet fully rebuilt middleware and routes.
*
* Attempt one more time with the default dummy value.
*/
if (canUseKeyless__server) {
try {
return decryptData(encryptedRequestData, KEYLESS_ENCRYPTION_KEY);
} catch (e) {
throwInvalidEncryptionKey();
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Simple retrying with the dummy value

'@clerk/nextjs': patch
---

Improve experience when swapping keys on Keyless mode.
Copy link
Member

Choose a reason for hiding this comment

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

💡 Should we mention here regarding the hot-swapped .env case? Just to clarify the "improve experience" part

'@clerk/nextjs': patch
---

Improve experience when swapping keys on Keyless mode.
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
Improve experience when swapping keys on Keyless mode.
Improve error message when swapping keys defined as environment variables. Affects (code=encryption_key_invalid).
**On Keyless mode**: Attempt to not throw the error.

Copy link
Member Author

Choose a reason for hiding this comment

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

@BRKalow @LauraBeatris is this better ?

@panteliselef panteliselef enabled auto-merge (squash) December 16, 2024 18:48
@panteliselef panteliselef merged commit 5ed3576 into main Dec 16, 2024
29 checks passed
@panteliselef panteliselef deleted the elef/actls-38-improve-possible-error-after-claiming-the-keys-and-adding branch December 16, 2024 18:54
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.

4 participants