Unverified Commit 149f18e8 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

enable experimental.routerBfCache behind cacheComponents (#84923)

This removes the `routerBfCache` and enables it automatically when
`cacheComponents` is enabled. This leverages `React.Activity` on layout
segments to support restoring previously visited UI segments while
preserving state.
parent 683a318d
...@@ -856,8 +856,6 @@ pub struct ExperimentalConfig { ...@@ -856,8 +856,6 @@ pub struct ExperimentalConfig {
/// directory. /// directory.
ppr: Option<ExperimentalPartialPrerendering>, ppr: Option<ExperimentalPartialPrerendering>,
taint: Option<bool>, taint: Option<bool>,
#[serde(rename = "routerBFCache")]
router_bfcache: Option<bool>,
proxy_timeout: Option<f64>, proxy_timeout: Option<f64>,
/// enables the minification of server code. /// enables the minification of server code.
server_minification: Option<bool>, server_minification: Option<bool>,
......
...@@ -205,9 +205,6 @@ export function getDefineEnv({ ...@@ -205,9 +205,6 @@ export function getDefineEnv({
'process.env.__NEXT_DYNAMIC_ON_HOVER': Boolean( 'process.env.__NEXT_DYNAMIC_ON_HOVER': Boolean(
config.experimental.dynamicOnHover config.experimental.dynamicOnHover
), ),
'process.env.__NEXT_ROUTER_BF_CACHE': Boolean(
config.experimental.routerBFCache
),
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': 'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE':
config.experimental.optimisticClientCache ?? true, config.experimental.optimisticClientCache ?? true,
'process.env.__NEXT_MIDDLEWARE_PREFETCH': 'process.env.__NEXT_MIDDLEWARE_PREFETCH':
......
...@@ -2,7 +2,7 @@ import type { FlightRouterState } from '../../shared/lib/app-router-types' ...@@ -2,7 +2,7 @@ import type { FlightRouterState } from '../../shared/lib/app-router-types'
import { useState } from 'react' import { useState } from 'react'
// When the flag is disabled, only track the currently active tree // When the flag is disabled, only track the currently active tree
const MAX_BF_CACHE_ENTRIES = process.env.__NEXT_ROUTER_BF_CACHE ? 3 : 1 const MAX_BF_CACHE_ENTRIES = process.env.__NEXT_CACHE_COMPONENTS ? 3 : 1
export type RouterBFCacheEntry = { export type RouterBFCacheEntry = {
tree: FlightRouterState tree: FlightRouterState
......
...@@ -757,7 +757,7 @@ export default function OuterLayoutRouter({ ...@@ -757,7 +757,7 @@ export default function OuterLayoutRouter({
) )
} }
if (process.env.__NEXT_ROUTER_BF_CACHE) { if (process.env.__NEXT_CACHE_COMPONENTS) {
const boundaryName = getBoundaryNameForSuspenseDevTools(tree) const boundaryName = getBoundaryNameForSuspenseDevTools(tree)
child = ( child = (
<Activity <Activity
......
...@@ -423,6 +423,11 @@ export function serverActionReducer( ...@@ -423,6 +423,11 @@ export function serverActionReducer(
redirectType || RedirectType.push redirectType || RedirectType.push
) )
) )
// TODO: Investigate why this is needed with Activity.
if (process.env.__NEXT_CACHE_COMPONENTS) {
return state
}
} else { } else {
resolve(actionResult) resolve(actionResult)
} }
......
...@@ -249,7 +249,6 @@ export const experimentalSchema = { ...@@ -249,7 +249,6 @@ export const experimentalSchema = {
rootParams: z.boolean().optional(), rootParams: z.boolean().optional(),
isolatedDevBuild: z.boolean().optional(), isolatedDevBuild: z.boolean().optional(),
mcpServer: z.boolean().optional(), mcpServer: z.boolean().optional(),
routerBFCache: z.boolean().optional(),
removeUncaughtErrorAndRejectionListeners: z.boolean().optional(), removeUncaughtErrorAndRejectionListeners: z.boolean().optional(),
validateRSCRequestHeaders: z.boolean().optional(), validateRSCRequestHeaders: z.boolean().optional(),
scrollRestoration: z.boolean().optional(), scrollRestoration: z.boolean().optional(),
......
...@@ -591,11 +591,6 @@ export interface ExperimentalConfig { ...@@ -591,11 +591,6 @@ export interface ExperimentalConfig {
*/ */
taint?: boolean taint?: boolean
/**
* Enables the Back/Forward Cache for the router.
*/
routerBFCache?: boolean
/** /**
* Uninstalls all "unhandledRejection" and "uncaughtException" listeners from * Uninstalls all "unhandledRejection" and "uncaughtException" listeners from
* the global process so that we can override the behavior, which in some * the global process so that we can override the behavior, which in some
...@@ -1517,7 +1512,6 @@ export const defaultConfig = Object.freeze({ ...@@ -1517,7 +1512,6 @@ export const defaultConfig = Object.freeze({
webpackMemoryOptimizations: false, webpackMemoryOptimizations: false,
optimizeServerReact: true, optimizeServerReact: true,
viewTransition: false, viewTransition: false,
routerBFCache: false,
removeUncaughtErrorAndRejectionListeners: false, removeUncaughtErrorAndRejectionListeners: false,
validateRSCRequestHeaders: !!( validateRSCRequestHeaders: !!(
process.env.__NEXT_TEST_MODE || !isStableBuild() process.env.__NEXT_TEST_MODE || !isStableBuild()
......
...@@ -140,7 +140,8 @@ describe('app dir - basepath', () => { ...@@ -140,7 +140,8 @@ describe('app dir - basepath', () => {
// This verifies the redirect & server response happens in a single roundtrip, // This verifies the redirect & server response happens in a single roundtrip,
// if the redirect resource was static. In development, these responses are always // if the redirect resource was static. In development, these responses are always
// dynamically generated, so we only expect a single request for build/deploy. // dynamically generated, so we only expect a single request for build/deploy.
if (!isNextDev) { // TODO(client-segment-cache): re-enable when this optimization is added back
if (!isNextDev && !process.env.__NEXT_CACHE_COMPONENTS) {
expect(requests).toHaveLength(1) expect(requests).toHaveLength(1)
expect(responses).toHaveLength(1) expect(responses).toHaveLength(1)
} }
......
...@@ -18,3 +18,7 @@ export default async function Page({ ...@@ -18,3 +18,7 @@ export default async function Page({
</> </>
) )
} }
export function generateStaticParams() {
return [{ n: '1' }, { n: '2' }]
}
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { cacheComponents: true,
routerBFCache: true,
},
} }
module.exports = nextConfig module.exports = nextConfig
...@@ -34,8 +34,11 @@ describe('parallel-routes-catchall-css', () => { ...@@ -34,8 +34,11 @@ describe('parallel-routes-catchall-css', () => {
// the slot's background color should be red // the slot's background color should be red
expect(await getSlotBackgroundColor(browser)).toBe('rgb(255, 0, 0)') expect(await getSlotBackgroundColor(browser)).toBe('rgb(255, 0, 0)')
// there should no longer be a main element // the main element should either not exist or not be visible
expect(await browser.hasElementByCssSelector('#main')).toBeFalsy() const mainDisplay = await browser.eval(
`document.querySelector('#main') ? window.getComputedStyle(document.querySelector('#main')).display : null`
)
expect(mainDisplay === null || mainDisplay === 'none').toBe(true)
// the slot background should still be red on a fresh load // the slot background should still be red on a fresh load
await browser.refresh() await browser.refresh()
......
...@@ -5,7 +5,9 @@ export default function RootLayout({ ...@@ -5,7 +5,9 @@ export default function RootLayout({
}) { }) {
return ( return (
<html lang="en"> <html lang="en">
<body>{children}</body> <body>
<div>{children}</div>
</body>
</html> </html>
) )
} }
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils'
import type * as Playwright from 'playwright' import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act' import { createRouterAct } from 'router-act'
...@@ -116,14 +117,6 @@ describe('runtime prefetching', () => { ...@@ -116,14 +117,6 @@ describe('runtime prefetching', () => {
await browser.back() await browser.back()
// Reveal the link to the second page again. It should not be prefetched again
await act(async () => {
const linkToggle = await browser.elementByCss(
`input[data-link-accordion="/${prefix}/dynamic-params/456"]`
)
await linkToggle.click()
}, 'no-requests')
// Navigate to the other page // Navigate to the other page
await act(async () => { await act(async () => {
await act( await act(
...@@ -233,14 +226,6 @@ describe('runtime prefetching', () => { ...@@ -233,14 +226,6 @@ describe('runtime prefetching', () => {
if (!isNextDeploy) { if (!isNextDeploy) {
await browser.back() await browser.back()
// Reveal the link to the second page again. It should not be prefetched again
await act(async () => {
const linkToggle = await browser.elementByCss(
`input[data-link-accordion="/with-root-param/de/${prefix}/root-params"]`
)
await linkToggle.click()
}, 'no-requests')
// Navigate to the other page // Navigate to the other page
await act(async () => { await act(async () => {
await act( await act(
...@@ -348,14 +333,6 @@ describe('runtime prefetching', () => { ...@@ -348,14 +333,6 @@ describe('runtime prefetching', () => {
await browser.back() await browser.back()
// Reveal the link to the second page again. It should not be prefetched again
await act(async () => {
const linkToggle = await browser.elementByCss(
`input[data-link-accordion="/${prefix}/search-params?searchParam=456"]`
)
await linkToggle.click()
}, 'no-requests')
// Navigate to the other page // Navigate to the other page
await act( await act(
async () => { async () => {
...@@ -495,23 +472,8 @@ describe('runtime prefetching', () => { ...@@ -495,23 +472,8 @@ describe('runtime prefetching', () => {
// Go back to the previous page // Go back to the previous page
await browser.back() await browser.back()
// Reveal the link again to trigger a runtime prefetch for the new value of the cookie // wait a tick before navigating
await act(async () => { await waitFor(500)
const linkToggle = await browser.elementByCss(
`input[data-link-accordion="/${prefix}/cookies"]`
)
await linkToggle.click()
}, [
// Should allow reading dynamic params
{
includes: 'Cookie: updatedValue',
},
// Should not prefetch the dynamic content
{
includes: 'Dynamic content',
block: 'reject',
},
])
// Navigate to the page // Navigate to the page
await act(async () => { await act(async () => {
......
...@@ -187,10 +187,6 @@ describe('segment cache (staleness)', () => { ...@@ -187,10 +187,6 @@ describe('segment cache (staleness)', () => {
await page.clock.setFixedTime(startDate + 29 * 1000) await page.clock.setFixedTime(startDate + 29 * 1000)
await act(async () => { await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/dynamic"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/dynamic"]') const link = await browser.elementByCss('a[href="/dynamic"]')
await link.click() await link.click()
// The next page is immediately rendered // The next page is immediately rendered
...@@ -207,10 +203,6 @@ describe('segment cache (staleness)', () => { ...@@ -207,10 +203,6 @@ describe('segment cache (staleness)', () => {
await act( await act(
async () => { async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/dynamic"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/dynamic"]') const link = await browser.elementByCss('a[href="/dynamic"]')
await link.click() await link.click()
}, },
......
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