Skip to content

Commit

Permalink
modify prefetch cache entry type based on redirect response
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Sep 18, 2024
1 parent 31ee0eb commit 7a98507
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
import { callServer } from '../../../app-call-server'
import {
ACTION_HEADER,
NEXT_IS_PRERENDER_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_URL,
RSC_CONTENT_TYPE_HEADER,
Expand Down Expand Up @@ -53,6 +54,7 @@ type FetchServerActionResult = {
redirectLocation: URL | undefined
actionResult?: ActionResult
actionFlightData?: NormalizedFlightData[] | string
isPrerender: boolean
revalidatedParts: {
tag: boolean
cookie: boolean
Expand Down Expand Up @@ -90,6 +92,7 @@ async function fetchServerAction(
})

const location = res.headers.get('x-action-redirect')
const isPrerender = !!res.headers.get(NEXT_IS_PRERENDER_HEADER)
let revalidatedParts: FetchServerActionResult['revalidatedParts']
try {
const revalidatedHeader = JSON.parse(
Expand Down Expand Up @@ -132,6 +135,7 @@ async function fetchServerAction(
actionFlightData: normalizeFlightData(response.f),
redirectLocation,
revalidatedParts,
isPrerender,
}
}

Expand All @@ -140,6 +144,7 @@ async function fetchServerAction(
actionFlightData: normalizeFlightData(response.f),
redirectLocation,
revalidatedParts,
isPrerender,
}
}

Expand All @@ -158,6 +163,7 @@ async function fetchServerAction(
return {
redirectLocation,
revalidatedParts,
isPrerender,
}
}

Expand Down Expand Up @@ -191,6 +197,7 @@ export function serverActionReducer(
actionResult,
actionFlightData: flightData,
redirectLocation,
isPrerender,
}) => {
// Make sure the redirection is a push instead of a replace.
// Issue: https://github.com/vercel/next.js/issues/53911
Expand Down Expand Up @@ -242,7 +249,7 @@ export function serverActionReducer(
tree: state.tree,
prefetchCache: state.prefetchCache,
nextUrl: state.nextUrl,
kind: PrefetchKind.FULL,
kind: isPrerender ? PrefetchKind.FULL : PrefetchKind.AUTO,
})

mutable.prefetchCache = state.prefetchCache
Expand Down
20 changes: 14 additions & 6 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,13 @@ describe('app-dir action handling', () => {
'redirected'
)

// no other requests should be made
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
// This verifies the redirect & server response happens in a single roundtrip,
// if the redirect resource was static. In development, these responses are always
// dynamically generated, so we only expect a single request for build/deploy.
if (!isNextDev) {
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
}

const request = requests[0]
const response = responses[0]
Expand Down Expand Up @@ -1056,9 +1060,13 @@ describe('app-dir action handling', () => {
await browser.elementById(`redirect-${redirectType}`).click()
await check(() => browser.url(), `${next.url}${destinationPagePath}`)

// no other requests should be made
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
// This verifies the redirect & server response happens in a single roundtrip,
// if the redirect resource was static. In development, these responses are always
// dynamically generated, so we only expect a single request for build/deploy.
if (!isNextDev) {
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
}

const request = requests[0]
const response = responses[0]
Expand Down
12 changes: 8 additions & 4 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { check, retry } from 'next-test-utils'
import type { Request, Response } from 'playwright'

describe('app dir - basepath', () => {
const { next } = nextTestSetup({
const { next, isNextDev } = nextTestSetup({
files: __dirname,
dependencies: {
sass: 'latest',
Expand Down Expand Up @@ -132,9 +132,13 @@ describe('app dir - basepath', () => {

expect(await browser.waitForElementByCss('#page-2').text()).toBe(`Page 2`)

// verify that the POST request was only made to the action handler
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
// This verifies the redirect & server response happens in a single roundtrip,
// if the redirect resource was static. In development, these responses are always
// dynamically generated, so we only expect a single request for build/deploy.
if (!isNextDev) {
expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1)
}

const request = requests[0]
const response = responses[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
import { check, retry } from 'next-test-utils'

describe('parallel-routes-revalidation', () => {
const { next, isNextStart, isNextDeploy } = nextTestSetup({
const { next, isNextDev, isNextStart, isNextDeploy } = nextTestSetup({
files: __dirname,
})

Expand Down Expand Up @@ -447,7 +447,9 @@ describe('parallel-routes-revalidation', () => {
await browser.waitForIdleNetwork()

await retry(async () => {
expect(rscRequests.length).toBe(0)
if (!isNextDev) {
expect(rscRequests.length).toBe(0)
}

if (isNextStart) {
expect(prefetchRequests.length).toBe(4)
Expand Down

0 comments on commit 7a98507

Please sign in to comment.