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

router: apply server actions in a similar way to router.refresh() (#53373)

## What?

I was investigating reports of server actions with `revalidatePath` /
`revalidateTag` not invalidating the client-side router cache. Managed
to reproduce the issue here:
https://github.com/timneutkens/server-actions-test (requires Vercel KV
to run).

While looking at the reducer I noticed a few things that seemed off:

- setTimeout to trigger another `dispatch` on the same reducer with the
fetched data. This means that it would not be part of the same React
Transition.
- redirects weren't applying the data that comes back from the server
(that allows for single hop navigation)
- prefetchCache was invalidated, but the router cache which is used for
back/forward navigation was not invalidated, causing back/forward to not
get the new data.
- all changes in the action-reducer mutate. The part that shouldn't
mutate was part of that setTimeout dispatch.

This PR aims to solve all of these by reworking the actions reducer to
be handled similarly to `router.refresh()`. Since `router.refresh()` was
already modeled to be similar to `revalidatePath('/')`.

The flow is now more like the other reducers:
- Fetch data
- Wait for the data to come back
- Apply the data to the cache and keep it in a mutable variable
- Return the new cache and otherwise values like the url

In particular the actions reducer handles a few extra specifics:
- Resolving the server action promise, so that the value is passed to
the application code waiting for the result.
- Applying redirects from `redirect()` calls.
- Invalidating the router cache

## Followup changes

- Currently when calling `revalidatePath('/dashboard')` the entire
router cache is invalidated instead of only `/dashboard` and further
down, this is not in scope for this PR but still has to be added.
- `notFound()`, I'm not sure how this is supposed to work exactly, it
doesn't really make sense to me to allow it in server actions.

Kudos @feedthejim for helping investigate for this PR 😌 

<!-- Thanks for opening a PR! Your contribution is much appreciated.
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 #

-->
parent 03a09ba1
...@@ -132,11 +132,7 @@ const createEmptyCacheNode = () => ({ ...@@ -132,11 +132,7 @@ const createEmptyCacheNode = () => ({
parallelRoutes: new Map(), parallelRoutes: new Map(),
}) })
function useServerActionDispatcher( function useServerActionDispatcher(dispatch: React.Dispatch<ReducerActions>) {
changeByServerResponse: RouterChangeByServerResponse,
dispatch: React.Dispatch<ReducerActions>,
navigate: RouterNavigate
) {
const serverActionDispatcher: ServerActionDispatcher = useCallback( const serverActionDispatcher: ServerActionDispatcher = useCallback(
(actionPayload) => { (actionPayload) => {
startTransition(() => { startTransition(() => {
...@@ -144,12 +140,11 @@ function useServerActionDispatcher( ...@@ -144,12 +140,11 @@ function useServerActionDispatcher(
...actionPayload, ...actionPayload,
type: ACTION_SERVER_ACTION, type: ACTION_SERVER_ACTION,
mutable: {}, mutable: {},
navigate, cache: createEmptyCacheNode(),
changeByServerResponse,
}) })
}) })
}, },
[changeByServerResponse, dispatch, navigate] [dispatch]
) )
globalServerActionDispatcher = serverActionDispatcher globalServerActionDispatcher = serverActionDispatcher
} }
...@@ -262,7 +257,7 @@ function Router({ ...@@ -262,7 +257,7 @@ function Router({
const changeByServerResponse = useChangeByServerResponse(dispatch) const changeByServerResponse = useChangeByServerResponse(dispatch)
const navigate = useNavigate(dispatch) const navigate = useNavigate(dispatch)
useServerActionDispatcher(changeByServerResponse, dispatch, navigate) useServerActionDispatcher(dispatch)
/** /**
* The app router that is exposed through `useRouter`. It's only concerned with dispatching actions to the reducer, does not hold state. * The app router that is exposed through `useRouter`. It's only concerned with dispatching actions to the reducer, does not hold state.
......
...@@ -17,14 +17,18 @@ import { createFromFetch } from 'react-server-dom-webpack/client' ...@@ -17,14 +17,18 @@ import { createFromFetch } from 'react-server-dom-webpack/client'
import { encodeReply } from 'react-server-dom-webpack/client' import { encodeReply } from 'react-server-dom-webpack/client'
import { import {
PrefetchKind,
ReadonlyReducerState, ReadonlyReducerState,
ReducerState, ReducerState,
ServerActionAction, ServerActionAction,
} from '../router-reducer-types' } from '../router-reducer-types'
import { addBasePath } from '../../../add-base-path' import { addBasePath } from '../../../add-base-path'
import { createHrefFromUrl } from '../create-href-from-url' import { createHrefFromUrl } from '../create-href-from-url'
import { RedirectType, getRedirectError } from '../../redirect' import { handleExternalUrl } from './navigate-reducer'
import { applyRouterStatePatchToTree } from '../apply-router-state-patch-to-tree'
import { isNavigatingToNewRootLayout } from '../is-navigating-to-new-root-layout'
import { CacheStates } from '../../../../shared/lib/app-router-context'
import { handleMutable } from '../handle-mutable'
import { fillLazyItemsTillLeafWithHead } from '../fill-lazy-items-till-leaf-with-head'
type FetchServerActionResult = { type FetchServerActionResult = {
redirectLocation: URL | undefined redirectLocation: URL | undefined
...@@ -98,17 +102,18 @@ async function fetchServerAction( ...@@ -98,17 +102,18 @@ async function fetchServerAction(
} }
) )
// if it was a redirection, then result is just a regular RSC payload
if (location) { if (location) {
const [, payload] = response // if it was a redirection, then result is just a regular RSC payload
const [, actionFlightData] = (response as any) ?? []
return { return {
actionFlightData: payload?.[1], actionFlightData: actionFlightData,
redirectLocation, redirectLocation,
revalidatedParts, revalidatedParts,
} }
}
// otherwise it's a tuple of [actionResult, actionFlightData] // otherwise it's a tuple of [actionResult, actionFlightData]
} else { const [actionResult, [, actionFlightData]] = (response as any) ?? []
const [actionResult, [, actionFlightData]] = response ?? []
return { return {
actionResult, actionResult,
actionFlightData, actionFlightData,
...@@ -116,7 +121,6 @@ async function fetchServerAction( ...@@ -116,7 +121,6 @@ async function fetchServerAction(
revalidatedParts, revalidatedParts,
} }
} }
}
return { return {
redirectLocation, redirectLocation,
revalidatedParts, revalidatedParts,
...@@ -131,113 +135,145 @@ export function serverActionReducer( ...@@ -131,113 +135,145 @@ export function serverActionReducer(
state: ReadonlyReducerState, state: ReadonlyReducerState,
action: ServerActionAction action: ServerActionAction
): ReducerState { ): ReducerState {
// the action could be called twice so we need to check if we already have applied it const { mutable, cache, resolve, reject } = action
if (action.mutable.serverActionApplied) { const href = state.canonicalUrl
return state
let currentTree = state.tree
const isForCurrentTree =
JSON.stringify(mutable.previousTree) === JSON.stringify(currentTree)
if (isForCurrentTree) {
return handleMutable(state, mutable)
} }
if (!action.mutable.inFlightServerAction) { if (!action.mutable.inFlightServerAction) {
action.mutable.previousTree = state.tree
action.mutable.previousUrl = state.canonicalUrl
action.mutable.inFlightServerAction = createRecordFromThenable( action.mutable.inFlightServerAction = createRecordFromThenable(
fetchServerAction(state, action) fetchServerAction(state, action)
) )
} }
// TODO-APP: Make try/catch wrap only readRecordValue so that other errors bubble up through the reducer instead.
try { try {
// suspends until the server action is resolved. // suspends until the server action is resolved.
const { const {
actionResult, actionResult,
actionFlightData, actionFlightData: flightData,
redirectLocation, redirectLocation,
revalidatedParts, // revalidatedParts,
} = readRecordValue( } = readRecordValue(
action.mutable.inFlightServerAction! action.mutable.inFlightServerAction!
) as Awaited<FetchServerActionResult> ) as Awaited<FetchServerActionResult>
// Invalidate the cache for the revalidated parts. This has to be done before the mutable.previousTree = state.tree
// cache is updated with the action's flight data again.
if (revalidatedParts.tag || revalidatedParts.cookie) { if (!flightData) {
// Invalidate everything if the tag is set. if (!mutable.actionResultResolved) {
state.prefetchCache.clear() resolve(actionResult)
} else if (revalidatedParts.paths.length > 0) { mutable.actionResultResolved = true
// Invalidate all subtrees that are below the revalidated paths, and invalidate
// all the prefetch cache.
// TODO-APP: Currently the prefetch cache doesn't have subtree information,
// so we need to invalidate the entire cache if a path was revalidated.
state.prefetchCache.clear()
} }
// If there is a redirect but no flight data we need to do a mpaNavigation.
if (redirectLocation) { if (redirectLocation) {
// the redirection might have a flight data associated with it, so we'll populate the cache with it return handleExternalUrl(
if (actionFlightData) { state,
const href = createHrefFromUrl(redirectLocation, false) mutable,
const previousCacheEntry = state.prefetchCache.get(href) redirectLocation.href,
state.prefetchCache.set(href, { state.pushRef.pendingPush
data: createRecordFromThenable( )
Promise.resolve([ }
actionFlightData, return state
// TODO-APP: verify the logic around canonical URL overrides
undefined,
])
),
kind: previousCacheEntry?.kind ?? PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null,
})
} }
// we throw the redirection in the action handler so that it is caught during render if (typeof flightData === 'string') {
action.reject( // Handle case when navigating to page in `pages` from `app`
getRedirectError(redirectLocation.toString(), RedirectType.push) return handleExternalUrl(
state,
mutable,
flightData,
state.pushRef.pendingPush
) )
} else { }
if (actionFlightData) {
const href = createHrefFromUrl( // Remove cache.data as it has been resolved at this point.
new URL(action.mutable.previousUrl!, window.location.origin), mutable.inFlightServerAction = null
false
for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
console.log('SERVER ACTION APPLY FAILED')
return state
}
// Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
[''],
currentTree,
treePatch
) )
const previousCacheEntry = state.prefetchCache.get(href)
state.prefetchCache.set( if (newTree === null) {
createHrefFromUrl( throw new Error('SEGMENT MISMATCH')
new URL(action.mutable.previousUrl!, window.location.origin),
false
),
{
data: createRecordFromThenable(
Promise.resolve([
actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides
undefined,
])
),
kind: previousCacheEntry?.kind ?? PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null,
} }
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(
state,
mutable,
href,
state.pushRef.pendingPush
) )
// this is an intentional hack around React: we want to update the tree in a new render }
setTimeout(() => {
action.changeByServerResponse( // The one before last item is the router state tree patch
action.mutable.previousTree!, const [subTreeData, head] = flightDataPath.slice(-2)
actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides // Handles case where prefetch only returns the router tree patch without rendered components.
undefined if (subTreeData !== null) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(
cache,
// Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined,
treePatch,
head
) )
}) mutable.cache = cache
mutable.prefetchCache = new Map()
} }
action.resolve(actionResult) mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
currentTree = newTree
}
if (redirectLocation) {
const newHref = createHrefFromUrl(redirectLocation, false)
mutable.canonicalUrl = newHref
}
if (!mutable.actionResultResolved) {
resolve(actionResult)
mutable.actionResultResolved = true
} }
return handleMutable(state, mutable)
} catch (e: any) { } catch (e: any) {
if (e.status === 'rejected') { if (e.status === 'rejected') {
action.reject(e.value) if (!mutable.actionResultResolved) {
} else { reject(e.value)
throw e mutable.actionResultResolved = true
}
} }
action.mutable.serverActionApplied = true // When the server action is rejected we don't update the state and instead call the reject handler of the promise.
return state return state
}
throw e
}
} }
...@@ -40,12 +40,9 @@ export interface Mutable { ...@@ -40,12 +40,9 @@ export interface Mutable {
shouldScroll?: boolean shouldScroll?: boolean
} }
export interface ServerActionMutable { export interface ServerActionMutable extends Mutable {
inFlightServerAction?: Promise<any> | null inFlightServerAction?: Promise<any> | null
serverActionApplied?: boolean actionResultResolved?: boolean
previousTree?: FlightRouterState
previousUrl?: string
prefetchCache?: AppRouterState['prefetchCache']
} }
/** /**
...@@ -70,7 +67,7 @@ export interface FastRefreshAction { ...@@ -70,7 +67,7 @@ export interface FastRefreshAction {
export type ServerActionDispatcher = ( export type ServerActionDispatcher = (
args: Omit< args: Omit<
ServerActionAction, ServerActionAction,
'type' | 'mutable' | 'navigate' | 'changeByServerResponse' 'type' | 'mutable' | 'navigate' | 'changeByServerResponse' | 'cache'
> >
) => void ) => void
...@@ -80,9 +77,8 @@ export interface ServerActionAction { ...@@ -80,9 +77,8 @@ export interface ServerActionAction {
actionArgs: any[] actionArgs: any[]
resolve: (value: any) => void resolve: (value: any) => void
reject: (reason?: any) => void reject: (reason?: any) => void
cache: CacheNode
mutable: ServerActionMutable mutable: ServerActionMutable
navigate: RouterNavigate
changeByServerResponse: RouterChangeByServerResponse
} }
/** /**
......
...@@ -206,6 +206,11 @@ async function createRedirectRenderResult( ...@@ -206,6 +206,11 @@ async function createRedirectRenderResult(
) )
} }
// Ensures that when the path was revalidated we don't return a partial response on redirects
// if (staticGenerationStore.pathWasRevalidated) {
forwardedHeaders.delete('next-router-state-tree')
// }
try { try {
const headResponse = await fetchIPv4v6(fetchUrl, { const headResponse = await fetchIPv4v6(fetchUrl, {
method: 'HEAD', method: 'HEAD',
......
...@@ -104,10 +104,10 @@ export type ActionResult = Promise<any> ...@@ -104,10 +104,10 @@ export type ActionResult = Promise<any>
export type NextFlightResponse = [buildId: string, flightData: FlightData] export type NextFlightResponse = [buildId: string, flightData: FlightData]
// Response from `createFromFetch` for server actions. Action's flight data can be null // Response from `createFromFetch` for server actions. Action's flight data can be null
export type ActionFlightResponse = [ export type ActionFlightResponse =
ActionResult, | [ActionResult, [buildId: string, flightData: FlightData | null]]
[buildId: NextFlightResponse[0], flightData: NextFlightResponse[1] | null] // This case happens when `redirect()` is called in a server action.
] | NextFlightResponse
/** /**
* Property holding the current subTreeData. * Property holding the current subTreeData.
......
...@@ -374,7 +374,7 @@ createNextDescribe( ...@@ -374,7 +374,7 @@ createNextDescribe(
await check(async () => { await check(async () => {
return browser.eval('window.location.toString()') return browser.eval('window.location.toString()')
}, 'https://example.com/') }, 'https://next-data-api-endpoint.vercel.app/api/random?page')
}) })
it('should allow cookie and header async storages', async () => { it('should allow cookie and header async storages', async () => {
...@@ -422,7 +422,7 @@ createNextDescribe( ...@@ -422,7 +422,7 @@ createNextDescribe(
await check(async () => { await check(async () => {
return browser.eval('window.location.toString()') return browser.eval('window.location.toString()')
}, 'https://example.com/') }, 'https://next-data-api-endpoint.vercel.app/api/random?page')
}) })
// TODO: investigate flakey behavior with revalidate // TODO: investigate flakey behavior with revalidate
......
...@@ -47,7 +47,11 @@ export default function Counter() { ...@@ -47,7 +47,11 @@ export default function Counter() {
<form> <form>
<button <button
id="redirect-external" id="redirect-external"
formAction={() => redirectAction('https://example.com')} formAction={() =>
redirectAction(
'https://next-data-api-endpoint.vercel.app/api/random?page'
)
}
> >
redirect external redirect external
</button> </button>
......
...@@ -49,7 +49,11 @@ export default function Counter() { ...@@ -49,7 +49,11 @@ export default function Counter() {
<form> <form>
<button <button
id="redirect-external" id="redirect-external"
formAction={() => redirectAction('https://example.com')} formAction={() =>
redirectAction(
'https://next-data-api-endpoint.vercel.app/api/random?page'
)
}
> >
redirect external redirect external
</button> </button>
......
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