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

Add unstable_useFlushEffects hook #34117

Merged
merged 33 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b47023c
Add useFlushEffect
devknoll Feb 9, 2022
4cd5560
Flush styled-jsx using a flush effect
devknoll Feb 9, 2022
2890aeb
Better typing and tweaks
devknoll Feb 10, 2022
1d8f789
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 10, 2022
c18f87d
More types
devknoll Feb 10, 2022
ba6a9f7
Fix stream outputing uint8array
devknoll Feb 10, 2022
84d9d6b
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 10, 2022
0c0416d
Change to useFlushEffects
devknoll Feb 11, 2022
a9b66cc
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 14, 2022
e7c256d
Initial tests
devknoll Feb 14, 2022
a0f686b
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 14, 2022
55c5d74
Revert runtime
devknoll Feb 14, 2022
a708d8a
More tests
devknoll Feb 14, 2022
9bd184b
Fix lint
devknoll Feb 14, 2022
5a6364d
Fix tests
devknoll Feb 14, 2022
744d64d
Fix lint
devknoll Feb 14, 2022
ca70712
Fix lint
devknoll Feb 14, 2022
d77a1f8
Fix tests
devknoll Feb 14, 2022
cc85323
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 15, 2022
866c27e
Fix tests
devknoll Feb 15, 2022
2623d54
Add docs and fix tests
devknoll Feb 15, 2022
90bd65f
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 15, 2022
0c992a5
Fix tests
devknoll Feb 15, 2022
c51b271
Fix test
devknoll Feb 15, 2022
2683de3
Fix tests
devknoll Feb 16, 2022
d20e051
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 16, 2022
a90bffa
Fix tests
devknoll Feb 16, 2022
3943895
Fix tests
devknoll Feb 16, 2022
72d87da
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 16, 2022
c538862
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 17, 2022
edc116f
Prefix w/ unstable and fix unnecessary assertion
devknoll Feb 17, 2022
fec9cb6
Remove key that is already being set by styled-jsx
devknoll Feb 17, 2022
3f38f17
Merge branch 'canary' into x-add-use-flush-effect
devknoll Feb 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions errors/client-flush-effects.md
@@ -0,0 +1,24 @@
# `useFlushEffects` can not be called on the client

#### Why This Error Occurred

The `useFlushEffects` hook was executed while rendering a component on the client, or in another unsupported environment.

#### Possible Ways to Fix It

The `useFlushEffects` hook can only be called while _server rendering a client component_. As a best practice, we recommend creating a wrapper hook:

```jsx
// lib/use-style-libraries.js
import { useFlushEffects } from 'next/streaming'

export default function useStyleLibraries() {
if (typeof window === 'undefined') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useFlushEffects([
/* ... */
])
}
/* ... */
}
```
8 changes: 8 additions & 0 deletions errors/manifest.json
Expand Up @@ -623,6 +623,14 @@
{
"title": "opening-an-issue",
"path": "/errors/opening-an-issue.md"
},
{
"title": "multiple-flush-effects",
"path": "/errors/multiple-flush-effects.md"
},
{
"title": "client-flush-effects",
"path": "/errors/client-flush-effects.md"
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions errors/multiple-flush-effects.md
@@ -0,0 +1,9 @@
# The `useFlushEffects` hook cannot be used more than once.

#### Why This Error Occurred

The `useFlushEffects` hook is being used more than once while a page is being rendered.

#### Possible Ways to Fix It

The `useFlushEffects` hook should only be called inside the body of the `pages/_app` component, before returning any `<Suspense>` boundaries. Restructure your application to prevent extraneous calls.
1 change: 1 addition & 0 deletions packages/next/client/streaming/index.ts
@@ -1,2 +1,3 @@
export { useRefreshRoot as unstable_useRefreshRoot } from './refresh'
export { useWebVitalsReport as unstable_useWebVitalsReport } from './vitals'
export { useFlushEffects as unstable_useFlushEffects } from '../../shared/lib/flush-effects'
4 changes: 2 additions & 2 deletions packages/next/server/render-result.ts
@@ -1,9 +1,9 @@
import type { ServerResponse } from 'http'

export default class RenderResult {
_result: string | ReadableStream
_result: string | ReadableStream<Uint8Array>

constructor(response: string | ReadableStream) {
constructor(response: string | ReadableStream<Uint8Array>) {
this._result = response
}

Expand Down