Skip to content

Commit 34d673a

Browse files
authored
chore(nextjs): Use ezheaders to access headers and cookies (clerk#4392)
1 parent 434b432 commit 34d673a

File tree

8 files changed

+43
-43
lines changed

8 files changed

+43
-43
lines changed

.changeset/nasty-meals-call.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/nextjs": minor
3+
---
4+
5+
Replace `next/headers` with `ezheaders`

package-lock.json

+22-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/nextjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@clerk/shared": "2.10.1",
7373
"@clerk/types": "4.28.0",
7474
"crypto-js": "4.2.0",
75+
"ezheaders": "0.1.0",
7576
"server-only": "0.0.1",
7677
"tslib": "2.4.1"
7778
},
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use server';
22

3-
import { cookies } from 'next/headers';
3+
import { cookie } from 'ezheaders';
44

55
// This function needs to be async as we'd like to support next versions in the range of [14.1.2,14.2.0)
66
// These versions required 'use server' files to export async methods only. This check was later relaxed
77
// and the async is no longer required in newer next versions.
88
// ref: https://github.com/vercel/next.js/pull/62821
9-
export async function invalidateCacheAction() {
10-
void (await cookies()).delete(`__clerk_invalidate_cache_cookie_${Date.now()}`);
9+
export async function invalidateCacheAction(): Promise<void> {
10+
await cookie(`__clerk_invalidate_cache_cookie_${Date.now()}`, '');
1111
}

packages/nextjs/src/app-router/server/ClerkProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AuthObject } from '@clerk/backend';
22
import type { InitialState, Without } from '@clerk/types';
3-
import { headers } from 'next/headers';
3+
import { header } from 'ezheaders';
44
import nextPkg from 'next/package.json';
55
import React from 'react';
66

@@ -21,7 +21,7 @@ const getDynamicClerkState = React.cache(async function getDynamicClerkState() {
2121
});
2222

2323
const getNonceFromCSPHeader = React.cache(async function getNonceFromCSPHeader() {
24-
return getScriptNonceFromHeader((await headers()).get('Content-Security-Policy') || '') || '';
24+
return getScriptNonceFromHeader((await header('Content-Security-Policy')) || '') || '';
2525
});
2626

2727
export async function ClerkProvider(

packages/nextjs/src/app-router/server/utils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ export async function buildRequestLike() {
2323
try {
2424
// Dynamically import next/headers, otherwise Next12 apps will break
2525
// @ts-ignore: Cannot find module 'next/headers' or its corresponding type declarations.ts(2307)
26-
const { headers } = await import('next/headers');
27-
const resolvedHeaders = await headers();
28-
return new NextRequest('https://placeholder.com', { headers: resolvedHeaders });
26+
const { getHeaders } = await import('ezheaders');
27+
return new NextRequest('https://placeholder.com', { headers: await getHeaders() });
2928
} catch (e: any) {
3029
// rethrow the error when react throws a prerendering bailout
3130
// https://nextjs.org/docs/messages/ppr-caught-error

playground/app-router/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"next": "^14.2.4",
2424
"react": "18.2.0",
2525
"react-dom": "18.2.0",
26-
"typescript": "5.0.4"
26+
"typescript": "5.0.4",
27+
"ezheaders": "^0.0.3"
2728
}
2829
}

playground/app-router/src/app/protected/page.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useAuth, ClerkLoaded, SignedIn, SignedOut, UserButton } from '@clerk/nextjs';
1+
import { ClerkLoaded, SignedIn, SignedOut, UserButton } from '@clerk/nextjs';
2+
import { auth } from '@clerk/nextjs/server';
23
import React from 'react';
34
import { ClientSideWrapper } from '@/app/protected/ClientSideWrapper';
4-
import { headers } from 'next/headers';
5+
import { header } from 'ezheaders';
56

67
export default async function Page() {
7-
const { userId } = useAuth();
8-
const resolvedHeaders = await headers().get('x-clerk-debug');
8+
const { userId } = await auth();
9+
const xClerkDebug = await header('x-clerk-debug');
910

10-
console.log('Auth run in /protected', userId, resolvedHeaders.get('x-clerk-debug'), resolvedHeaders.keys());
11-
// console.log(auth());
11+
console.log('Auth run in /protected', userId, xClerkDebug);
1212
return (
1313
<div>
1414
<h1>Protected page</h1>

0 commit comments

Comments
 (0)