Skip to content

feat: enable TTL for all sites #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
export const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
export const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
export const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g
export const MINIMUM_REVALIDATE_SECONDS = 60

export const DIVIDER = `
────────────────────────────────────────────────────────────────
`
34 changes: 26 additions & 8 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const pLimit = require('p-limit')
const { join } = require('pathe')
const slash = require('slash')

const { MINIMUM_REVALIDATE_SECONDS, DIVIDER } = require('../constants')

const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/

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

const isrFiles = new Set()

const shortRevalidateRoutes = []

Object.entries(prerenderManifest.routes).forEach(([route, { initialRevalidateSeconds }]) => {
if (initialRevalidateSeconds) {
// Find all files used by ISR routes
const trimmedPath = route.slice(1)
isrFiles.add(`${trimmedPath}.html`)
isrFiles.add(`${trimmedPath}.json`)
if (initialRevalidateSeconds < MINIMUM_REVALIDATE_SECONDS) {
shortRevalidateRoutes.push({ Route: route, Revalidate: initialRevalidateSeconds })
}
}
})

Expand Down Expand Up @@ -176,8 +183,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
The following middleware matched statically-rendered pages:

${yellowBright([...matchingMiddleware].map((mid) => `- /${mid}/_middleware`).join('\n'))}

────────────────────────────────────────────────────────────────
${DIVIDER}
`,
)
// There could potentially be thousands of matching pages, so we don't want to spam the console with this
Expand All @@ -187,8 +193,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
The following files matched middleware and were not moved to the CDN:

${yellowBright([...matchedPages].map((mid) => `- ${mid}`).join('\n'))}

────────────────────────────────────────────────────────────────
${DIVIDER}
`,
)
}
Expand All @@ -208,8 +213,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
The following files matched redirects and were not moved to the CDN:

${yellowBright([...matchedRedirects].map((mid) => `- ${mid}`).join('\n'))}

────────────────────────────────────────────────────────────────
${DIVIDER}
`,
)
}
Expand All @@ -219,8 +223,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
The following files matched beforeFiles rewrites and were not moved to the CDN:

${yellowBright([...matchedRewrites].map((mid) => `- ${mid}`).join('\n'))}

────────────────────────────────────────────────────────────────
${DIVIDER}
`,
)
}
Expand Down Expand Up @@ -248,6 +251,21 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
} catch {}
}
}

if (shortRevalidateRoutes.length !== 0) {
console.log(outdent`
The following routes use "revalidate" values of under ${MINIMUM_REVALIDATE_SECONDS} seconds, which is not supported.
They will use a revalidate time of ${MINIMUM_REVALIDATE_SECONDS} seconds instead.
`)
console.table(shortRevalidateRoutes)
// TODO: add these docs
// console.log(
// outdent`
// For more information, see https://ntl.fyi/next-revalidate-time
// ${DIVIDER}
// `,
// )
}
}

const patchFile = async ({ file, from, to }) => {
Expand Down
11 changes: 1 addition & 10 deletions src/helpers/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export const generateRedirects = async ({
const dataRedirects = []
const pageRedirects = []
const isrRedirects = []
let hasIsr = false

const dynamicRouteEntries = Object.entries(dynamicRoutes)

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

Expand Down Expand Up @@ -130,7 +128,7 @@ export const generateRedirects = async ({
// ISR redirects are handled by the regular function. Forced to avoid pre-rendered pages
...isrRedirects.map((redirect) => ({
from: `${basePath}${redirect}`,
to: process.env.EXPERIMENTAL_ODB_TTL ? ODB_FUNCTION_PATH : HANDLER_FUNCTION_PATH,
to: ODB_FUNCTION_PATH,
status: 200,
force: true,
})),
Expand All @@ -150,11 +148,4 @@ export const generateRedirects = async ({
// Everything else is handled by the regular function
{ from: `${basePath}/*`, to: HANDLER_FUNCTION_PATH, status: 200 },
)
if (hasIsr) {
console.log(
yellowBright(outdent`
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.
`),
)
}
}
2 changes: 1 addition & 1 deletion src/templates/getHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const makeHandler =
const cacheHeader = multiValueHeaders['cache-control']?.[0]

if (cacheHeader?.includes('stale-while-revalidate')) {
if (requestMode === 'odb' && process.env.EXPERIMENTAL_ODB_TTL) {
if (requestMode === 'odb') {
requestMode = 'isr'
const ttl = getMaxAge(cacheHeader)
// Long-expiry TTL is basically no TTL
Expand Down
28 changes: 14 additions & 14 deletions test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -279,43 +279,43 @@ Array [
"force": true,
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/1.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/2.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/withFallback/1.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/en/getStaticProps/withRevalidate/withFallback/2.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/getStaticProps/with-revalidate.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"from": "/_next/data/build-id/getStaticProps/withFallback/:id.json",
Expand Down Expand Up @@ -406,19 +406,19 @@ Array [
"force": true,
"from": "/es/getStaticProps/with-revalidate",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/fr/getStaticProps/with-revalidate",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/getStaticProps/with-revalidate",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"from": "/getStaticProps/withFallback/:id",
Expand All @@ -439,13 +439,13 @@ Array [
"force": true,
"from": "/getStaticProps/withRevalidate/1",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/getStaticProps/withRevalidate/2",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"from": "/getStaticProps/withRevalidate/withFallback/:id",
Expand All @@ -456,13 +456,13 @@ Array [
"force": true,
"from": "/getStaticProps/withRevalidate/withFallback/1",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
"from": "/getStaticProps/withRevalidate/withFallback/2",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": true,
Expand Down