Unverified Commit f086283e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Remove the next/streaming endpoint and experimental refresh api (#38837)

Drop the `next/streaming` endpoint. Currently it only holds the only one experimental api and we'd like to deprecate it in favor of oher future APIs for app dir
parent ea9f8552
# React 18 # React 18
[React 18](https://reactjs.org/blog/2022/03/29/react-v18.html) adds new features including Suspense, automatic batching of updates, APIs like `startTransition`, and a new streaming API for server rendering with support for `React.lazy`. [React 18](https://reactjs.org/blog/2022/03/29/react-v18.html) adds new features including Suspense, automatic batching of updates, APIs like `startTransition`, and a new streaming API for server rendering with support for `React.lazy`.
Next.js also provides streaming related APIs, please checkout [next/streaming](/docs/api-reference/next/streaming.md) for details.
React 18 is now released. Read more about [React 18](https://reactjs.org/blog/2022/03/29/react-v18.html). React 18 is now released. Read more about [React 18](https://reactjs.org/blog/2022/03/29/react-v18.html).
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
React 18 includes architectural improvements to React server-side rendering (SSR) performance. This means you can use `Suspense` in your React components in streaming SSR mode and React will render content on the server and send updates through HTTP streams. React 18 includes architectural improvements to React server-side rendering (SSR) performance. This means you can use `Suspense` in your React components in streaming SSR mode and React will render content on the server and send updates through HTTP streams.
React Server Components, an experimental feature, is based on streaming. You can read more about Server Components related streaming APIs in [`next/streaming`](/docs/api-reference/next/streaming.md). However, this guide focuses on streaming with React 18.
## Using Streaming Server-Rendering ## Using Streaming Server-Rendering
When you use Suspense in a server-rendered page, there is no extra configuration required to use streaming SSR. When deployed, streaming can be utilized through infrastructure like [Edge Functions](https://vercel.com/edge) on Vercel (with the Edge Runtime) or with a Node.js server (with the Node.js runtime). AWS Lambda Functions do not currently support streaming responses. When you use Suspense in a server-rendered page, there is no extra configuration required to use streaming SSR. When deployed, streaming can be utilized through infrastructure like [Edge Functions](https://vercel.com/edge) on Vercel (with the Edge Runtime) or with a Node.js server (with the Node.js runtime). AWS Lambda Functions do not currently support streaming responses.
...@@ -12,7 +10,7 @@ All SSR pages have the ability to render components into streams and the client ...@@ -12,7 +10,7 @@ All SSR pages have the ability to render components into streams and the client
As an added bonus, in streaming SSR mode the client will also use selective hydration to prioritize component hydration based on user interactions, further improving performance. As an added bonus, in streaming SSR mode the client will also use selective hydration to prioritize component hydration based on user interactions, further improving performance.
For non-SSR pages, all Suspense boundaries will still be [statically optimized](/docs/advanced-features/automatic-static-optimization.md). Check out [`next/streaming`](/docs/api-reference/next/streaming.md) for the API reference for streaming SSR. For non-SSR pages, all Suspense boundaries will still be [statically optimized](/docs/advanced-features/automatic-static-optimization.md).
## Streaming Features ## Streaming Features
......
---
description: Streaming related APIs to build Next.js apps in streaming SSR or with React Server Components.
---
# next/streaming
The experimental `next/streaming` module provides streaming related APIs to port the existing functionality of Next.js apps to streaming scenarios and facilitate the usage of React Server Components.
## unstable_useRefreshRoot
Since Server Components are rendered on the server-side, in some cases you might need to partially refresh content from the server.
For example, a search bar (client component) which displays search results as server components. You'd want to update the search results while typing and rerender the results list with a certain frequency (e.g. with each keystroke or on a debounce).
The `unstable_useRefreshRoot` hook returns a `refresh` API to let you re-render the React tree smoothly without flickering. This is only allowed for use on the client-side and will only affect Server Components at the moment.
```jsx
// pages/index.server.js
import Search from '../components/search.client.js'
import SearchResults from '../components/search-results.server.js'
function Home() {
return (
<div>
<Search />
<SearchResults />
</div>
)
}
```
```jsx
// components/search.client.js
import { unstable_useRefreshRoot as useRefreshRoot } from 'next/streaming'
export default function Search() {
const refresh = useRefreshRoot()
return (
<SearchUI
onChange={() => {
refresh()
// Or refresh with updated props:
// refresh(nextProps)
}}
/>
)
}
```
...@@ -414,8 +414,10 @@ ...@@ -414,8 +414,10 @@
"path": "/docs/api-reference/next/server.md" "path": "/docs/api-reference/next/server.md"
}, },
{ {
"title": "next/streaming", "path": "/docs/api-reference/next/streaming",
"path": "/docs/api-reference/next/streaming.md" "redirect": {
"destination": "/docs/advanced-features/react-18"
}
}, },
{ {
"title": "next/future/image (experimental)", "title": "next/future/image (experimental)",
......
/* global location */ /* global location */
import '../build/polyfills/polyfill-module' import '../build/polyfills/polyfill-module'
import React, { useState } from 'react' import React from 'react'
import { HeadManagerContext } from '../shared/lib/head-manager-context' import { HeadManagerContext } from '../shared/lib/head-manager-context'
import mitt, { MittEmitter } from '../shared/lib/mitt' import mitt, { MittEmitter } from '../shared/lib/mitt'
import { RouterContext } from '../shared/lib/router-context' import { RouterContext } from '../shared/lib/router-context'
...@@ -678,8 +678,6 @@ if (process.env.__NEXT_RSC) { ...@@ -678,8 +678,6 @@ if (process.env.__NEXT_RSC) {
createFromFetch, createFromFetch,
createFromReadableStream, createFromReadableStream,
} = require('next/dist/compiled/react-server-dom-webpack') } = require('next/dist/compiled/react-server-dom-webpack')
const { RefreshContext } = require('./streaming/refresh')
const encoder = new TextEncoder() const encoder = new TextEncoder()
let initialServerDataBuffer: string[] | undefined = undefined let initialServerDataBuffer: string[] | undefined = undefined
...@@ -808,28 +806,7 @@ if (process.env.__NEXT_RSC) { ...@@ -808,28 +806,7 @@ if (process.env.__NEXT_RSC) {
RSCComponent = (props: any) => { RSCComponent = (props: any) => {
const cacheKey = getCacheKey() const cacheKey = getCacheKey()
const { __flight__ } = props const { __flight__ } = props
const [, dispatch] = useState({}) return <ServerRoot cacheKey={cacheKey} serialized={__flight__} />
const startTransition = (React as any).startTransition
const rerender = () => dispatch({})
// If there is no cache, or there is serialized data already
function refreshCache(nextProps?: any) {
startTransition(() => {
const currentCacheKey = getCacheKey()
const response = createFromFetch(
fetchFlight(currentCacheKey, nextProps)
)
rscCache.set(currentCacheKey, response)
rerender()
})
}
return (
<RefreshContext.Provider value={refreshCache}>
<ServerRoot cacheKey={cacheKey} serialized={__flight__} />
</RefreshContext.Provider>
)
} }
} }
......
export { useRefreshRoot as unstable_useRefreshRoot } from './refresh'
import { createContext, useContext } from 'react'
export const RefreshContext = createContext((_props?: any) => {})
export function useRefreshRoot() {
return useContext(RefreshContext)
}
...@@ -45,8 +45,6 @@ ...@@ -45,8 +45,6 @@
"jest.d.ts", "jest.d.ts",
"amp.js", "amp.js",
"amp.d.ts", "amp.d.ts",
"streaming.js",
"streaming.d.ts",
"index.d.ts", "index.d.ts",
"types/index.d.ts", "types/index.d.ts",
"types/global.d.ts", "types/global.d.ts",
......
export * from './dist/client/streaming'
module.exports = require('./dist/client/streaming')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment