@@ -7,14 +7,19 @@ import {
7
7
invokeFunction ,
8
8
runPlugin ,
9
9
type FixtureTestContext ,
10
+ runPluginStep ,
10
11
} from '../utils/fixture.js'
11
12
import {
13
+ changeMDate ,
12
14
decodeBlobKey ,
13
15
encodeBlobKey ,
14
16
generateRandomObjectID ,
15
17
getBlobEntries ,
16
18
startMockBlobStore ,
17
19
} from '../utils/helpers.js'
20
+ import { join } from 'path'
21
+ import { existsSync } from 'node:fs'
22
+ import { minify } from 'next/dist/build/swc/index.js'
18
23
19
24
// Disable the verbose logging of the lambda-local runtime
20
25
getLogger ( ) . level = 'alert'
@@ -36,6 +41,22 @@ describe('page router', () => {
36
41
test < FixtureTestContext > ( 'page router with static revalidate' , async ( ctx ) => {
37
42
await createFixture ( 'page-router' , ctx )
38
43
console . time ( 'runPlugin' )
44
+ const {
45
+ constants : { PUBLISH_DIR } ,
46
+ } = await runPluginStep ( ctx , 'onPreBuild' )
47
+ const filePaths = [
48
+ 'server/pages/static/revalidate-automatic.html' ,
49
+ 'server/pages/static/revalidate-automatic.json' ,
50
+ 'server/pages/static/revalidate-slow.html' ,
51
+ 'server/pages/static/revalidate-slow.json' ,
52
+ ]
53
+
54
+ filePaths . forEach ( async ( filePath ) => {
55
+ if ( existsSync ( filePath ) ) {
56
+ // Changing the fetch files modified date to a past date since the test files are copied and dont preserve the mtime locally
57
+ await changeMDate ( join ( PUBLISH_DIR , filePath ) , 1674690060000 )
58
+ }
59
+ } )
39
60
await runPlugin ( ctx )
40
61
console . timeEnd ( 'runPlugin' )
41
62
// check if the blob entries where successful set on the build plugin
@@ -55,19 +76,20 @@ describe('page router', () => {
55
76
const call1Date = load ( call1 . body ) ( '[data-testid="date-now"]' ) . text ( )
56
77
expect ( call1 . statusCode ) . toBe ( 200 )
57
78
expect ( load ( call1 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Show #71' )
58
- expect ( call1 . headers , 'a cache hit on the first invocation of a prerendered page' ) . toEqual (
79
+ // Because we're using mtime instead of Date.now() first invocation will actually be a cache miss.
80
+ expect ( call1 . headers , 'a cache miss on the first invocation of a prerendered page' ) . toEqual (
59
81
expect . objectContaining ( {
60
- 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; h i t / ) ,
82
+ 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; m i s s / ) ,
61
83
'netlify-cdn-cache-control' : 's-maxage=5, stale-while-revalidate=31536000' ,
62
84
} ) ,
63
85
)
64
86
87
+ // wait to have a stale page
88
+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 9_000 ) )
89
+
65
90
// Ping this now so we can wait in parallel
66
91
const callLater = await invokeFunction ( ctx , { url : 'static/revalidate-slow' } )
67
92
68
- // wait to have a stale page
69
- await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 3_000 ) )
70
-
71
93
// now it should be a cache miss
72
94
const call2 = await invokeFunction ( ctx , { url : 'static/revalidate-automatic' } )
73
95
const call2Date = load ( call2 . body ) ( '[data-testid="date-now"]' ) . text ( )
@@ -131,9 +153,10 @@ describe('app router', () => {
131
153
const post1Date = load ( post1 . body ) ( '[data-testid="date-now"]' ) . text ( )
132
154
expect ( post1 . statusCode ) . toBe ( 200 )
133
155
expect ( load ( post1 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Revalidate Fetch' )
134
- expect ( post1 . headers , 'a cache hit on the first invocation of a prerendered page' ) . toEqual (
156
+ expect ( post1 . headers , 'a cache miss on the first invocation of a prerendered page' ) . toEqual (
157
+ // It will be stale/miss instead of hit
135
158
expect . objectContaining ( {
136
- 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; h i t / ) ,
159
+ 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; m i s s / ) ,
137
160
'netlify-cdn-cache-control' : 's-maxage=5, stale-while-revalidate=31536000' ,
138
161
} ) ,
139
162
)
@@ -228,9 +251,9 @@ describe('route', () => {
228
251
name : 'Under the Dome' ,
229
252
} ) ,
230
253
} )
231
- expect ( call1 . headers , 'a cache hit on the first invocation of a prerendered route ' ) . toEqual (
254
+ expect ( call1 . headers , 'a cache miss on the first invocation' ) . toEqual (
232
255
expect . objectContaining ( {
233
- 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; h i t / ) ,
256
+ 'cache-status' : expect . stringMatching ( / " N e x t .j s " ; m i s s / ) ,
234
257
} ) ,
235
258
)
236
259
// wait to have a stale route
0 commit comments