forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loose RSC import restrictions for 3rd party packages (vercel#56501)
When we landed vercel#51179 it broke library like `apollo-client` as it's bundling client hooks into RSC bundle, so our RSC linter caught them and reported fatal errors. But those client hook APIs won't get executed in RSC. The original purpose of erroring on invalid hooks for server & client components was to catch bugs easier, but it might be too strict for the 3rd party libraries like `apollo-client` due to few reasons. We changed the rules only applying on user land source code. For 3rd party packages if they're not being imported correctly into proper server or client components, we're still showing runtime errors instead of fatal build errors. x-ref: apollographql/apollo-client#10974 Closes NEXT-1673
- Loading branch information
Showing
11 changed files
with
139 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
test/development/acceptance-app/fixtures/rsc-build-errors/next.config.js
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
test/development/acceptance-app/fixtures/rsc-runtime-errors/app/client/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use client' | ||
export default function page() { | ||
return 'page' | ||
} |
12 changes: 12 additions & 0 deletions
12
test/development/acceptance-app/fixtures/rsc-runtime-errors/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/development/acceptance-app/fixtures/rsc-runtime-errors/app/server/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function page() { | ||
return 'page' | ||
} |
6 changes: 6 additions & 0 deletions
6
...pment/acceptance-app/fixtures/rsc-runtime-errors/node_modules_bak/client-package/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useState } from 'react' | ||
|
||
export function callClientApi() { | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
return useState(0) | ||
} |
4 changes: 4 additions & 0 deletions
4
...t/acceptance-app/fixtures/rsc-runtime-errors/node_modules_bak/client-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "client-package", | ||
"exports": "./index.js" | ||
} |
7 changes: 7 additions & 0 deletions
7
...pment/acceptance-app/fixtures/rsc-runtime-errors/node_modules_bak/server-package/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use client' | ||
|
||
import { cookies } from 'next/headers' | ||
|
||
export function callServerApi() { | ||
return cookies() | ||
} |
4 changes: 4 additions & 0 deletions
4
...t/acceptance-app/fixtures/rsc-runtime-errors/node_modules_bak/server-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "server-package", | ||
"exports": "./index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
test/development/acceptance-app/rsc-runtime-errors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import path from 'path' | ||
import { outdent } from 'outdent' | ||
import { FileRef, createNextDescribe } from 'e2e-utils' | ||
import { | ||
check, | ||
getRedboxDescription, | ||
hasRedbox, | ||
shouldRunTurboDevTest, | ||
} from 'next-test-utils' | ||
|
||
createNextDescribe( | ||
'Error overlay - RSC runtime errors', | ||
{ | ||
files: new FileRef(path.join(__dirname, 'fixtures', 'rsc-runtime-errors')), | ||
packageJson: { | ||
scripts: { | ||
setup: 'cp -r ./node_modules_bak/* ./node_modules', | ||
build: 'yarn setup && next build', | ||
dev: `yarn setup && next ${ | ||
shouldRunTurboDevTest() ? 'dev --turbo' : 'dev' | ||
}`, | ||
start: 'next start', | ||
}, | ||
}, | ||
installCommand: 'yarn', | ||
startCommand: (global as any).isNextDev ? 'yarn dev' : 'yarn start', | ||
}, | ||
({ next }) => { | ||
it('should show runtime errors if invalid client API from node_modules is executed', async () => { | ||
await next.patchFile( | ||
'app/server/page.js', | ||
outdent` | ||
import { callClientApi } from 'client-package' | ||
export default function Page() { | ||
callClientApi() | ||
return 'page' | ||
} | ||
` | ||
) | ||
|
||
const browser = await next.browser('/server') | ||
|
||
await check( | ||
async () => ((await hasRedbox(browser, true)) ? 'success' : 'fail'), | ||
/success/ | ||
) | ||
const errorDescription = await getRedboxDescription(browser) | ||
|
||
expect(errorDescription).toContain( | ||
`Error: useState only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/react-client-hook-in-server-component` | ||
) | ||
}) | ||
|
||
it('should show runtime errors if invalid server API from node_modules is executed', async () => { | ||
await next.patchFile( | ||
'app/client/page.js', | ||
outdent` | ||
'use client' | ||
import { callServerApi } from 'server-package' | ||
export default function Page() { | ||
callServerApi() | ||
return 'page' | ||
} | ||
` | ||
) | ||
|
||
const browser = await next.browser('/client') | ||
|
||
await check( | ||
async () => ((await hasRedbox(browser, true)) ? 'success' : 'fail'), | ||
/success/ | ||
) | ||
const errorDescription = await getRedboxDescription(browser) | ||
|
||
expect(errorDescription).toContain( | ||
`Error: Invariant: cookies() expects to have requestAsyncStorage, none available.` | ||
) | ||
}) | ||
} | ||
) |