@@ -4,7 +4,7 @@ import { HttpResponse, http, passthrough } from 'msw'
4
4
import { setupServer } from 'msw/node'
5
5
import { platform } from 'node:process'
6
6
import { v4 } from 'uuid'
7
- import { afterAll , afterEach , beforeAll , beforeEach , expect , test , vi } from 'vitest'
7
+ import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , test , vi } from 'vitest'
8
8
import { type FixtureTestContext } from '../utils/contexts.js'
9
9
import { createFixture , invokeFunction , runPlugin } from '../utils/fixture.js'
10
10
import { encodeBlobKey , generateRandomObjectID , startMockBlobStore } from '../utils/helpers.js'
@@ -19,7 +19,6 @@ beforeAll(() => {
19
19
// and passthrough everything else
20
20
server = setupServer (
21
21
http . post ( 'https://api.netlify.com/api/v1/purge' , ( ) => {
22
- console . log ( 'intercepted purge api call' )
23
22
return HttpResponse . json ( { } )
24
23
} ) ,
25
24
http . all ( / .* / , ( ) => passthrough ( ) ) ,
@@ -91,7 +90,6 @@ test<FixtureTestContext>('Should revalidate path with On-demand Revalidation', a
91
90
expect ( staticPageRevalidated . headers ?. [ 'cache-status' ] ) . toMatch ( / " N e x t .j s " ; h i t / )
92
91
const dateCacheRevalidated = load ( staticPageRevalidated . body ) ( '[data-testid="date-now"]' ) . text ( )
93
92
94
- console . log ( { dateCacheInitial, dateCacheRevalidated } )
95
93
expect ( dateCacheInitial ) . not . toBe ( dateCacheRevalidated )
96
94
} )
97
95
@@ -153,3 +151,189 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a
153
151
'Served 404 page content should use non-default locale if non-default locale is explicitly used in pathname (after basePath)' ,
154
152
) . toBe ( 'fr' )
155
153
} )
154
+
155
+ // These tests describe how the 404 caching should work, but unfortunately it doesn't work like
156
+ // this in v5 and a fix would represent a breaking change so we are skipping them for now, but
157
+ // leaving them here for future reference when considering the next major version
158
+ describe . skip ( '404 caching' , ( ) => {
159
+ describe ( '404 without getStaticProps' , ( ) => {
160
+ test < FixtureTestContext > ( 'not matching dynamic paths should be cached permanently' , async ( ctx ) => {
161
+ await createFixture ( 'page-router' , ctx )
162
+ await runPlugin ( ctx )
163
+
164
+ const notExistingPage = await invokeFunction ( ctx , {
165
+ url : '/not-existing-page' ,
166
+ } )
167
+
168
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
169
+
170
+ expect (
171
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
172
+ 'should be cached permanently' ,
173
+ ) . toBe ( 's-maxage=31536000, stale-while-revalidate=31536000, durable' )
174
+ } )
175
+ test < FixtureTestContext > ( 'matching dynamic path with revalidate should be cached for revalidate time' , async ( ctx ) => {
176
+ await createFixture ( 'page-router' , ctx )
177
+ await runPlugin ( ctx )
178
+
179
+ const notExistingPage = await invokeFunction ( ctx , {
180
+ url : '/products/not-found-with-revalidate' ,
181
+ } )
182
+
183
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
184
+
185
+ expect (
186
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
187
+ 'should be cached for revalidate time' ,
188
+ ) . toBe ( 's-maxage=600, stale-while-revalidate=31536000, durable' )
189
+ } )
190
+ } )
191
+
192
+ describe ( '404 with getStaticProps without revalidate' , ( ) => {
193
+ test < FixtureTestContext > ( 'not matching dynamic paths should be cached permanently' , async ( ctx ) => {
194
+ await createFixture ( 'page-router-base-path-i18n' , ctx )
195
+ await runPlugin ( ctx )
196
+
197
+ const notExistingPage = await invokeFunction ( ctx , {
198
+ url : '/base/path/not-existing-page' ,
199
+ } )
200
+
201
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
202
+
203
+ expect (
204
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
205
+ 'should be cached permanently' ,
206
+ ) . toBe ( 's-maxage=31536000, stale-while-revalidate=31536000, durable' )
207
+ } )
208
+ test < FixtureTestContext > ( 'matching dynamic path with revalidate should be cached for revalidate time' , async ( ctx ) => {
209
+ await createFixture ( 'page-router-base-path-i18n' , ctx )
210
+ await runPlugin ( ctx )
211
+
212
+ const notExistingPage = await invokeFunction ( ctx , {
213
+ url : '/base/path/products/not-found-with-revalidate' ,
214
+ } )
215
+
216
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
217
+
218
+ expect (
219
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
220
+ 'should be cached for revalidate time' ,
221
+ ) . toBe ( 's-maxage=600, stale-while-revalidate=31536000, durable' )
222
+ } )
223
+ } )
224
+
225
+ describe ( '404 with getStaticProps with revalidate' , ( ) => {
226
+ test < FixtureTestContext > ( 'not matching dynamic paths should be cached for 404 page revalidate' , async ( ctx ) => {
227
+ await createFixture ( 'page-router-404-get-static-props-with-revalidate' , ctx )
228
+ await runPlugin ( ctx )
229
+
230
+ // ignoring initial stale case
231
+ await invokeFunction ( ctx , {
232
+ url : 'not-existing-page' ,
233
+ } )
234
+
235
+ await new Promise ( ( res ) => setTimeout ( res , 100 ) )
236
+
237
+ const notExistingPage = await invokeFunction ( ctx , {
238
+ url : 'not-existing-page' ,
239
+ } )
240
+
241
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
242
+
243
+ expect (
244
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
245
+ 'should be cached for 404 page revalidate' ,
246
+ ) . toBe ( 's-maxage=300, stale-while-revalidate=31536000, durable' )
247
+ } )
248
+
249
+ test < FixtureTestContext > ( 'matching dynamic path with revalidate should be cached for revalidate time' , async ( ctx ) => {
250
+ await createFixture ( 'page-router-404-get-static-props-with-revalidate' , ctx )
251
+ await runPlugin ( ctx )
252
+
253
+ // ignoring initial stale case
254
+ await invokeFunction ( ctx , {
255
+ url : 'products/not-found-with-revalidate' ,
256
+ } )
257
+ await new Promise ( ( res ) => setTimeout ( res , 100 ) )
258
+
259
+ const notExistingPage = await invokeFunction ( ctx , {
260
+ url : 'products/not-found-with-revalidate' ,
261
+ } )
262
+
263
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
264
+
265
+ expect (
266
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
267
+ 'should be cached for revalidate time' ,
268
+ ) . toBe ( 's-maxage=600, stale-while-revalidate=31536000, durable' )
269
+ } )
270
+ } )
271
+ } )
272
+
273
+ // This is a temporary fix to ensure that the 404 page itself is cached correctly when requested
274
+ // directly. This is a workaround for a specific customer and should be removed once the 404 caching
275
+ // is fixed in the next major version.
276
+ describe ( '404 page caching' , ( ) => {
277
+ beforeAll ( ( ) => {
278
+ process . env . CACHE_404_PAGE = 'true'
279
+ } )
280
+
281
+ afterAll ( ( ) => {
282
+ delete process . env . CACHE_404_PAGE
283
+ } )
284
+
285
+ test < FixtureTestContext > ( '404 without getStaticProps' , async ( ctx ) => {
286
+ await createFixture ( 'page-router' , ctx )
287
+ await runPlugin ( ctx )
288
+
289
+ const notExistingPage = await invokeFunction ( ctx , {
290
+ url : '/404' ,
291
+ } )
292
+
293
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
294
+
295
+ expect (
296
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
297
+ 'should be cached permanently' ,
298
+ ) . toBe ( 's-maxage=31536000, stale-while-revalidate=31536000, durable' )
299
+ } )
300
+
301
+ test < FixtureTestContext > ( '404 with getStaticProps without revalidate' , async ( ctx ) => {
302
+ await createFixture ( 'page-router-base-path-i18n' , ctx )
303
+ await runPlugin ( ctx )
304
+
305
+ const notExistingPage = await invokeFunction ( ctx , {
306
+ url : '/base/404' ,
307
+ } )
308
+
309
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
310
+
311
+ expect (
312
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
313
+ 'should be cached permanently' ,
314
+ ) . toBe ( 's-maxage=31536000, stale-while-revalidate=31536000, durable' )
315
+ } )
316
+
317
+ test < FixtureTestContext > ( '404 with getStaticProps with revalidate' , async ( ctx ) => {
318
+ await createFixture ( 'page-router-404-get-static-props-with-revalidate' , ctx )
319
+ await runPlugin ( ctx )
320
+
321
+ // ignoring initial stale case
322
+ await invokeFunction ( ctx , {
323
+ url : '/404' ,
324
+ } )
325
+
326
+ await new Promise ( ( res ) => setTimeout ( res , 100 ) )
327
+
328
+ const notExistingPage = await invokeFunction ( ctx , {
329
+ url : '/404' ,
330
+ } )
331
+
332
+ expect ( notExistingPage . statusCode ) . toBe ( 404 )
333
+
334
+ expect (
335
+ notExistingPage . headers [ 'netlify-cdn-cache-control' ] ,
336
+ 'should be cached for 404 page revalidate' ,
337
+ ) . toBe ( 's-maxage=300, stale-while-revalidate=31536000, durable' )
338
+ } )
339
+ } )
0 commit comments