Skip to content

Commit c16a3f9

Browse files
committed
Use addDependency to track metadata route file changes (#66714)
Use `addDependency` to track the file path passed to `next-metadata-route-loader` NOTE: We cannot apply the `next-metadata-route-loader` directly to the metatda convention source files, since the json file could be processed by json loader (Related previous fix #62615) Previously when we passed down the file path as argument to the loader, which sort of breaking the caching of webpack as the actual resource path is string, it's not tracked as a dependency. This change fixed the bad caching issue of static metadata routes. Based on the above reason we use `addDependency` here to track the dependency change Closes NEXT-3521 Closes #65755
1 parent 942e45a commit c16a3f9

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

packages/next/src/build/webpack/loaders/next-metadata-route-loader.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const cacheHeader = {
2222

2323
type MetadataRouteLoaderOptions = {
2424
page: string
25+
// Using separate argument to avoid json being parsed and hit error
26+
// x-ref: https://github.com/vercel/next.js/pull/62615
2527
filePath: string
2628
isDynamic: '1' | '0'
2729
}
@@ -55,8 +57,8 @@ async function getStaticAssetRouteCode(
5557
fileBaseName === 'favicon'
5658
? 'public, max-age=0, must-revalidate'
5759
: process.env.NODE_ENV !== 'production'
58-
? cacheHeader.none
59-
: cacheHeader.longCache
60+
? cacheHeader.none
61+
: cacheHeader.longCache
6062
const code = `\
6163
/* static asset route */
6264
import { NextResponse } from 'next/server'
@@ -259,6 +261,7 @@ const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction<MetadataRouteLo
259261
async function () {
260262
const { page, isDynamic, filePath } = this.getOptions()
261263
const { name: fileBaseName } = getFilenameAndExtension(filePath)
264+
this.addDependency(filePath)
262265

263266
let code = ''
264267
if (isDynamic === '1') {
Binary file not shown.
Binary file not shown.
Loading
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
import crypto from 'crypto'
3+
4+
function generateMD5(text: string) {
5+
const hash = crypto.createHash('md5')
6+
hash.update(text)
7+
return hash.digest('hex')
8+
}
9+
10+
describe('app dir - metadata static routes cache', () => {
11+
const { next } = nextTestSetup({
12+
files: __dirname,
13+
skipStart: true,
14+
})
15+
16+
it('should generate different content after replace the static metadata file', async () => {
17+
await next.build()
18+
19+
const faviconBuildContent = await next.readFile(
20+
'.next/server/app/favicon.ico.body'
21+
)
22+
const opengrpahImageBuildContent = await next.readFile(
23+
'.next/server/app/opengraph-image.png.body'
24+
)
25+
26+
const faviconMd5 = generateMD5(faviconBuildContent)
27+
const opengraphImageMd5 = generateMD5(opengrpahImageBuildContent)
28+
29+
// Update favicon and opengraph image
30+
const newFaviconContent = await next.readFile('app/favicon.ico.new')
31+
await next.patchFile('app/favicon.ico', newFaviconContent)
32+
33+
const newOpengraphImageContent = await next.readFile(
34+
'app/opengraph-image.png.new'
35+
)
36+
await next.patchFile('app/opengraph-image.png', newOpengraphImageContent)
37+
38+
await next.build()
39+
const faviconBuildContentNew = await next.readFile(
40+
'.next/server/app/favicon.ico.body'
41+
)
42+
const opengrpahImageBuildContentNew = await next.readFile(
43+
'.next/server/app/opengraph-image.png.body'
44+
)
45+
46+
const faviconMd5New = generateMD5(faviconBuildContentNew)
47+
const opengraphImageMd5New = generateMD5(opengrpahImageBuildContentNew)
48+
49+
expect(faviconMd5).not.toBe(faviconMd5New)
50+
expect(opengraphImageMd5).not.toBe(opengraphImageMd5New)
51+
})
52+
})

test/turbopack-build-tests-manifest.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15480,6 +15480,22 @@
1548015480
"pending": [],
1548115481
"flakey": [],
1548215482
"runtimeError": false
15483+
},
15484+
"test/production/typescript-basic/typechecking.test.ts": {
15485+
"passed": ["typechecking should typecheck"],
15486+
"failed": [],
15487+
"pending": [],
15488+
"flakey": [],
15489+
"runtimeError": false
15490+
},
15491+
"test/production/app-dir/metadata-static-route-cache/metadata-static-route-cache.test.ts": {
15492+
"passed": [],
15493+
"failed": [
15494+
"app dir - metadata static routes cache should generate different content after replace the static metadata file"
15495+
],
15496+
"pending": [],
15497+
"flakey": [],
15498+
"runtimeError": false
1548315499
}
1548415500
},
1548515501
"rules": {

0 commit comments

Comments
 (0)