Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c7e26193
Unverified
Commit
c7e26193
authored
Sep 19, 2022
by
Tim Neutkens
Committed by
GitHub
Sep 19, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor fetchServerResponse (#40674)
parent
356b6cec
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
39 deletions
+43
-39
app-index.tsx
packages/next/client/app-index.tsx
+5
-7
app-router.client.tsx
packages/next/client/components/app-router.client.tsx
+6
-5
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+8
-5
reducer.ts
packages/next/client/components/reducer.ts
+6
-8
app-render.tsx
packages/next/server/app-render.tsx
+15
-14
index.d.ts
packages/next/types/index.d.ts
+3
-0
No files found.
packages/next/client/app-index.tsx
View file @
c7e26193
...
@@ -2,7 +2,8 @@
...
@@ -2,7 +2,8 @@
import
'../build/polyfills/polyfill-module'
import
'../build/polyfills/polyfill-module'
// @ts-ignore react-dom/client exists when using React 18
// @ts-ignore react-dom/client exists when using React 18
import
ReactDOMClient
from
'react-dom/client'
import
ReactDOMClient
from
'react-dom/client'
import
React
from
'react'
// TODO-APP: change to React.use once it becomes stable
import
React
,
{
experimental_use
as
use
}
from
'react'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
import
measureWebVitals
from
'./performance-relayer'
import
measureWebVitals
from
'./performance-relayer'
...
@@ -16,9 +17,6 @@ declare global {
...
@@ -16,9 +17,6 @@ declare global {
const
__webpack_require__
:
any
const
__webpack_require__
:
any
}
}
// TODO-APP: change to React.use once it becomes stable
const
use
=
(
React
as
any
).
experimental_use
// eslint-disable-next-line no-undef
// eslint-disable-next-line no-undef
const
getChunkScriptFilename
=
__webpack_require__
.
u
const
getChunkScriptFilename
=
__webpack_require__
.
u
const
chunkFilenameMap
:
any
=
{}
const
chunkFilenameMap
:
any
=
{}
...
@@ -127,7 +125,7 @@ function createResponseCache() {
...
@@ -127,7 +125,7 @@ function createResponseCache() {
}
}
const
rscCache
=
createResponseCache
()
const
rscCache
=
createResponseCache
()
function
useInitialServerResponse
(
cacheKey
:
string
)
{
function
useInitialServerResponse
(
cacheKey
:
string
)
:
Promise
<
JSX
.
Element
>
{
const
response
=
rscCache
.
get
(
cacheKey
)
const
response
=
rscCache
.
get
(
cacheKey
)
if
(
response
)
return
response
if
(
response
)
return
response
...
@@ -143,7 +141,7 @@ function useInitialServerResponse(cacheKey: string) {
...
@@ -143,7 +141,7 @@ function useInitialServerResponse(cacheKey: string) {
return
newResponse
return
newResponse
}
}
function
ServerRoot
({
cacheKey
}:
{
cacheKey
:
string
})
{
function
ServerRoot
({
cacheKey
}:
{
cacheKey
:
string
})
:
JSX
.
Element
{
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
rscCache
.
delete
(
cacheKey
)
rscCache
.
delete
(
cacheKey
)
})
})
...
@@ -171,7 +169,7 @@ function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
...
@@ -171,7 +169,7 @@ function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
return
children
as
React
.
ReactElement
return
children
as
React
.
ReactElement
}
}
function
RSCComponent
(
props
:
any
)
{
function
RSCComponent
(
props
:
any
)
:
JSX
.
Element
{
const
cacheKey
=
getCacheKey
()
const
cacheKey
=
getCacheKey
()
return
<
ServerRoot
{
...
props
}
cacheKey=
{
cacheKey
}
/>
return
<
ServerRoot
{
...
props
}
cacheKey=
{
cacheKey
}
/>
}
}
...
...
packages/next/client/components/app-router.client.tsx
View file @
c7e26193
...
@@ -32,11 +32,11 @@ import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'
...
@@ -32,11 +32,11 @@ import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'
/**
/**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
*/
*/
export
function
fetchServerResponse
(
export
async
function
fetchServerResponse
(
url
:
URL
,
url
:
URL
,
flightRouterState
:
FlightRouterState
,
flightRouterState
:
FlightRouterState
,
prefetch
?:
true
prefetch
?:
true
):
Promise
<
FlightData
>
{
):
Promise
<
[
FlightData
:
FlightData
]
>
{
const
flightUrl
=
new
URL
(
url
)
const
flightUrl
=
new
URL
(
url
)
const
searchParams
=
flightUrl
.
searchParams
const
searchParams
=
flightUrl
.
searchParams
// Enable flight response
// Enable flight response
...
@@ -50,9 +50,10 @@ export function fetchServerResponse(
...
@@ -50,9 +50,10 @@ export function fetchServerResponse(
searchParams
.
append
(
'__flight_prefetch__'
,
'1'
)
searchParams
.
append
(
'__flight_prefetch__'
,
'1'
)
}
}
const
promise
=
fetch
(
flightUrl
.
toString
())
const
res
=
await
fetch
(
flightUrl
.
toString
())
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
return
createFromFetch
(
promise
)
const
flightData
:
FlightData
=
await
createFromFetch
(
Promise
.
resolve
(
res
))
return
[
flightData
]
}
}
/**
/**
...
@@ -197,7 +198,7 @@ export default function AppRouter({
...
@@ -197,7 +198,7 @@ export default function AppRouter({
window
.
history
.
state
?.
tree
||
initialTree
,
window
.
history
.
state
?.
tree
||
initialTree
,
true
true
)
)
const
flightData
=
await
r
const
[
flightData
]
=
await
r
// @ts-ignore startTransition exists
// @ts-ignore startTransition exists
React
.
startTransition
(()
=>
{
React
.
startTransition
(()
=>
{
dispatch
({
dispatch
({
...
...
packages/next/client/components/layout-router.client.tsx
View file @
c7e26193
'client'
'client'
import
React
,
{
useContext
,
useEffect
,
useRef
}
from
'react'
import
React
,
{
useContext
,
useEffect
,
useRef
,
// TODO-APP: change to React.use once it becomes stable
experimental_use
as
use
,
}
from
'react'
import
type
{
import
type
{
ChildProp
,
ChildProp
,
//Segment
//Segment
...
@@ -19,9 +25,6 @@ import {
...
@@ -19,9 +25,6 @@ import {
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
// import { matchSegment } from './match-segments'
// import { matchSegment } from './match-segments'
// TODO-APP: change to React.use once it becomes stable
const
use
=
(
React
as
any
).
experimental_use
/**
/**
* Check if every segment in array a and b matches
* Check if every segment in array a and b matches
*/
*/
...
@@ -230,7 +233,7 @@ export function InnerLayoutRouter({
...
@@ -230,7 +233,7 @@ export function InnerLayoutRouter({
* Flight response data
* Flight response data
*/
*/
// When the data has not resolved yet `use` will suspend here.
// When the data has not resolved yet `use` will suspend here.
const
flightData
=
use
(
childNode
.
data
)
const
[
flightData
]
=
use
(
childNode
.
data
)
// Handle case when navigating to page in `pages` from `app`
// Handle case when navigating to page in `pages` from `app`
if
(
typeof
flightData
===
'string'
)
{
if
(
typeof
flightData
===
'string'
)
{
...
...
packages/next/client/components/reducer.ts
View file @
c7e26193
...
@@ -6,13 +6,11 @@ import type {
...
@@ -6,13 +6,11 @@ import type {
FlightSegmentPath
,
FlightSegmentPath
,
Segment
,
Segment
,
}
from
'../../server/app-render'
}
from
'../../server/app-render'
import
React
from
'react'
// TODO-APP: change to React.use once it becomes stable
import
{
experimental_use
as
use
}
from
'react'
import
{
matchSegment
}
from
'./match-segments'
import
{
matchSegment
}
from
'./match-segments'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
// TODO-APP: change to React.use once it becomes stable
const
use
=
(
React
as
any
).
experimental_use
/**
/**
* Invalidate cache one level down from the router state.
* Invalidate cache one level down from the router state.
*/
*/
...
@@ -233,7 +231,7 @@ function fillCacheWithDataProperty(
...
@@ -233,7 +231,7 @@ function fillCacheWithDataProperty(
newCache
:
CacheNode
,
newCache
:
CacheNode
,
existingCache
:
CacheNode
,
existingCache
:
CacheNode
,
segments
:
string
[],
segments
:
string
[],
fetchResponse
:
any
fetchResponse
:
()
=>
ReturnType
<
typeof
fetchServerResponse
>
):
{
bailOptimistic
:
boolean
}
|
undefined
{
):
{
bailOptimistic
:
boolean
}
|
undefined
{
const
isLastEntry
=
segments
.
length
===
1
const
isLastEntry
=
segments
.
length
===
1
...
@@ -734,7 +732,7 @@ export function reducer(
...
@@ -734,7 +732,7 @@ export function reducer(
cache
,
cache
,
state
.
cache
,
state
.
cache
,
segments
.
slice
(
1
),
segments
.
slice
(
1
),
()
:
Promise
<
FlightData
>
=>
fetchServerResponse
(
url
,
optimisticTree
)
()
=>
fetchServerResponse
(
url
,
optimisticTree
)
)
)
// If optimistic fetch couldn't happen it falls back to the non-optimistic case.
// If optimistic fetch couldn't happen it falls back to the non-optimistic case.
...
@@ -765,7 +763,7 @@ export function reducer(
...
@@ -765,7 +763,7 @@ export function reducer(
}
}
// Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves.
// Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves.
const
flightData
=
use
(
cache
.
data
)
const
[
flightData
]
=
use
(
cache
.
data
)
// Handle case when navigating to page in `pages` from `app`
// Handle case when navigating to page in `pages` from `app`
if
(
typeof
flightData
===
'string'
)
{
if
(
typeof
flightData
===
'string'
)
{
...
@@ -956,7 +954,7 @@ export function reducer(
...
@@ -956,7 +954,7 @@ export function reducer(
'refetch'
,
'refetch'
,
])
])
}
}
const
flightData
=
use
(
cache
.
data
)
const
[
flightData
]
=
use
(
cache
.
data
)
// Handle case when navigating to page in `pages` from `app`
// Handle case when navigating to page in `pages` from `app`
if
(
typeof
flightData
===
'string'
)
{
if
(
typeof
flightData
===
'string'
)
{
...
...
packages/next/server/app-render.tsx
View file @
c7e26193
...
@@ -2,7 +2,8 @@ import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
...
@@ -2,7 +2,8 @@ import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
import
type
{
LoadComponentsReturnType
}
from
'./load-components'
import
type
{
LoadComponentsReturnType
}
from
'./load-components'
import
type
{
ServerRuntime
}
from
'../types'
import
type
{
ServerRuntime
}
from
'../types'
import
React
from
'react'
// TODO-APP: change to React.use once it becomes stable
import
React
,
{
experimental_use
as
use
}
from
'react'
import
{
ParsedUrlQuery
,
stringify
as
stringifyQuery
}
from
'querystring'
import
{
ParsedUrlQuery
,
stringify
as
stringifyQuery
}
from
'querystring'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
renderToReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
import
{
renderToReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
...
@@ -29,9 +30,6 @@ import { FlushEffectsContext } from '../shared/lib/flush-effects'
...
@@ -29,9 +30,6 @@ import { FlushEffectsContext } from '../shared/lib/flush-effects'
import
{
stripInternalQueries
}
from
'./internal-utils'
import
{
stripInternalQueries
}
from
'./internal-utils'
import
type
{
ComponentsType
}
from
'../build/webpack/loaders/next-app-loader'
import
type
{
ComponentsType
}
from
'../build/webpack/loaders/next-app-loader'
// TODO-APP: change to React.use once it becomes stable
const
use
=
(
React
as
any
).
experimental_use
// this needs to be required lazily so that `next-server` can set
// this needs to be required lazily so that `next-server` can set
// the env before we require
// the env before we require
const
ReactDOMServer
=
shouldUseReactRoot
const
ReactDOMServer
=
shouldUseReactRoot
...
@@ -139,6 +137,10 @@ function preloadDataFetchingRecord(
...
@@ -139,6 +137,10 @@ function preloadDataFetchingRecord(
return
record
return
record
}
}
interface
FlightResponseRef
{
current
:
Promise
<
JSX
.
Element
>
|
null
}
/**
/**
* Render Flight stream.
* Render Flight stream.
* This is only used for renderToHTML, the Flight response does not need additional wrappers.
* This is only used for renderToHTML, the Flight response does not need additional wrappers.
...
@@ -147,19 +149,18 @@ function useFlightResponse(
...
@@ -147,19 +149,18 @@ function useFlightResponse(
writable
:
WritableStream
<
Uint8Array
>
,
writable
:
WritableStream
<
Uint8Array
>
,
req
:
ReadableStream
<
Uint8Array
>
,
req
:
ReadableStream
<
Uint8Array
>
,
serverComponentManifest
:
any
,
serverComponentManifest
:
any
,
flightResponseRef
:
{
flightResponseRef
:
FlightResponseRef
,
current
:
ReturnType
<
typeof
createFromReadableStream
>
|
null
},
nonce
?:
string
nonce
?:
string
)
{
)
:
Promise
<
JSX
.
Element
>
{
if
(
flightResponseRef
.
current
)
{
if
(
flightResponseRef
.
current
!==
null
)
{
return
flightResponseRef
.
current
return
flightResponseRef
.
current
}
}
const
[
renderStream
,
forwardStream
]
=
readableStreamTee
(
req
)
const
[
renderStream
,
forwardStream
]
=
readableStreamTee
(
req
)
flightResponseRef
.
current
=
createFromReadableStream
(
renderStream
,
{
const
res
=
createFromReadableStream
(
renderStream
,
{
moduleMap
:
serverComponentManifest
.
__ssr_module_mapping__
,
moduleMap
:
serverComponentManifest
.
__ssr_module_mapping__
,
})
})
flightResponseRef
.
current
=
res
let
bootstrapped
=
false
let
bootstrapped
=
false
// We only attach CSS chunks to the inlined data.
// We only attach CSS chunks to the inlined data.
...
@@ -197,7 +198,7 @@ function useFlightResponse(
...
@@ -197,7 +198,7 @@ function useFlightResponse(
}
}
process
()
process
()
return
flightResponseRef
.
current
return
res
}
}
/**
/**
...
@@ -224,7 +225,7 @@ function createServerComponentRenderer(
...
@@ -224,7 +225,7 @@ function createServerComponentRenderer(
>
>
},
},
nonce
?:
string
nonce
?:
string
)
{
)
:
()
=>
JSX
.
Element
{
// We need to expose the `__webpack_require__` API globally for
// We need to expose the `__webpack_require__` API globally for
// react-server-dom-webpack. This is a hack until we find a better way.
// react-server-dom-webpack. This is a hack until we find a better way.
if
(
ComponentMod
.
__next_app_webpack_require__
||
ComponentMod
.
__next_rsc__
)
{
if
(
ComponentMod
.
__next_app_webpack_require__
||
ComponentMod
.
__next_rsc__
)
{
...
@@ -251,10 +252,10 @@ function createServerComponentRenderer(
...
@@ -251,10 +252,10 @@ function createServerComponentRenderer(
return
RSCStream
return
RSCStream
}
}
const
flightResponseRef
=
{
current
:
null
}
const
flightResponseRef
:
FlightResponseRef
=
{
current
:
null
}
const
writable
=
transformStream
.
writable
const
writable
=
transformStream
.
writable
return
function
ServerComponentWrapper
()
{
return
function
ServerComponentWrapper
()
:
JSX
.
Element
{
const
reqStream
=
createRSCStream
()
const
reqStream
=
createRSCStream
()
const
response
=
useFlightResponse
(
const
response
=
useFlightResponse
(
writable
,
writable
,
...
...
packages/next/types/index.d.ts
View file @
c7e26193
...
@@ -39,6 +39,9 @@ declare module 'react' {
...
@@ -39,6 +39,9 @@ declare module 'react' {
interface
LinkHTMLAttributes
<
T
>
extends
HTMLAttributes
<
T
>
{
interface
LinkHTMLAttributes
<
T
>
extends
HTMLAttributes
<
T
>
{
nonce
?:
string
nonce
?:
string
}
}
// TODO-APP: check if this is the right type.
function
experimental_use
<
T
>
(
promise
:
Promise
<
T
>
):
Awaited
<
T
>
}
}
export
type
Redirect
=
export
type
Redirect
=
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment