Skip to content

fix(clerk-js,astro,nuxt,tanstack-react-start,remix,react-router): Introduce helper function to prevent infinite handshake in Netlify - #5656

Merged
wobsoriano merged 22 commits into
mainfrom
rob/eco-603-netlify-handshake-fix
Apr 30, 2025
Merged

wobsoriano merged 22 commits into
mainfrom
rob/eco-603-netlify-handshake-fix

Conversation

@wobsoriano

@wobsoriano wobsoriano commented Apr 17, 2025 •

Copy link
Copy Markdown
Member

Description

This PR is a follow-up to #4745 but applies the changes to all SDKs (except @clerk/nextjs).

This fixes a handshake redirect loop issue in apps deployed to Netlify with Clerk development instance (see #5547). I opened a thread on Netlify's side about this with full info, which can be seen here. Multiple solutions have been tried, including the Netlify-Vary header with no success.

I also thought of moving the handleNetlifyCacheInDevInstance function to @clerk/backend but this may introduce unnecessary complexity. The goal of this PR is to temporarily fix the issue until we find a better solution but open for discussions!

You can test it with the ff. apps deployed to Netlify using this PRs snapshots + Clerk dev instance (you should not experience a redirect loop when visiting any of them):

Resolves ECO-603

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 Apr 17, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4eb903a

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

This PR includes changesets to release 19 packages
Name Type
@clerk/astro Patch
@clerk/clerk-js Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/remix Patch
@clerk/shared Patch
@clerk/tanstack-react-start Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/agent-toolkit Patch
@clerk/backend Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/clerk-react Patch
@clerk/testing Patch
@clerk/vue 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 Apr 17, 2025 •

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 Apr 30, 2025 8:00pm

@clerk clerk deleted a comment from clerk-cookie Apr 17, 2025
@wobsoriano

Copy link
Copy Markdown
Member Author

!snapshot

@clerk-cookie

Copy link
Copy Markdown
Collaborator

Hey @wobsoriano - the snapshot version command generated the following package versions:

Package Version
@clerk/agent-toolkit 0.0.24-snapshot.v20250417032557
@clerk/astro 2.6.6-snapshot.v20250417032557
@clerk/backend 1.29.1-snapshot.v20250417032557
@clerk/chrome-extension 2.2.31-snapshot.v20250417032557
@clerk/clerk-js 5.61.2-snapshot.v20250417032557
@clerk/elements 0.23.16-snapshot.v20250417032557
@clerk/clerk-expo 2.9.14-snapshot.v20250417032557
@clerk/expo-passkeys 0.2.8-snapshot.v20250417032557
@clerk/express 1.4.7-snapshot.v20250417032557
@clerk/fastify 2.2.7-snapshot.v20250417032557
@clerk/localizations 3.13.12-snapshot.v20250417032557
@clerk/nextjs 6.16.0-snapshot.v20250417032557
@clerk/nuxt 1.5.7-snapshot.v20250417032557
@clerk/clerk-react 5.28.2-snapshot.v20250417032557
@clerk/react-router 1.2.7-snapshot.v20250417032557
@clerk/remix 4.5.19-snapshot.v20250417032557
@clerk/shared 3.7.2-snapshot.v20250417032557
@clerk/tanstack-react-start 0.13.7-snapshot.v20250417032557
@clerk/testing 1.4.41-snapshot.v20250417032557
@clerk/themes 2.2.34-snapshot.v20250417032557
@clerk/types 4.54.2-snapshot.v20250417032557
@clerk/vue 1.6.3-snapshot.v20250417032557

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/agent-toolkit

npm i @clerk/agent-toolkit@0.0.24-snapshot.v20250417032557 --save-exact

@clerk/astro

npm i @clerk/astro@2.6.6-snapshot.v20250417032557 --save-exact

@clerk/backend

npm i @clerk/backend@1.29.1-snapshot.v20250417032557 --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@2.2.31-snapshot.v20250417032557 --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@5.61.2-snapshot.v20250417032557 --save-exact

@clerk/elements

npm i @clerk/elements@0.23.16-snapshot.v20250417032557 --save-exact

@clerk/clerk-expo

npm i @clerk/clerk-expo@2.9.14-snapshot.v20250417032557 --save-exact

@clerk/expo-passkeys

npm i @clerk/expo-passkeys@0.2.8-snapshot.v20250417032557 --save-exact

@clerk/express

npm i @clerk/express@1.4.7-snapshot.v20250417032557 --save-exact

@clerk/fastify

npm i @clerk/fastify@2.2.7-snapshot.v20250417032557 --save-exact

@clerk/localizations

npm i @clerk/localizations@3.13.12-snapshot.v20250417032557 --save-exact

@clerk/nextjs

npm i @clerk/nextjs@6.16.0-snapshot.v20250417032557 --save-exact

@clerk/nuxt

npm i @clerk/nuxt@1.5.7-snapshot.v20250417032557 --save-exact

@clerk/clerk-react

npm i @clerk/clerk-react@5.28.2-snapshot.v20250417032557 --save-exact

@clerk/react-router

npm i @clerk/react-router@1.2.7-snapshot.v20250417032557 --save-exact

@clerk/remix

npm i @clerk/remix@4.5.19-snapshot.v20250417032557 --save-exact

@clerk/shared

npm i @clerk/shared@3.7.2-snapshot.v20250417032557 --save-exact

@clerk/tanstack-react-start

npm i @clerk/tanstack-react-start@0.13.7-snapshot.v20250417032557 --save-exact

@clerk/testing

npm i @clerk/testing@1.4.41-snapshot.v20250417032557 --save-exact

@clerk/themes

npm i @clerk/themes@2.2.34-snapshot.v20250417032557 --save-exact

@clerk/types

npm i @clerk/types@4.54.2-snapshot.v20250417032557 --save-exact

@clerk/vue

npm i @clerk/vue@1.6.3-snapshot.v20250417032557 --save-exact

publishableKey: string;
}) {
// eslint-disable-next-line turbo/no-undeclared-env-vars
const isOnNetlify = process.env.URL?.endsWith('netlify.app') || Boolean(process.env.NETLIFY_FUNCTIONS_TOKEN);

@wobsoriano wobsoriano Apr 17, 2025 •

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.

URL and NETLIFY_FUNCTIONS_TOKEN are runtime environment variables injected by Netlify. We can use this to determine if this patch should run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't we use process.env.NETLIFY?

URL will not always end with netlify.app since it can also be your custom domain, as per https://docs.netlify.com/functions/environment-variables/#functions

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.

It's for build time only and not available in other SDKs at runtime (but worked in Astro 🤔 ). I added it just now as fallback!

@wobsoriano
wobsoriano requested a review from LekoArts April 17, 2025 04:01

@tmilewski tmilewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

:nit: It feels to me like packages/shared/src/__tests__/netlifyCacheHandler.ts would want to be packages/shared/src/__tests__/netlifyCacheHandler.test.ts

@tmilewski tmilewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🚀

@wobsoriano
wobsoriano merged commit 2716622 into main Apr 30, 2025
@wobsoriano
wobsoriano deleted the rob/eco-603-netlify-handshake-fix branch April 30, 2025 20:07

This branch was successfully deployed

1 active deployment
Preview — 4eb903a1 Deployed Apr 30, 2025 by vercel[bot]
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.

4 participants