-
Couldn't load subscription status.
- Fork 29.7k
Fix validate use server in pages router #85282
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
02a995e
fix: validate 'use server' directive in Pages Router
swarnava dc7458a
chore: run prettier-fix on test file
swarnava 759fec8
fix: update PARSE_PATTERN to include 'use server' and 'use client' di…
swarnava e9810d4
docs: add comments explaining validation scope and server-only recomm…
swarnava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
84 changes: 84 additions & 0 deletions
84
test/development/acceptance/use-directives-in-pages-router.test.ts
This file contains hidden or 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,84 @@ | ||
| /* eslint-env jest */ | ||
| import { nextTestSetup } from 'e2e-utils' | ||
| import { createSandbox } from 'development-sandbox' | ||
| import { outdent } from 'outdent' | ||
|
|
||
| const initialFiles = new Map([ | ||
| ['app/_.js', ''], // app dir need to exists, otherwise the SWC RSC checks will not run | ||
| [ | ||
| 'pages/index.js', | ||
| outdent` | ||
| export default function Page() { | ||
| return <div>Hello</div> | ||
| } | ||
| `, | ||
| ], | ||
| ]) | ||
|
|
||
| describe('Error for "use server" directive in Pages Router', () => { | ||
| const { next } = nextTestSetup({ | ||
| files: {}, | ||
| skipStart: true, | ||
| }) | ||
|
|
||
| test('"use server" directive is not allowed in Pages Router', async () => { | ||
| await using sandbox = await createSandbox( | ||
| next, | ||
| new Map([ | ||
| ...initialFiles, | ||
| [ | ||
| 'pages/test-page.js', | ||
| outdent` | ||
| "use server" | ||
|
|
||
| export default function Page() { | ||
| return <div>Hello</div> | ||
| } | ||
| `, | ||
| ], | ||
| ]) | ||
| ) | ||
| const { session } = sandbox | ||
|
|
||
| await session.assertHasRedbox() | ||
| await expect(session.getRedboxSource()).resolves.toMatch( | ||
| /cannot use "use server" directive.*Server Actions are only supported in the App Router/ | ||
| ) | ||
| }) | ||
|
|
||
| test('"use client" directive is allowed in Pages Router', async () => { | ||
| await using sandbox = await createSandbox( | ||
| next, | ||
| new Map([ | ||
| ...initialFiles, | ||
| [ | ||
| 'pages/test-page.js', | ||
| outdent` | ||
| "use client" | ||
|
|
||
| export default function Page() { | ||
| return <div>Hello</div> | ||
| } | ||
| `, | ||
| ], | ||
| ]) | ||
| ) | ||
| const { session, browser } = sandbox | ||
|
|
||
| // Should not have a redbox - "use client" is allowed | ||
| await session.assertNoRedbox() | ||
|
|
||
| const text = await browser.elementByCss('body').text() | ||
| expect(text).toContain('Hello') | ||
| }) | ||
|
|
||
| test('Pages Router pages without directives work correctly', async () => { | ||
| await using sandbox = await createSandbox(next, initialFiles) | ||
| const { session, browser } = sandbox | ||
|
|
||
| await session.assertNoRedbox() | ||
|
|
||
| const text = await browser.elementByCss('body').text() | ||
| expect(text).toContain('Hello') | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.