Skip to content

Commit 704d54d

Browse files
authored
Merge branch 'canary' into styfle/next-1688-mark-onloadingcomplete-as-deprecated
2 parents f71c170 + 451a54c commit 704d54d

File tree

12 files changed

+129
-0
lines changed

12 files changed

+129
-0
lines changed

packages/next/cache.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
22
export { revalidatePath } from 'next/dist/server/web/spec-extension/revalidate-path'
33
export { revalidateTag } from 'next/dist/server/web/spec-extension/revalidate-tag'
4+
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'

packages/next/cache.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const cacheExports = {
55
.revalidateTag,
66
revalidatePath: require('next/dist/server/web/spec-extension/revalidate-path')
77
.revalidatePath,
8+
unstable_noStore:
9+
require('next/dist/server/web/spec-extension/unstable-no-store')
10+
.unstable_noStore,
811
}
912

1013
// https://nodejs.org/api/esm.html#commonjs-namespaces
@@ -15,3 +18,4 @@ module.exports = cacheExports
1518
exports.unstable_cache = cacheExports.unstable_cache
1619
exports.revalidatePath = cacheExports.revalidatePath
1720
exports.revalidateTag = cacheExports.revalidateTag
21+
exports.unstable_noStore = cacheExports.unstable_noStore

packages/next/src/client/components/static-generation-async-storage.external.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface StaticGenerationStore {
1212
readonly isOnDemandRevalidate?: boolean
1313
readonly isPrerendering?: boolean
1414
readonly isRevalidate?: boolean
15+
readonly isUnstableCacheCallback?: boolean
1516

1617
forceDynamic?: boolean
1718
fetchCache?:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is for modularized imports for next/server to get fully-treeshaking.
2+
export { unstable_noStore as default } from '../spec-extension/unstable-no-store'

packages/next/src/server/web/spec-extension/unstable-cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function unstable_cache<T extends Callback>(
5454
fetchCache: 'only-no-store',
5555
urlPathname: store?.urlPathname || '/',
5656
isStaticGeneration: !!store?.isStaticGeneration,
57+
isUnstableCacheCallback: true,
5758
},
5859
async () => {
5960
const tags = validateTags(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { staticGenerationAsyncStorage } from '../../../client/components/static-generation-async-storage.external'
2+
import { staticGenerationBailout } from '../../../client/components/static-generation-bailout'
3+
4+
export function unstable_noStore() {
5+
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
6+
7+
if (staticGenerationStore?.isUnstableCacheCallback) {
8+
// if called within a next/cache call, we want to cache the result
9+
// and defer to the next/cache call to handle how to cache the result.
10+
return
11+
}
12+
13+
staticGenerationBailout('unstable_noStore', {
14+
link: 'https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering',
15+
})
16+
}

test/e2e/app-dir/app-static/app-static.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ createNextDescribe(
710710
'articles/[slug]/page_client-reference-manifest.js',
711711
'articles/works.html',
712712
'articles/works.rsc',
713+
'no-store/dynamic/page.js',
714+
'no-store/dynamic/page_client-reference-manifest.js',
715+
'no-store/static.html',
716+
'no-store/static.rsc',
717+
'no-store/static/page.js',
718+
'no-store/static/page_client-reference-manifest.js',
713719
].sort()
714720
)
715721
})
@@ -1018,6 +1024,22 @@ createNextDescribe(
10181024
"initialRevalidateSeconds": false,
10191025
"srcRoute": "/hooks/use-search-params/with-suspense",
10201026
},
1027+
"/no-store/static": Object {
1028+
"dataRoute": "/no-store/static.rsc",
1029+
"experimentalBypassFor": Array [
1030+
Object {
1031+
"key": "Next-Action",
1032+
"type": "header",
1033+
},
1034+
Object {
1035+
"key": "content-type",
1036+
"type": "header",
1037+
"value": "multipart/form-data",
1038+
},
1039+
],
1040+
"initialRevalidateSeconds": false,
1041+
"srcRoute": "/no-store/static",
1042+
},
10211043
"/partial-gen-params-no-additional-lang/en/RAND": Object {
10221044
"dataRoute": "/partial-gen-params-no-additional-lang/en/RAND.rsc",
10231045
"experimentalBypassFor": Array [
@@ -2921,6 +2943,30 @@ createNextDescribe(
29212943
})
29222944
})
29232945

2946+
describe('unstable_noStore', () => {
2947+
it('should opt-out of static optimization', async () => {
2948+
const res = await next.fetch('/no-store/dynamic')
2949+
const html = await res.text()
2950+
const data = cheerio.load(html)('#uncached-data').text()
2951+
const res2 = await next.fetch('/no-store/dynamic')
2952+
const html2 = await res2.text()
2953+
const data2 = cheerio.load(html2)('#uncached-data').text()
2954+
2955+
expect(data).not.toEqual(data2)
2956+
})
2957+
2958+
it('should not opt-out of static optimization when used in next/cache', async () => {
2959+
const res = await next.fetch('/no-store/static')
2960+
const html = await res.text()
2961+
const data = cheerio.load(html)('#data').text()
2962+
const res2 = await next.fetch('/no-store/static')
2963+
const html2 = await res2.text()
2964+
const data2 = cheerio.load(html2)('#data').text()
2965+
2966+
expect(data).toEqual(data2)
2967+
})
2968+
})
2969+
29242970
it('should keep querystring on static page', async () => {
29252971
const browser = await next.browser('/blog/tim?message=hello-world')
29262972
const checkUrl = async () =>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getUncachedRandomData } from '../no-store-fn'
2+
3+
export default async function Page() {
4+
const uncachedData = await getUncachedRandomData()
5+
6+
return (
7+
<div>
8+
<p>random: {Math.random()}</p>
9+
<p id="uncached-data">uncachedData: {uncachedData.random}</p>
10+
</div>
11+
)
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { unstable_noStore } from 'next/cache'
2+
3+
export function getUncachedRandomData() {
4+
unstable_noStore()
5+
return {
6+
random: Math.random(),
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use client'
2+
3+
export function RevalidateButton({ onClick }) {
4+
return (
5+
<form action={onClick}>
6+
<button type="submit">revalidate</button>
7+
</form>
8+
)
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { revalidateTag, unstable_cache } from 'next/cache'
2+
import { getUncachedRandomData } from '../no-store-fn'
3+
import { RevalidateButton } from '../revalidate-button'
4+
5+
export default async function Page() {
6+
async function revalidate() {
7+
'use server'
8+
await revalidateTag('no-store-fn')
9+
}
10+
11+
const cachedData = await unstable_cache(
12+
async () => {
13+
return getUncachedRandomData()
14+
},
15+
['random'],
16+
{
17+
tags: ['no-store-fn'],
18+
}
19+
)()
20+
21+
return (
22+
<div>
23+
<p>random: {Math.random()}</p>
24+
<p id="data">cachedData: {cachedData.random}</p>
25+
<RevalidateButton onClick={revalidate} />
26+
</div>
27+
)
28+
}

test/e2e/app-dir/app-static/next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
logging: {
55
level: 'verbose',
66
},
7+
serverActions: true,
78
incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER,
89
},
910

0 commit comments

Comments
 (0)