Skip to content

fix(astro): Prevent Netlify from doing a handshake redirect loop - #4745

Merged
wobsoriano merged 11 commits into
mainfrom
rob/eco-202-redirect-loop-in-netlify-and-clerk-dev-instance
Dec 11, 2024
Merged

wobsoriano merged 11 commits into
mainfrom
rob/eco-202-redirect-loop-in-netlify-and-clerk-dev-instance

Conversation

@wobsoriano

@wobsoriano wobsoriano commented Dec 10, 2024 •

Copy link
Copy Markdown
Member

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:

@changeset-bot

changeset-bot Bot commented Dec 10, 2024 •

Copy link
Copy Markdown

🦋 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

@vercel

vercel Bot commented Dec 10, 2024 •

Copy link
Copy Markdown

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)) {

@wobsoriano wobsoriano Dec 10, 2024 •

Copy link
Copy Markdown
Member Author

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

This branch was successfully deployed

1 active deployment
Preview — 4e352f90 Deployed Dec 10, 2024 by vercel[bot]
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