Skip to content

Commit 5f428f3

Browse files
committed
chore: add comments
1 parent 287cb1f commit 5f428f3

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

demo/pages/getStaticProps/withRevalidate/[id].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function getStaticProps({ params }) {
3333
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
3434
const data = await res.json()
3535
const time = new Date().toLocaleTimeString()
36-
await new Promise((resolve) => setTimeout(resolve, 10000))
36+
await new Promise((resolve) => setTimeout(resolve, 1000))
3737
return {
3838
props: {
3939
show: data,

src/helpers/config.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
7878
const staticRouteEntries = Object.entries(staticRoutes)
7979

8080
staticRouteEntries.forEach(([route, { dataRoute, initialRevalidateSeconds }]) => {
81-
// With revalidate we need to rewrite to SSR rather than ODB
81+
// Only look for revalidate as we need to rewrite these to SSR rather than ODB
8282
if (initialRevalidateSeconds === false) {
83+
// These can be ignored, as they're static files handled by the CDN
8384
return
8485
}
8586
if (i18n.defaultLocale && route.startsWith(`/${i18n.defaultLocale}/`)) {
@@ -104,30 +105,48 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
104105

105106
// This is only used in prod, so dev uses `next dev` directly
106107
netlifyConfig.redirects.push(
108+
// Static files are in `static`
107109
{ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 },
110+
// API routes always need to be served from the regular function
111+
{
112+
from: `${basePath}/api`,
113+
to: HANDLER_FUNCTION_PATH,
114+
status: 200,
115+
},
116+
{
117+
from: `${basePath}/api/*`,
118+
to: HANDLER_FUNCTION_PATH,
119+
status: 200,
120+
},
121+
// Preview mode gets forced to the function, to bypess pre-rendered pages
108122
{
109123
from: `${basePath}/*`,
110124
to: HANDLER_FUNCTION_PATH,
111125
status: 200,
112126
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
113127
force: true,
114128
},
129+
// ISR redirects are handled by the regular function. Forced to avoid pre-rendered pages
115130
...isrRedirects.map((redirect) => ({
116131
from: `${basePath}${redirect}`,
117132
to: HANDLER_FUNCTION_PATH,
118133
status: 200,
119134
force: true,
120135
})),
136+
// These are pages with fallback set, which need an ODB
137+
// Data redirects go first, to avoid conflict with splat redirects
121138
...dataRedirects.map((redirect) => ({
122139
from: `${basePath}${redirect}`,
123140
to: ODB_FUNCTION_PATH,
124141
status: 200,
125142
})),
143+
// ...then all the other fallback pages
126144
...pageRedirects.map((redirect) => ({
127145
from: `${basePath}${redirect}`,
128146
to: ODB_FUNCTION_PATH,
129147
status: 200,
130148
})),
149+
// Everything else is handled by the regular function
131150
{ from: `${basePath}/*`, to: HANDLER_FUNCTION_PATH, status: 200 },
132151
)
133152
if (hasIsr) {

src/helpers/files.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
5353

5454
Object.entries(prerenderManifest.routes).forEach(([route, { initialRevalidateSeconds }]) => {
5555
if (initialRevalidateSeconds) {
56+
// Find all files used by ISR routes
5657
const trimmedPath = route.slice(1)
5758
isrFiles.add(`${trimmedPath}.html`)
5859
isrFiles.add(`${trimmedPath}.json`)
@@ -83,6 +84,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
8384
const limit = pLimit(Math.max(2, cpus().length))
8485
const promises = pages.map(async (rawPath) => {
8586
const filePath = slash(rawPath)
87+
// Don't move ISR files, as they're used for the first request
8688
if (isrFiles.has(filePath)) {
8789
return
8890
}

src/templates/getHandler.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ const makeHandler =
2020
require.resolve('./pages.js')
2121
} catch {}
2222

23+
// We don't want to write ISR files to disk in the lambda environment
24+
conf.experimental.isrFlushToDisk = false
25+
2326
// Set during the request as it needs the host header. Hoisted so we can define the function once
2427
let base
25-
conf.experimental.isrFlushToDisk = false
2628

2729
// Only do this if we have some static files moved to the CDN
2830
if (staticManifest.length !== 0) {
@@ -42,11 +44,11 @@ const makeHandler =
4244
if (file.startsWith(pageRoot)) {
4345
// We only want the part after `pages/`
4446
const filePath = file.slice(pageRoot.length + 1)
45-
const cacheFile = path.join(cacheDir, filePath)
4647

4748
// Is it in the CDN and not local?
4849
if (staticFiles.has(filePath) && !existsSync(file)) {
4950
// This name is safe to use, because it's one that was already created by Next
51+
const cacheFile = path.join(cacheDir, filePath)
5052
// Have we already cached it? We ignore the cache if running locally to avoid staleness
5153
if ((!existsSync(cacheFile) || process.env.NETLIFY_DEV) && base) {
5254
await promises.mkdir(path.dirname(cacheFile), { recursive: true })

0 commit comments

Comments
 (0)