Skip to content

fix(nextjs): Remove sourcemaps from bundled server-actions #3773

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

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

nikosdouvlis
Copy link
Member

@nikosdouvlis nikosdouvlis commented Jul 19, 2024

Description

This PR excludes all server actions imported by client components. Even though we correctly build and bundle the Nextjs package (compiled files + sourcemaps), it seems that the sourcemaps for any server actions imported by client components are not served by nextjs when running npm run dev or npm run start.

This PR solves the Error: Clerk: auth() was called but Clerk can't detect usage of authMiddleware(). error many users where getting during init. The error only popped up when browser devtools were open, as the browser would eventually request the sourcemap for the server action.

The URL of the request is similar to /_next/...../filename.map.js which our default matcher ignores, so the middleware wouldn't run. The Clerk error would then be thrown if any Clerk helpers were used in a layout as the 404 page still renders that layout.

Checklist

  • npm test runs as expected.
  • npm run 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 Jul 19, 2024

🦋 Changeset detected

Latest commit: 5b833ab

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

@nikosdouvlis nikosdouvlis force-pushed the nikos/hotfix/server-actions-sourcemaps branch from 8ec6e1b to 910e99d Compare July 19, 2024 19:21
@nikosdouvlis
Copy link
Member Author

!snapshot

@clerk clerk deleted a comment from clerk-cookie Jul 19, 2024
@clerk-cookie
Copy link
Collaborator

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

Package Version
@clerk/astro 1.0.1-snapshot.v910e99d
@clerk/backend 1.4.0-snapshot.v910e99d
@clerk/chrome-extension 1.1.5-snapshot.v910e99d
@clerk/clerk-js 5.10.0-snapshot.v910e99d
@clerk/elements 0.10.5-snapshot.v910e99d
@clerk/clerk-expo 1.2.8-snapshot.v910e99d
@clerk/express 0.0.18-snapshot.v910e99d
@clerk/fastify 1.0.20-snapshot.v910e99d
gatsby-plugin-clerk 5.0.0-beta.45
@clerk/localizations 2.5.0-snapshot.v910e99d
@clerk/nextjs 5.2.5-snapshot.v910e99d
@clerk/clerk-react 5.2.9-snapshot.v910e99d
@clerk/remix 4.2.4-snapshot.v910e99d
@clerk/clerk-sdk-node 5.0.17-snapshot.v910e99d
@clerk/shared 2.4.0-snapshot.v910e99d
@clerk/tanstack-start 0.1.8-snapshot.v910e99d
@clerk/testing 1.2.0-snapshot.v910e99d
@clerk/themes 2.1.13-snapshot.v910e99d
@clerk/types 4.9.0-snapshot.v910e99d

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

npm i @clerk/astro@1.0.1-snapshot.v910e99d --save-exact

@clerk/backend

npm i @clerk/backend@1.4.0-snapshot.v910e99d --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@1.1.5-snapshot.v910e99d --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@5.10.0-snapshot.v910e99d --save-exact

@clerk/elements

npm i @clerk/elements@0.10.5-snapshot.v910e99d --save-exact

@clerk/clerk-expo

npm i @clerk/clerk-expo@1.2.8-snapshot.v910e99d --save-exact

@clerk/express

npm i @clerk/express@0.0.18-snapshot.v910e99d --save-exact

@clerk/fastify

npm i @clerk/fastify@1.0.20-snapshot.v910e99d --save-exact

gatsby-plugin-clerk

npm i gatsby-plugin-clerk@5.0.0-beta.45 --save-exact

@clerk/localizations

npm i @clerk/localizations@2.5.0-snapshot.v910e99d --save-exact

@clerk/nextjs

npm i @clerk/nextjs@5.2.5-snapshot.v910e99d --save-exact

@clerk/clerk-react

npm i @clerk/clerk-react@5.2.9-snapshot.v910e99d --save-exact

@clerk/remix

npm i @clerk/remix@4.2.4-snapshot.v910e99d --save-exact

@clerk/clerk-sdk-node

npm i @clerk/clerk-sdk-node@5.0.17-snapshot.v910e99d --save-exact

@clerk/shared

npm i @clerk/shared@2.4.0-snapshot.v910e99d --save-exact

@clerk/tanstack-start

npm i @clerk/tanstack-start@0.1.8-snapshot.v910e99d --save-exact

@clerk/testing

npm i @clerk/testing@1.2.0-snapshot.v910e99d --save-exact

@clerk/themes

npm i @clerk/themes@2.1.13-snapshot.v910e99d --save-exact

@clerk/types

npm i @clerk/types@4.9.0-snapshot.v910e99d --save-exact

Copy link
Member

@panteliselef panteliselef left a comment

Choose a reason for hiding this comment

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

Nice catch, was this happening for a while, or after a specific nextjs version ?

This PR excludes all server actions imported by client components. Even though we correctly build and bundle the Nextjs package (compiled files + sourcemaps), it seems that the sourcemaps for any server actions imported by client components are not served by nextjs when running npm run dev or npm run start.

This PR solves the Error: Clerk: auth() was called but Clerk can't detect usage of authMiddleware(). error many users where getting during init. The error only popped up when browser devtools were open, as the browser would eventually request the sourcemap for the server action.

The URL of the request is similar to /_next/...../filename.map.js which our default matcher ignores, so the middleware wouldn't run. The Clerk error would then be thrown if any Clerk helpers were used in a layout as the 404 page still renders that layout.
@nikosdouvlis nikosdouvlis force-pushed the nikos/hotfix/server-actions-sourcemaps branch from 910e99d to 5b833ab Compare July 22, 2024 09:48
@nikosdouvlis
Copy link
Member Author

Nice catch, was this happening for a while, or after a specific nextjs version ?

I'm not sure when/if sourcemap handling changed at some point by Nextjs, but we introduced the invalidateCache server action in @clerk/nextjs@5.1.5.

@nikosdouvlis nikosdouvlis merged commit 9bb676e into main Jul 22, 2024
3 checks passed
@nikosdouvlis nikosdouvlis deleted the nikos/hotfix/server-actions-sourcemaps branch July 22, 2024 09:51
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.

3 participants