Skip to content

Commit 5ab6123

Browse files
committed
fix: preview mode for odb/fallback pages
1 parent 386029e commit 5ab6123

File tree

7 files changed

+57
-46
lines changed

7 files changed

+57
-46
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const getNextDistDir = require('./getNextDistDir')
1111
const { logItem } = require('./logger')
1212

1313
// Create a Netlify Function for the page with the given file path
14-
const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage, isISR }) => {
14+
const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage, isODB, forFallbackPreviewMode }) => {
1515
// Set function name based on file path
1616
const functionName = getNetlifyFunctionName(filePath, isApiPage)
1717
const functionDirectory = join(functionsPath, functionName)
@@ -23,8 +23,9 @@ const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage,
2323
}
2424

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

2930
// Copy function helper
3031
await copy(join(TEMPLATES_DIR, 'getHandlerFunction.js'), join(functionDirectory, 'getHandlerFunction.js'))

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

+7-2
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 isRouteWithFallback = require('../../helpers/isRouteWithFallback')
910

1011
const getPages = require('./pages')
1112

@@ -35,11 +36,15 @@ const getRedirects = async () => {
3536
const relativePath = getFilePathForRoute(srcRoute || route, 'js')
3637
const filePath = slash(join('pages', relativePath))
3738
const functionName = getNetlifyFunctionName(filePath)
39+
const isODB = await isRouteWithFallback(srcRoute)
3840

3941
// Preview mode conditions
4042
const conditions = ['Cookie=__prerender_bypass,__next_preview_data']
43+
// ODB pages' preview mode needs a special flagged standard function because
44+
// their default function (an ODB) is not functional for preview mode
4145
const target = `/.netlify/functions/${functionName}`
42-
const previewModeRedirect = { conditions, force: true, target }
46+
const previewModeTarget = isODB ? `/.netlify/functions/preview-${functionName}` : target
47+
const previewModeRedirect = { conditions, force: true, target: previewModeTarget }
4348

4449
// Add a preview mode redirect for the standard route
4550
redirects.push({
@@ -54,7 +59,7 @@ const getRedirects = async () => {
5459
})
5560

5661
// Preview mode default locale redirect must precede normal default locale redirect
57-
await addDefaultLocaleRedirect(redirects, route, target, previewModeRedirect)
62+
await addDefaultLocaleRedirect(redirects, route, target, { conditions, force: true })
5863
await addDefaultLocaleRedirect(redirects, route)
5964
})
6065

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ const setup = async (functionsPath) => {
1414
const pages = await getPages()
1515

1616
// Create Netlify Function for every page
17-
return pages.map(({ route }) => {
17+
return pages.reduce((jobs, { route }) => {
1818
const relativePath = getFilePathForRoute(route, 'js')
1919
const filePath = slash(join('pages', relativePath))
20-
return { type: 'function', filePath, functionsPath, isISR: true }
21-
})
20+
// Need two different functions - one ODB for normal pages, one standard for preview mode
21+
return [
22+
...jobs,
23+
{ type: 'function', filePath, functionsPath, isODB: true },
24+
{ type: 'function', filePath, functionsPath, forFallbackPreviewMode: true },
25+
]
26+
}, [])
2227
}
2328

2429
module.exports = setup

Diff for: src/lib/templates/getTemplate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const getTemplate = ({ filePath, isISR }) => {
2-
if (isISR) {
1+
const getTemplate = ({ filePath, isODB }) => {
2+
if (isODB) {
33
return `// Auto-generated file. DO NOT MODIFY.
44
const { getHandlerFunction } = require('./getHandlerFunction')
55
const { builder } = require('@netlify/functions')

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ exports[`Routing creates Netlify redirects 1`] = `
88
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
99
/_next/data/%BUILD_ID%/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
1010
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
11-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
12-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
13-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
14-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
15-
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/3.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
16-
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/4.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
11+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
12+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
13+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
14+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
15+
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/3.json /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
16+
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/4.json /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
1717
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
1818
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/2.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
1919
/api/hello-background /foo/api/hello-background 301!
@@ -31,17 +31,17 @@ exports[`Routing creates Netlify redirects 1`] = `
3131
/foo/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
3232
/foo/getStaticProps/static /getStaticProps/static 200
3333
/foo/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
34-
/foo/getStaticProps/withFallback/3 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
34+
/foo/getStaticProps/withFallback/3 /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
3535
/foo/getStaticProps/withFallback/3 /getStaticProps/withFallback/3 200
36-
/foo/getStaticProps/withFallback/4 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
36+
/foo/getStaticProps/withFallback/4 /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
3737
/foo/getStaticProps/withFallback/4 /getStaticProps/withFallback/4 200
38-
/foo/getStaticProps/withFallback/my/path/1 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
38+
/foo/getStaticProps/withFallback/my/path/1 /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
3939
/foo/getStaticProps/withFallback/my/path/1 /getStaticProps/withFallback/my/path/1 200
40-
/foo/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
40+
/foo/getStaticProps/withFallback/my/path/2 /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
4141
/foo/getStaticProps/withFallback/my/path/2 /getStaticProps/withFallback/my/path/2 200
42-
/foo/getStaticProps/withFallbackBlocking/3 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
42+
/foo/getStaticProps/withFallbackBlocking/3 /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
4343
/foo/getStaticProps/withFallbackBlocking/3 /getStaticProps/withFallbackBlocking/3 200
44-
/foo/getStaticProps/withFallbackBlocking/4 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
44+
/foo/getStaticProps/withFallbackBlocking/4 /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
4545
/foo/getStaticProps/withFallbackBlocking/4 /getStaticProps/withFallbackBlocking/4 200
4646
/foo/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
4747
/foo/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ exports[`Routing creates Netlify redirects 1`] = `
1616
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
1717
/_next/data/%BUILD_ID%/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
1818
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
19-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
20-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
21-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
22-
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
23-
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/3.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
24-
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/4.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
19+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
20+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
21+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
22+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
23+
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/3.json /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
24+
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/4.json /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
2525
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
2626
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/2.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
2727
/api/hello-background /.netlify/functions/next_api_hello-background 200
@@ -31,12 +31,12 @@ exports[`Routing creates Netlify redirects 1`] = `
3131
/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
3232
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
3333
/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
34-
/getStaticProps/withFallback/3 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
35-
/getStaticProps/withFallback/4 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
36-
/getStaticProps/withFallback/my/path/1 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
37-
/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
38-
/getStaticProps/withFallbackBlocking/3 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
39-
/getStaticProps/withFallbackBlocking/4 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
34+
/getStaticProps/withFallback/3 /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
35+
/getStaticProps/withFallback/4 /.netlify/functions/preview-next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
36+
/getStaticProps/withFallback/my/path/1 /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
37+
/getStaticProps/withFallback/my/path/2 /.netlify/functions/preview-next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
38+
/getStaticProps/withFallbackBlocking/3 /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
39+
/getStaticProps/withFallbackBlocking/4 /.netlify/functions/preview-next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
4040
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
4141
/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
4242
/_next/data/%BUILD_ID%/getServerSideProps/all.json /.netlify/functions/next_getServerSideProps_all_slug 200

0 commit comments

Comments
 (0)