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

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

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-chairs-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/nextjs": patch
---

Return build-time error if `auth` and `currentUser` server-side helpers are imported into Client Components.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@clerk/types": "4.8.0",
"crypto-js": "4.2.0",
"path-to-regexp": "6.2.2",
"server-only": "0.0.1",
"tslib": "2.4.1"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'server-only';
Copy link
Member Author

@LauraBeatris LauraBeatris Jul 18, 2024

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.


import type { AuthObject, RedirectFun } from '@clerk/backend/internal';
import { constants, createClerkRequest, createRedirect } from '@clerk/backend/internal';
import { notFound, redirect } from 'next/navigation';
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/src/app-router/server/currentUser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'server-only';

import type { User } from '@clerk/backend';

import { clerkClient } from '../../server/clerkClient';
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/src/server/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
jest.mock('server-only', () => null);
Copy link
Member Author

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)


import * as publicExports from '../index';

describe('/server public exports', () => {
Expand Down
Loading