Unverified Commit be38f61c authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Use throw to trigger Suspense to avoid use() in reducer warning (#40950)

This is a temporarily fix as discussed with @sebmarkbage. It will be superseded by async reducer functions once that's added.




## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 7e8eb288
...@@ -7,10 +7,43 @@ import type { ...@@ -7,10 +7,43 @@ import type {
Segment, Segment,
} from '../../server/app-render' } from '../../server/app-render'
// TODO-APP: change to React.use once it becomes stable // 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'
/**
* Create data fetching record for Promise.
*/
// TODO-APP: change `any` to type inference.
function createRecordFromThenable(thenable: any) {
thenable.status = 'pending'
thenable.then(
(value: any) => {
if (thenable.status === 'pending') {
thenable.status = 'fulfilled'
thenable.value = value
}
},
(err: any) => {
if (thenable.status === 'pending') {
thenable.status = 'rejected'
thenable.value = err
}
}
)
return thenable
}
/**
* Read record value or throw Promise if it's not resolved yet.
*/
function readRecordValue(thenable: any) {
if (thenable.status === 'fulfilled') {
return thenable.value
} else {
throw thenable
}
}
function createHrefFromUrl(url: URL): string { function createHrefFromUrl(url: URL): string {
return url.pathname + url.search + url.hash return url.pathname + url.search + url.hash
} }
...@@ -781,11 +814,13 @@ function clientReducer( ...@@ -781,11 +814,13 @@ function clientReducer(
// If no in-flight fetch at the top, start it. // If no in-flight fetch at the top, start it.
if (!cache.data) { if (!cache.data) {
cache.data = fetchServerResponse(url, state.tree) cache.data = createRecordFromThenable(
fetchServerResponse(url, state.tree)
)
} }
// 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, canonicalUrlOverride] = use(cache.data) const [flightData, canonicalUrlOverride] = readRecordValue(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') {
...@@ -993,14 +1028,16 @@ function clientReducer( ...@@ -993,14 +1028,16 @@ function clientReducer(
if (!cache.data) { if (!cache.data) {
// Fetch data from the root of the tree. // Fetch data from the root of the tree.
cache.data = fetchServerResponse(new URL(href, location.origin), [ cache.data = createRecordFromThenable(
state.tree[0], fetchServerResponse(new URL(href, location.origin), [
state.tree[1], state.tree[0],
state.tree[2], state.tree[1],
'refetch', state.tree[2],
]) 'refetch',
])
)
} }
const [flightData, canonicalUrlOverride] = use(cache.data) const [flightData, canonicalUrlOverride] = readRecordValue(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') {
......
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