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(astro): Prevent Netlify from doing a handshake redirect loop #4745

Merged

Conversation

wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Dec 10, 2024

Description

Netlify caches the responses from pages. When the middleware initiates a handshake redirect and returns to the original URL, Netlify serves the cached response (along with the cookies), leading to an infinite handshake redirect loop.

I've tried using Netlify's cache key variations but it doesn't seem to work.

This PR adds a cache bust parameter to the original URL to prevent Netlify from serving the cached response. The parameter is automatically removed from the URL after the page loads.

I don't have e2e tests for it, but you can compare the ff.:

  1. https://astro-clerk-template.netlify.app - one without fix, notice the redirect loop after signing in
  2. https://deploy-preview-2--astro-clerk-template.netlify.app - the one with fix, should work as expected after signing in and handshake flow

Resolves ECO-202

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:

Copy link

changeset-bot bot commented Dec 10, 2024

🦋 Changeset detected

Latest commit: 4e352f9

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

This PR includes changesets to release 1 package
Name Type
@clerk/astro 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 10, 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 10, 2024 9:57pm

function handleNetlifyCacheInDevInstance(locationHeader: string, requestState: RequestState) {
// Only run on Netlify environment and Clerk development instance
// eslint-disable-next-line turbo/no-undeclared-env-vars
if (import.meta.env.NETLIFY && isDevelopmentFromPublishableKey(requestState.publishableKey)) {
Copy link
Member Author

@wobsoriano wobsoriano Dec 10, 2024

Choose a reason for hiding this comment

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

import.meta.env.NETLIFY is an env var provided by NETLIFY. You can test the result here.

@wobsoriano wobsoriano marked this pull request as ready for review December 10, 2024 20:23
@wobsoriano wobsoriano changed the title fix(astro): Prevent Netlify adapter from doing a handshake redirect loop fix(astro): Prevent Netlify from doing a handshake redirect loop Dec 10, 2024
@wobsoriano wobsoriano merged commit 12f92e9 into main Dec 11, 2024
29 checks passed
@wobsoriano wobsoriano deleted the rob/eco-202-redirect-loop-in-netlify-and-clerk-dev-instance branch December 11, 2024 16:02
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.

3 participants