Skip to content

Commit fd7130f

Browse files
fix: use forced catchall for preview mode (#793)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 327f46a commit fd7130f

File tree

3 files changed

+6
-37
lines changed

3 files changed

+6
-37
lines changed

demo/pages/previewTest.js

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import Link from 'next/link'
22

3-
const StaticTest = ({ number }) => {
3+
const StaticTest = ({ isPreview }) => {
44
return (
55
<div>
6-
<p>
7-
This page uses getStaticProps() and is SSRed when in preview mode.
8-
<br />
9-
<br />
10-
By default, it shows the TV show by ID (as static HTML).
11-
<br />
12-
But when in preview mode, it shows person by ID instead (SSRed).
13-
</p>
146

15-
<hr />
16-
17-
<h1>Number: {number}</h1>
187

8+
<h1>Is preview? {isPreview ? "Yes!" : "No"}</h1>
9+
<p><a href={isPreview ? "/api/exitPreview" : "/api/enterPreview"}>{isPreview ? "Exit Preview" : "Enter Preview"}</a></p>
1910
<Link href="/">
2011
<a>Go back home</a>
2112
</Link>
@@ -24,20 +15,11 @@ const StaticTest = ({ number }) => {
2415
}
2516

2617
export const getStaticProps = async ({ preview }) => {
27-
let number
2818

29-
// In preview mode, use odd number
30-
if (preview) {
31-
number = 3
32-
}
33-
// In normal mode, use even number
34-
else {
35-
number = 4
36-
}
3719

3820
return {
3921
props: {
40-
number,
22+
isPreview: Boolean(preview),
4123
},
4224
}
4325
}

src/helpers/config.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,6 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
8080
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
8181
}
8282

83-
const staticManifest = join(netlifyConfig.build.publish, 'static-manifest.json')
84-
if (process.env.EXPERIMENTAL_MOVE_STATIC_PAGES && existsSync(staticManifest)) {
85-
// Static page files need to have a forced redirect for preview mode. Otherwise it's non-forced
86-
const staticFiles = await readJSON(staticManifest)
87-
netlifyConfig.redirects.push(
88-
...staticFiles.map((file) => ({
89-
from: `${basePath}/${file}`,
90-
to: HANDLER_FUNCTION_PATH,
91-
status: 200,
92-
force: true,
93-
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
94-
})),
95-
)
96-
}
97-
9883
// This is only used in prod, so dev uses `next dev` directly
9984
netlifyConfig.redirects.push(
10085
{ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 },
@@ -103,6 +88,7 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
10388
to: HANDLER_FUNCTION_PATH,
10489
status: 200,
10590
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
91+
force: true,
10692
},
10793
...redirects.map((redirect) => ({
10894
from: `${basePath}${redirect}`,

test/__snapshots__/index.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Array [
212212
"__next_preview_data",
213213
],
214214
},
215+
"force": true,
215216
"from": "/*",
216217
"status": 200,
217218
"to": "/.netlify/functions/___netlify-handler",

0 commit comments

Comments
 (0)