Skip to content

Commit 42d6e30

Browse files
authored
Fix useOptimistic in server components bug. Add tests for invalid React server APIs (#59621)
## What? Fixes a bug where `useOptimistic` wouldn't trigger a compiler error when imported in Server Components. Adds tests for the following `import { x } from 'react'` in Server Components, where `x` is the value: - Component - createContext - createFactory - PureComponent - useDeferredValue - useEffect - useImperativeHandle - useInsertionEffect - useLayoutEffect - useReducer - useRef - useState - useSyncExternalStore - useTransition - useOptimistic These show a particular error explaining how to add `"use client"`: ![CleanShot 2023-12-14 at 14 49 37@2x](https://github.com/vercel/next.js/assets/6324199/e47eab71-b2a2-4c14-bec0-0d5cdd720e80) <!-- Thanks for opening a PR! Your contribution is much appreciated.i: To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 6b3d145 commit 42d6e30

File tree

18 files changed

+159
-2
lines changed

18 files changed

+159
-2
lines changed

packages/next-swc/crates/core/src/react_server_components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ pub fn server_components<C: Comments>(
627627
JsWord::from("useState"),
628628
JsWord::from("useSyncExternalStore"),
629629
JsWord::from("useTransition"),
630-
JsWord::from("experimental_useOptimistic"),
630+
JsWord::from("useOptimistic"),
631631
],
632632
})
633633
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from 'react'
2+
3+
console.log({ Component })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createContext } from 'react'
2+
3+
console.log({ createContext })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createFactory } from 'react'
2+
3+
console.log({ createFactory })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { PureComponent } from 'react'
2+
3+
console.log({ PureComponent })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useDeferredValue } from 'react'
2+
3+
console.log({ useDeferredValue })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useEffect } from 'react'
2+
3+
console.log({ useEffect })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useImperativeHandle } from 'react'
2+
3+
console.log({ useImperativeHandle })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useInsertionEffect } from 'react'
2+
3+
console.log({ useInsertionEffect })
4+
5+
export default function Page() {
6+
return null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useLayoutEffect } from 'react'
2+
3+
console.log({ useLayoutEffect })
4+
5+
export default function Page() {
6+
return null
7+
}

0 commit comments

Comments
 (0)