Skip to content

Commit 25ad18c

Browse files
committed
tmp: outline 404 integration test cases for page router
1 parent 3423330 commit 25ad18c

File tree

1 file changed

+134
-13
lines changed

1 file changed

+134
-13
lines changed

tests/integration/page-router.test.ts

+134-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HttpResponse, http, passthrough } from 'msw'
44
import { setupServer } from 'msw/node'
55
import { platform } from 'node:process'
66
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'
88
import { type FixtureTestContext } from '../utils/contexts.js'
99
import { createFixture, invokeFunction, runPlugin } from '../utils/fixture.js'
1010
import { encodeBlobKey, generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
@@ -126,10 +126,6 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a
126126
load(responseImplicitDefaultLocale.body)('[data-testid="locale"]').text(),
127127
'Served 404 page content should use default locale if locale is not explicitly used in pathname (after basePath)',
128128
).toBe('en')
129-
expect(
130-
responseImplicitDefaultLocale.headers['netlify-cdn-cache-control'],
131-
'Response for not existing route if locale is not explicitly used in pathname (after basePath) should have 404 status',
132-
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
133129

134130
const responseExplicitDefaultLocale = await invokeFunction(ctx, {
135131
url: '/base/path/en/not-existing-page',
@@ -143,10 +139,6 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a
143139
load(responseExplicitDefaultLocale.body)('[data-testid="locale"]').text(),
144140
'Served 404 page content should use default locale if default locale is explicitly used in pathname (after basePath)',
145141
).toBe('en')
146-
expect(
147-
responseExplicitDefaultLocale.headers['netlify-cdn-cache-control'],
148-
'Response for not existing route if locale is not explicitly used in pathname (after basePath) should have 404 status',
149-
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
150142

151143
const responseNonDefaultLocale = await invokeFunction(ctx, {
152144
url: '/base/path/fr/not-existing-page',
@@ -160,8 +152,137 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a
160152
load(responseNonDefaultLocale.body)('[data-testid="locale"]').text(),
161153
'Served 404 page content should use non-default locale if non-default locale is explicitly used in pathname (after basePath)',
162154
).toBe('fr')
163-
expect(
164-
responseNonDefaultLocale.headers['netlify-cdn-cache-control'],
165-
'Response for not existing route if locale is not explicitly used in pathname (after basePath) should have 404 status',
166-
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
155+
})
156+
157+
describe('404 caching', () => {
158+
describe('default nextjs 404 (no custom 404)', () => {
159+
test.todo<FixtureTestContext>('not matching dynamic paths')
160+
test.todo<FixtureTestContext>('matching dynamic path without revalidate')
161+
test.todo<FixtureTestContext>('matching dynamic path with revalidate')
162+
})
163+
164+
describe('404 without getStaticProps', () => {
165+
test.todo<FixtureTestContext>('not matching dynamic paths')
166+
test.todo<FixtureTestContext>('matching dynamic path without revalidate')
167+
test.todo<FixtureTestContext>('matching dynamic path with revalidate')
168+
})
169+
170+
describe('404 with getStaticProps without revalidate', () => {
171+
test<FixtureTestContext>('not matching dynamic paths should be cached permanently', async (ctx) => {
172+
console.log('[test] not matching dynamic paths')
173+
174+
await createFixture('page-router-base-path-i18n', ctx)
175+
await runPlugin(ctx)
176+
177+
const notExistingPage = await invokeFunction(ctx, {
178+
url: '/base/path/not-existing-page',
179+
})
180+
181+
expect(
182+
notExistingPage.headers['netlify-cdn-cache-control'],
183+
'should be cached permanently',
184+
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
185+
})
186+
test<FixtureTestContext>('matching dynamic path without revalidate should be cached permanently', async (ctx) => {
187+
console.log('[test] matching dynamic path without revalidate')
188+
189+
await createFixture('page-router-base-path-i18n', ctx)
190+
await runPlugin(ctx)
191+
192+
const notExistingPage = await invokeFunction(ctx, {
193+
url: '/base/path/products/not-found-no-revalidate',
194+
})
195+
196+
expect(
197+
notExistingPage.headers['netlify-cdn-cache-control'],
198+
'should be cached permanently',
199+
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
200+
})
201+
test<FixtureTestContext>('matching dynamic path with revalidate should be cached for revalidate time', async (ctx) => {
202+
console.log('[test] matching dynamic path with revalidate')
203+
204+
await createFixture('page-router-base-path-i18n', ctx)
205+
await runPlugin(ctx)
206+
207+
const notExistingPage = await invokeFunction(ctx, {
208+
url: '/base/path/products/not-found-with-revalidate',
209+
})
210+
211+
expect(notExistingPage.statusCode).toBe(404)
212+
// this page was not prerendered, so no STALE case here
213+
expect(
214+
notExistingPage.headers['netlify-cdn-cache-control'],
215+
'should be cached for 600 seconds',
216+
).toBe('s-maxage=600, stale-while-revalidate=31536000, durable')
217+
})
218+
})
219+
220+
describe('404 with getStaticProps with revalidate', () => {
221+
test<FixtureTestContext>('not matching dynamic paths should be cached for 404 page revalidate', async (ctx) => {
222+
console.log('[test] not matching dynamic paths')
223+
224+
await createFixture('page-router-404-get-static-props-with-revalidate', ctx)
225+
await runPlugin(ctx)
226+
227+
// ignoring initial stale case
228+
await invokeFunction(ctx, {
229+
url: 'not-existing-page',
230+
})
231+
await new Promise((res) => setTimeout(res, 100))
232+
233+
const notExistingPage = await invokeFunction(ctx, {
234+
url: 'not-existing-page',
235+
})
236+
237+
expect(
238+
notExistingPage.headers['netlify-cdn-cache-control'],
239+
'should be cached permanently',
240+
).toBe('s-maxage=300, stale-while-revalidate=31536000, durable')
241+
})
242+
test.only<FixtureTestContext>('matching dynamic path without revalidate should be cached permanently', async (ctx) => {
243+
console.log('[test] matching dynamic path without revalidate')
244+
245+
await createFixture('page-router-404-get-static-props-with-revalidate', ctx)
246+
await runPlugin(ctx)
247+
248+
// // ignoring initial stale case
249+
// await invokeFunction(ctx, {
250+
// url: 'products/not-found-no-revalidate',
251+
// })
252+
// await new Promise((res) => setTimeout(res, 100))
253+
254+
const notExistingPage = await invokeFunction(ctx, {
255+
url: 'products/not-found-no-revalidate',
256+
})
257+
258+
expect(
259+
notExistingPage.headers['netlify-cdn-cache-control'],
260+
'should be cached permanently',
261+
).toBe('s-maxage=31536000, durable')
262+
})
263+
264+
test.only<FixtureTestContext>('matching dynamic path with revalidate should be cached for revalidate time', async (ctx) => {
265+
console.log('[test] matching dynamic path with revalidate')
266+
267+
await createFixture('page-router-404-get-static-props-with-revalidate', ctx)
268+
await runPlugin(ctx)
269+
270+
// // ignoring initial stale case
271+
// await invokeFunction(ctx, {
272+
// url: 'products/not-found-with-revalidate',
273+
// })
274+
// await new Promise((res) => setTimeout(res, 100))
275+
276+
const notExistingPage = await invokeFunction(ctx, {
277+
url: 'products/not-found-with-revalidate',
278+
})
279+
280+
expect(notExistingPage.statusCode).toBe(404)
281+
// this page was not prerendered, so no STALE case here
282+
expect(
283+
notExistingPage.headers['netlify-cdn-cache-control'],
284+
'should be cached for 600 seconds',
285+
).toBe('s-maxage=600, stale-while-revalidate=31535400, durable')
286+
})
287+
})
167288
})

0 commit comments

Comments
 (0)