Skip to content

chore(nextjs): Introduce build-time error for server-only helpers on client-side - #3758

Merged
LauraBeatris merged 4 commits into
mainfrom
server-only-build-time-error
Jul 18, 2024
Merged

chore(nextjs): Introduce build-time error for server-only helpers on client-side#3758
LauraBeatris merged 4 commits into
mainfrom
server-only-build-time-error

Conversation

@LauraBeatris

@LauraBeatrisLauraBeatris commented Jul 18, 2024

Copy link
Copy Markdown
Contributor

Description

Resolves#3660

Uses the server-only package to throw a build-time error if Clerk server-side helpers get accidentally imported on the client side.

CleanShot 2024-07-18 at 09 57 12@2x

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:

@LauraBeatrisLauraBeatris self-assigned this Jul 18, 2024
@changeset-bot

changeset-botBot commented Jul 18, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e8048e9

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

This PR includes changesets to release 1 package
NameType
@clerk/nextjsPatch

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

@LauraBeatris
LauraBeatrisforce-pushed the server-only-build-time-error branch from 1fa5832 to 790eea2CompareJuly 18, 2024 13:05
@LauraBeatrisLauraBeatris changed the title chore(nextjs): Introduce build-time error for server-only helpers on client-side contextchore(nextjs): Introduce build-time error for server-only helpers on client-side Jul 18, 2024
@LauraBeatrisLauraBeatris changed the title chore(nextjs): Introduce build-time error for server-only helpers on client-side chore(nextjs): Introduce build-time error for server-only helpers on client-sideJul 18, 2024
@LauraBeatris
LauraBeatris marked this pull request as ready for review July 18, 2024 13:59
@LauraBeatris
LauraBeatris requested review from a team and brkalowJuly 18, 2024 13:59
@@ -1,3 +1,5 @@
jest.mock('server-only', () => null);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's tricky to run server-only on Jest due to its multiple environments. I went ahead and decided to mock it instead:

reactjs/rfcs#227 (comment)

@@ -1,3 +1,5 @@
import 'server-only';

@LauraBeatrisLauraBeatrisJul 18, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I've imported it separately on auth and currentUser only instead of importing it once on server index.

My concern is that the error would be thrown even when importing type definitions, but I'd like to minimize the surface here.

@LauraBeatris
LauraBeatris merged commit 18bcfbc into mainJul 18, 2024
@LauraBeatris
LauraBeatris deleted the server-only-build-time-error branch July 18, 2024 17:44
Sign up for freeto 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.

@clerk/nextjs@5.2.0 cause a build warning in NextJS middleware

3 participants

@LauraBeatris@tmilewski@clerk-cookie