Skip to content

Commit 152cf03

Browse files
authored
feat: enable TTL for all sites (#916)
* feat: enable TTL for all sites * chore: snapidoo
1 parent 552e333 commit 152cf03

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ export const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
2121
export const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
2222
export const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
2323
export const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g
24+
export const MINIMUM_REVALIDATE_SECONDS = 60
25+
26+
export const DIVIDER = `
27+
────────────────────────────────────────────────────────────────
28+
`

src/helpers/files.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const pLimit = require('p-limit')
1919
const { join } = require('pathe')
2020
const slash = require('slash')
2121

22+
const { MINIMUM_REVALIDATE_SECONDS, DIVIDER } = require('../constants')
23+
2224
const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/
2325

2426
const isDynamicRoute = (route) => TEST_ROUTE.test(route)
@@ -90,12 +92,17 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
9092

9193
const isrFiles = new Set()
9294

95+
const shortRevalidateRoutes = []
96+
9397
Object.entries(prerenderManifest.routes).forEach(([route, { initialRevalidateSeconds }]) => {
9498
if (initialRevalidateSeconds) {
9599
// Find all files used by ISR routes
96100
const trimmedPath = route.slice(1)
97101
isrFiles.add(`${trimmedPath}.html`)
98102
isrFiles.add(`${trimmedPath}.json`)
103+
if (initialRevalidateSeconds < MINIMUM_REVALIDATE_SECONDS) {
104+
shortRevalidateRoutes.push({ Route: route, Revalidate: initialRevalidateSeconds })
105+
}
99106
}
100107
})
101108

@@ -176,8 +183,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
176183
The following middleware matched statically-rendered pages:
177184
178185
${yellowBright([...matchingMiddleware].map((mid) => `- /${mid}/_middleware`).join('\n'))}
179-
180-
────────────────────────────────────────────────────────────────
186+
${DIVIDER}
181187
`,
182188
)
183189
// There could potentially be thousands of matching pages, so we don't want to spam the console with this
@@ -187,8 +193,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
187193
The following files matched middleware and were not moved to the CDN:
188194
189195
${yellowBright([...matchedPages].map((mid) => `- ${mid}`).join('\n'))}
190-
191-
────────────────────────────────────────────────────────────────
196+
${DIVIDER}
192197
`,
193198
)
194199
}
@@ -208,8 +213,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
208213
The following files matched redirects and were not moved to the CDN:
209214
210215
${yellowBright([...matchedRedirects].map((mid) => `- ${mid}`).join('\n'))}
211-
212-
────────────────────────────────────────────────────────────────
216+
${DIVIDER}
213217
`,
214218
)
215219
}
@@ -219,8 +223,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
219223
The following files matched beforeFiles rewrites and were not moved to the CDN:
220224
221225
${yellowBright([...matchedRewrites].map((mid) => `- ${mid}`).join('\n'))}
222-
223-
────────────────────────────────────────────────────────────────
226+
${DIVIDER}
224227
`,
225228
)
226229
}
@@ -248,6 +251,21 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
248251
} catch {}
249252
}
250253
}
254+
255+
if (shortRevalidateRoutes.length !== 0) {
256+
console.log(outdent`
257+
The following routes use "revalidate" values of under ${MINIMUM_REVALIDATE_SECONDS} seconds, which is not supported.
258+
They will use a revalidate time of ${MINIMUM_REVALIDATE_SECONDS} seconds instead.
259+
`)
260+
console.table(shortRevalidateRoutes)
261+
// TODO: add these docs
262+
// console.log(
263+
// outdent`
264+
// For more information, see https://ntl.fyi/next-revalidate-time
265+
// ${DIVIDER}
266+
// `,
267+
// )
268+
}
251269
}
252270

253271
const patchFile = async ({ file, from, to }) => {

src/helpers/redirects.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export const generateRedirects = async ({
7070
const dataRedirects = []
7171
const pageRedirects = []
7272
const isrRedirects = []
73-
let hasIsr = false
7473

7574
const dynamicRouteEntries = Object.entries(dynamicRoutes)
7675

@@ -85,7 +84,6 @@ export const generateRedirects = async ({
8584
if (i18n?.defaultLocale && route.startsWith(`/${i18n.defaultLocale}/`)) {
8685
route = route.slice(i18n.defaultLocale.length + 1)
8786
}
88-
hasIsr = true
8987
isrRedirects.push(...netlifyRoutesForNextRoute(dataRoute), ...netlifyRoutesForNextRoute(route))
9088
})
9189

@@ -130,7 +128,7 @@ export const generateRedirects = async ({
130128
// ISR redirects are handled by the regular function. Forced to avoid pre-rendered pages
131129
...isrRedirects.map((redirect) => ({
132130
from: `${basePath}${redirect}`,
133-
to: process.env.EXPERIMENTAL_ODB_TTL ? ODB_FUNCTION_PATH : HANDLER_FUNCTION_PATH,
131+
to: ODB_FUNCTION_PATH,
134132
status: 200,
135133
force: true,
136134
})),
@@ -150,11 +148,4 @@ export const generateRedirects = async ({
150148
// Everything else is handled by the regular function
151149
{ from: `${basePath}/*`, to: HANDLER_FUNCTION_PATH, status: 200 },
152150
)
153-
if (hasIsr) {
154-
console.log(
155-
yellowBright(outdent`
156-
You have some pages that use ISR (pages that use getStaticProps with revalidate set), which is not currently fully-supported by this plugin. Be aware that results may be unreliable.
157-
`),
158-
)
159-
}
160151
}

src/templates/getHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const makeHandler =
8181
const cacheHeader = multiValueHeaders['cache-control']?.[0]
8282

8383
if (cacheHeader?.includes('stale-while-revalidate')) {
84-
if (requestMode === 'odb' && process.env.EXPERIMENTAL_ODB_TTL) {
84+
if (requestMode === 'odb') {
8585
requestMode = 'isr'
8686
const ttl = getMaxAge(cacheHeader)
8787
// Long-expiry TTL is basically no TTL

test/__snapshots__/index.js.snap

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,43 +279,43 @@ Array [
279279
"force": true,
280280
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/1.json",
281281
"status": 200,
282-
"to": "/.netlify/functions/___netlify-handler",
282+
"to": "/.netlify/builders/___netlify-odb-handler",
283283
},
284284
Object {
285285
"force": true,
286286
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/2.json",
287287
"status": 200,
288-
"to": "/.netlify/functions/___netlify-handler",
288+
"to": "/.netlify/builders/___netlify-odb-handler",
289289
},
290290
Object {
291291
"force": true,
292292
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/withFallback/1.json",
293293
"status": 200,
294-
"to": "/.netlify/functions/___netlify-handler",
294+
"to": "/.netlify/builders/___netlify-odb-handler",
295295
},
296296
Object {
297297
"force": true,
298298
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/withFallback/2.json",
299299
"status": 200,
300-
"to": "/.netlify/functions/___netlify-handler",
300+
"to": "/.netlify/builders/___netlify-odb-handler",
301301
},
302302
Object {
303303
"force": true,
304304
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
305305
"status": 200,
306-
"to": "/.netlify/functions/___netlify-handler",
306+
"to": "/.netlify/builders/___netlify-odb-handler",
307307
},
308308
Object {
309309
"force": true,
310310
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
311311
"status": 200,
312-
"to": "/.netlify/functions/___netlify-handler",
312+
"to": "/.netlify/builders/___netlify-odb-handler",
313313
},
314314
Object {
315315
"force": true,
316316
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
317317
"status": 200,
318-
"to": "/.netlify/functions/___netlify-handler",
318+
"to": "/.netlify/builders/___netlify-odb-handler",
319319
},
320320
Object {
321321
"from": "/_next/data/build-id/getStaticProps/withFallback/:id.json",
@@ -406,19 +406,19 @@ Array [
406406
"force": true,
407407
"from": "/es/getStaticProps/with-revalidate",
408408
"status": 200,
409-
"to": "/.netlify/functions/___netlify-handler",
409+
"to": "/.netlify/builders/___netlify-odb-handler",
410410
},
411411
Object {
412412
"force": true,
413413
"from": "/fr/getStaticProps/with-revalidate",
414414
"status": 200,
415-
"to": "/.netlify/functions/___netlify-handler",
415+
"to": "/.netlify/builders/___netlify-odb-handler",
416416
},
417417
Object {
418418
"force": true,
419419
"from": "/getStaticProps/with-revalidate",
420420
"status": 200,
421-
"to": "/.netlify/functions/___netlify-handler",
421+
"to": "/.netlify/builders/___netlify-odb-handler",
422422
},
423423
Object {
424424
"from": "/getStaticProps/withFallback/:id",
@@ -439,13 +439,13 @@ Array [
439439
"force": true,
440440
"from": "/getStaticProps/withRevalidate/1",
441441
"status": 200,
442-
"to": "/.netlify/functions/___netlify-handler",
442+
"to": "/.netlify/builders/___netlify-odb-handler",
443443
},
444444
Object {
445445
"force": true,
446446
"from": "/getStaticProps/withRevalidate/2",
447447
"status": 200,
448-
"to": "/.netlify/functions/___netlify-handler",
448+
"to": "/.netlify/builders/___netlify-odb-handler",
449449
},
450450
Object {
451451
"from": "/getStaticProps/withRevalidate/withFallback/:id",
@@ -456,13 +456,13 @@ Array [
456456
"force": true,
457457
"from": "/getStaticProps/withRevalidate/withFallback/1",
458458
"status": 200,
459-
"to": "/.netlify/functions/___netlify-handler",
459+
"to": "/.netlify/builders/___netlify-odb-handler",
460460
},
461461
Object {
462462
"force": true,
463463
"from": "/getStaticProps/withRevalidate/withFallback/2",
464464
"status": 200,
465-
"to": "/.netlify/functions/___netlify-handler",
465+
"to": "/.netlify/builders/___netlify-odb-handler",
466466
},
467467
Object {
468468
"force": true,

0 commit comments

Comments
 (0)