Skip to content

Commit bbedbdb

Browse files
committed
test: update 404 caching tests
1 parent 25ad18c commit bbedbdb

File tree

3 files changed

+54
-56
lines changed

3 files changed

+54
-56
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function NotFound() {
2+
return <p>Custom 404 page</p>
3+
}

tests/fixtures/page-router/pages/products/[slug].js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const Product = ({ time, slug }) => (
99
)
1010

1111
export async function getStaticProps({ params }) {
12+
if (params.slug === 'not-found-with-revalidate') {
13+
return {
14+
notFound: true,
15+
revalidate: 600,
16+
}
17+
}
18+
1219
return {
1320
props: {
1421
time: new Date().toISOString(),

tests/integration/page-router.test.ts

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -154,51 +154,59 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a
154154
).toBe('fr')
155155
})
156156

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-
157+
describe.only('404 caching', () => {
164158
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', () => {
171159
test<FixtureTestContext>('not matching dynamic paths should be cached permanently', async (ctx) => {
172-
console.log('[test] not matching dynamic paths')
160+
await createFixture('page-router', ctx)
161+
await runPlugin(ctx)
173162

174-
await createFixture('page-router-base-path-i18n', ctx)
163+
const notExistingPage = await invokeFunction(ctx, {
164+
url: '/not-existing-page',
165+
})
166+
167+
expect(notExistingPage.statusCode).toBe(404)
168+
169+
expect(
170+
notExistingPage.headers['netlify-cdn-cache-control'],
171+
'should be cached permanently',
172+
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
173+
})
174+
test<FixtureTestContext>('matching dynamic path with revalidate should be cached permanently', async (ctx) => {
175+
await createFixture('page-router', ctx)
175176
await runPlugin(ctx)
176177

177178
const notExistingPage = await invokeFunction(ctx, {
178-
url: '/base/path/not-existing-page',
179+
url: '/products/not-found-with-revalidate',
179180
})
180181

182+
expect(notExistingPage.statusCode).toBe(404)
183+
181184
expect(
182185
notExistingPage.headers['netlify-cdn-cache-control'],
183186
'should be cached permanently',
184187
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
185188
})
186-
test<FixtureTestContext>('matching dynamic path without revalidate should be cached permanently', async (ctx) => {
187-
console.log('[test] matching dynamic path without revalidate')
189+
})
190+
191+
describe('404 with getStaticProps without revalidate', () => {
192+
test<FixtureTestContext>('not matching dynamic paths should be cached permanently', async (ctx) => {
193+
console.log('[test] not matching dynamic paths')
188194

189195
await createFixture('page-router-base-path-i18n', ctx)
190196
await runPlugin(ctx)
191197

192198
const notExistingPage = await invokeFunction(ctx, {
193-
url: '/base/path/products/not-found-no-revalidate',
199+
url: '/base/path/not-existing-page',
194200
})
195201

202+
expect(notExistingPage.statusCode).toBe(404)
203+
196204
expect(
197205
notExistingPage.headers['netlify-cdn-cache-control'],
198206
'should be cached permanently',
199207
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
200208
})
201-
test<FixtureTestContext>('matching dynamic path with revalidate should be cached for revalidate time', async (ctx) => {
209+
test<FixtureTestContext>('matching dynamic path with revalidate should be cached permanently', async (ctx) => {
202210
console.log('[test] matching dynamic path with revalidate')
203211

204212
await createFixture('page-router-base-path-i18n', ctx)
@@ -209,80 +217,60 @@ describe('404 caching', () => {
209217
})
210218

211219
expect(notExistingPage.statusCode).toBe(404)
212-
// this page was not prerendered, so no STALE case here
220+
213221
expect(
214222
notExistingPage.headers['netlify-cdn-cache-control'],
215-
'should be cached for 600 seconds',
216-
).toBe('s-maxage=600, stale-while-revalidate=31536000, durable')
223+
'should be cached permanently',
224+
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable')
217225
})
218226
})
219227

220228
describe('404 with getStaticProps with revalidate', () => {
221229
test<FixtureTestContext>('not matching dynamic paths should be cached for 404 page revalidate', async (ctx) => {
222-
console.log('[test] not matching dynamic paths')
223-
224230
await createFixture('page-router-404-get-static-props-with-revalidate', ctx)
225231
await runPlugin(ctx)
226232

227233
// ignoring initial stale case
228234
await invokeFunction(ctx, {
229235
url: 'not-existing-page',
230236
})
237+
231238
await new Promise((res) => setTimeout(res, 100))
232239

233240
const notExistingPage = await invokeFunction(ctx, {
234241
url: 'not-existing-page',
235242
})
236243

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-
})
244+
expect(notExistingPage.statusCode).toBe(404)
257245

258246
expect(
259247
notExistingPage.headers['netlify-cdn-cache-control'],
260-
'should be cached permanently',
261-
).toBe('s-maxage=31536000, durable')
248+
'should be cached for 404 page revalidate',
249+
).toBe('s-maxage=300, stale-while-revalidate=31536000, durable')
262250
})
263251

264-
test.only<FixtureTestContext>('matching dynamic path with revalidate should be cached for revalidate time', async (ctx) => {
252+
test<FixtureTestContext>('matching dynamic path with revalidate should be cached for 404 page revalidate', async (ctx) => {
265253
console.log('[test] matching dynamic path with revalidate')
266254

267255
await createFixture('page-router-404-get-static-props-with-revalidate', ctx)
268256
await runPlugin(ctx)
269257

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))
258+
// ignoring initial stale case
259+
await invokeFunction(ctx, {
260+
url: 'products/not-found-with-revalidate',
261+
})
262+
await new Promise((res) => setTimeout(res, 100))
275263

276264
const notExistingPage = await invokeFunction(ctx, {
277265
url: 'products/not-found-with-revalidate',
278266
})
279267

280268
expect(notExistingPage.statusCode).toBe(404)
281-
// this page was not prerendered, so no STALE case here
269+
282270
expect(
283271
notExistingPage.headers['netlify-cdn-cache-control'],
284-
'should be cached for 600 seconds',
285-
).toBe('s-maxage=600, stale-while-revalidate=31535400, durable')
272+
'should be cached for 404 page revalidate',
273+
).toBe('s-maxage=300, stale-while-revalidate=31536000, durable')
286274
})
287275
})
288276
})

0 commit comments

Comments
 (0)