Skip to content

Commit 319d27c

Browse files
committed
chore: add new preview redirect to standard function for fallback srcRoutes
1 parent 2a7bdb5 commit 319d27c

File tree

8 files changed

+40
-3
lines changed

8 files changed

+40
-3
lines changed

Diff for: src/lib/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const CUSTOM_HEADERS_PATH = join('.', '_headers')
3838
// creating the next/image redirect
3939
const NEXT_IMAGE_FUNCTION_NAME = 'next_image'
4040

41+
const PREVIEW_MODE_COOKIES = ['Cookie=__prerender_bypass,__next_preview_data']
42+
4143
const SRC_FILES = [PUBLIC_PATH, NEXT_CONFIG_PATH, CUSTOM_REDIRECTS_PATH, CUSTOM_HEADERS_PATH, ...NEXT_SRC_DIRS]
4244

4345
module.exports = {
@@ -52,4 +54,5 @@ module.exports = {
5254
CUSTOM_HEADERS_PATH,
5355
NEXT_IMAGE_FUNCTION_NAME,
5456
SRC_FILES,
57+
PREVIEW_MODE_COOKIES,
5558
}

Diff for: src/lib/helpers/getPreviewModeFunctionName.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Some pages (getStaticProps/withFallback) require a separate standard function for preview mode; this provides the name for that function, to be called in several places
2+
3+
const getPreviewModeFunctionName = (functionName) => `preview-${functionName}`
4+
5+
module.exports = getPreviewModeFunctionName

Diff for: src/lib/helpers/setupNetlifyFunctionForPage.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const getTemplate = require('../templates/getTemplate')
88
const copyDynamicImportChunks = require('./copyDynamicImportChunks')
99
const getNetlifyFunctionName = require('./getNetlifyFunctionName')
1010
const getNextDistDir = require('./getNextDistDir')
11+
const getPreviewModeFunctionName = require('./getPreviewModeFunctionName')
1112
const { logItem } = require('./logger')
1213

1314
// Create a Netlify Function for the page with the given file path
@@ -23,7 +24,7 @@ const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage,
2324
}
2425

2526
// Write entry point to function directory
26-
const fileName = forFallbackPreviewMode ? `preview-${functionName}` : functionName
27+
const fileName = forFallbackPreviewMode ? getPreviewModeFunctionName(functionName) : functionName
2728
const entryPointPath = join(functionDirectory, `${fileName}.js`)
2829
await writeFile(entryPointPath, getTemplate({ filePath, isODB }))
2930

Diff for: src/lib/pages/getStaticProps/redirects.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const addDefaultLocaleRedirect = require('../../helpers/addDefaultLocaleRedirect
66
const asyncForEach = require('../../helpers/asyncForEach')
77
const getFilePathForRoute = require('../../helpers/getFilePathForRoute')
88
const getNetlifyFunctionName = require('../../helpers/getNetlifyFunctionName')
9+
const getPreviewModeFunctionName = require('../../helpers/getPreviewModeFunctionName')
910
const isRouteWithFallback = require('../../helpers/isRouteWithFallback')
1011

1112
const getPages = require('./pages')
@@ -43,7 +44,7 @@ const getRedirects = async () => {
4344
// ODB pages' preview mode needs a special flagged standard function because
4445
// their default function (an ODB) is not functional for preview mode
4546
const target = `/.netlify/functions/${functionName}`
46-
const previewModeTarget = isODB ? `/.netlify/functions/preview-${functionName}` : target
47+
const previewModeTarget = isODB ? `/.netlify/functions/${getPreviewModeFunctionName(functionName)}` : target
4748
const previewModeRedirect = { conditions, force: true, target: previewModeTarget }
4849

4950
// Add a preview mode redirect for the standard route

Diff for: src/lib/pages/getStaticPropsWithFallback/redirects.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const { join } = require('path')
22

33
const slash = require('slash')
44

5+
const { PREVIEW_MODE_COOKIES } = require('../../config')
56
const addLocaleRedirects = require('../../helpers/addLocaleRedirects')
67
const asyncForEach = require('../../helpers/asyncForEach')
78
const getFilePathForRoute = require('../../helpers/getFilePathForRoute')
89
const getNetlifyFunctionName = require('../../helpers/getNetlifyFunctionName')
10+
const getPreviewModeFunctionName = require('../../helpers/getPreviewModeFunctionName')
911

1012
const getPages = require('./pages')
1113

@@ -18,10 +20,19 @@ const getRedirects = async () => {
1820
const filePath = slash(join('pages', relativePath))
1921
const functionName = getNetlifyFunctionName(filePath)
2022
const target = `/.netlify/functions/${functionName}`
23+
const previewModeTarget = `/.netlify/functions/${getPreviewModeFunctionName(functionName)}`
2124

2225
await addLocaleRedirects(redirects, route, target)
2326

24-
// Add one redirect for the page
27+
// Add a redirect for preview mode pointing to the standard function
28+
redirects.push({
29+
route,
30+
target: previewModeTarget,
31+
conditions: PREVIEW_MODE_COOKIES,
32+
force: true,
33+
})
34+
35+
// Add one redirect pointing to the ODB
2536
redirects.push({
2637
route,
2738
target,

Diff for: src/tests/__snapshots__/basePath.test.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ exports[`Routing creates Netlify redirects 1`] = `
6565
/foo/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
6666
/foo/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
6767
/foo/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
68+
/foo/getStaticProps/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
69+
/foo/getStaticProps/withFallback/:id /getStaticProps/withFallback/[id] 200
6870
/foo/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
71+
/foo/getStaticProps/withFallback/:slug/* /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
72+
/foo/getStaticProps/withFallback/:slug/* /getStaticProps/withFallback/[...slug] 200
6973
/foo/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
74+
/foo/getStaticProps/withFallbackBlocking/:id /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
75+
/foo/getStaticProps/withFallbackBlocking/:id /getStaticProps/withFallbackBlocking/[id] 200
7076
/foo/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
77+
/foo/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
78+
/foo/getStaticProps/withRevalidate/withFallback/:id /getStaticProps/withRevalidate/withFallback/[id] 200
7179
/foo/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
7280
/foo/shows/:id /.netlify/functions/next_shows_id 200
7381
/foo/shows/:params/* /.netlify/functions/next_shows_params 200

Diff for: src/tests/__snapshots__/defaults.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ exports[`Routing creates Netlify redirects 1`] = `
5252
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
5353
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
5454
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
55+
/getStaticProps/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
5556
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
57+
/getStaticProps/withFallback/:slug/* /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
5658
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
59+
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
5760
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
61+
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
5862
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
5963
/nextimg/* /.netlify/functions/next_image 200
6064
/shows/:id /.netlify/functions/next_shows_id 200

Diff for: src/tests/__snapshots__/i18n.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ exports[`Routing creates Netlify redirects 1`] = `
118118
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
119119
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
120120
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
121+
/getStaticProps/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
121122
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
123+
/getStaticProps/withFallback/:slug/* /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
122124
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
125+
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
123126
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
127+
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
124128
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
125129
/nextimg/* /.netlify/functions/next_image 200
126130
/shows/:id /.netlify/functions/next_shows_id 200

0 commit comments

Comments
 (0)