Unverified Commit 55b26f96 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[PPF] Scaffold unstable_prefetch() (#97618)

Scaffolds a new API, `unstable_prefetch()`. It's only allowed in server
code, so we ban it from being imported in the client.
Implementation follows upstack. Intended to be merged together.
parent 18766e90
......@@ -726,6 +726,7 @@ impl ReactServerComponentValidator {
"cacheTag",
"unstable_cacheTag",
"unstable_navigation",
"unstable_prefetch",
// "unstable_noStore" // no-op in client, but allowed for legacy reasons
],
),
......
import { unstable_prefetch } from 'next/cache'
export async function test() {
await unstable_prefetch()
return null
}
import { unstable_prefetch } from 'next/cache';
export async function test() {
await unstable_prefetch();
return null;
}
x You're importing a module that depends on "unstable_prefetch" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use
| client", so this module is also a Client Component.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering
|
|
,-[input.js:1:1]
1 | import { unstable_prefetch } from 'next/cache'
: ^^^^^^^^^^^^^^^^^
`----
import { unstable_prefetch } from 'next/cache'
export async function test() {
await unstable_prefetch()
return null
}
import { unstable_prefetch } from 'next/cache';
export async function test() {
await unstable_prefetch();
return null;
}
x You're importing a module that depends on "unstable_prefetch". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
|
,-[input.js:1:1]
1 | import { unstable_prefetch } from 'next/cache'
: ^^^^^^^^^^^^^^^^^
`----
......@@ -156,3 +156,4 @@ export const unstable_cacheLife: typeof cacheLife
export const unstable_cacheTag: typeof cacheTag
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
......@@ -26,6 +26,7 @@ if (process.env.NEXT_RUNTIME === '') {
cacheLife: notAvailableInClient('cacheLife'),
cacheTag: notAvailableInClient('cacheTag'),
unstable_navigation: notAvailableInClient('unstable_navigation'),
unstable_prefetch: notAvailableInClient('unstable_prefetch'),
}
} else {
// Keep server requires in this branch so browser builds can DCE them.
......@@ -52,6 +53,8 @@ if (process.env.NEXT_RUNTIME === '') {
cacheTag: require('next/dist/server/use-cache/cache-tag').cacheTag,
unstable_navigation: require('next/dist/server/request/cache-stages')
.unstable_navigation,
unstable_prefetch: require('next/dist/server/request/cache-stages')
.unstable_prefetch,
}
}
......@@ -99,3 +102,4 @@ exports.unstable_cacheTag = cacheExports.unstable_cacheTag
exports.refresh = cacheExports.refresh
exports.io = cacheExports.io
exports.unstable_navigation = cacheExports.unstable_navigation
exports.unstable_prefetch = cacheExports.unstable_prefetch
......@@ -1482,5 +1482,6 @@
"1481": "Route %s used \\`unstable_navigation()\\`, which requires Cache Components to be enabled. Learn more: https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheComponents",
"1482": "Route %s used \\`unstable_navigation()\\` inside \\`after()\\` while rendering. The \\`unstable_navigation()\\` function is used to indicate the subsequent code must only run during an actual navigation, but \\`after()\\` executes after the request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/app/api-reference/functions/after",
"1483": "Route %s used \\`unstable_navigation()\\` inside \"use cache: private\". This is not currently supported. Instead, move the \"use cache\" directive to a function that's called below \\`await unstable_navigation()\\`, so that the cached content is deferred to the navigation without caching the stage boundary itself. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"1484": "Route %s used \\`unstable_navigation()\\` inside \"use cache\". This is not currently supported. Instead, move the \"use cache\" directive to a function that's called below \\`await unstable_navigation()\\`, so that the cached content is deferred to the navigation without caching the stage boundary itself. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache"
"1484": "Route %s used \\`unstable_navigation()\\` inside \"use cache\". This is not currently supported. Instead, move the \"use cache\" directive to a function that's called below \\`await unstable_navigation()\\`, so that the cached content is deferred to the navigation without caching the stage boundary itself. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"1485": "\"unstable_prefetch() is not implemented yet.\""
}
......@@ -31,6 +31,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......@@ -190,6 +191,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......@@ -269,6 +271,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......@@ -347,6 +350,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......@@ -426,6 +430,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......@@ -509,6 +514,7 @@ describe('cache-life-type-utils', () => {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
/**
......
......@@ -179,6 +179,7 @@ declare module 'next/cache' {
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
${overloads}
......
......@@ -11,6 +11,22 @@ import { isRequestApiAllowedInCurrentPhase } from './utils'
import { InvariantError } from '../../shared/lib/invariant-error'
import { RenderStage } from '../app-render/staged-rendering'
/**
* When `partialPrefetching` is enabled, this function allows you to indicate
* that the subsequent code should be excluded from the shell. It will be deferred until
* a prefetch (i.e. when using `<Link prefetch={true}>`) or a navigation.
*
* It has no effect during static prerendering — static output is computed
* once and shared across many clients, so there's no per-request cost to
* save — and no effect on the initial load of a page.
*
* Unlike `connection()`, it does not mark the subtree as request-dependent —
* content below `await unstable_prefetch()` remains fully cacheable.
*/
export function unstable_prefetch(): Promise<void> {
throw new Error('"unstable_prefetch() is not implemented yet."')
}
/**
* This function allows you to indicate that the subsequent code should be
* deferred to the actual navigation instead of rendering during a runtime
......
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