Skip to content

fix: use forced catchall for preview mode #793

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 4 commits into from
Nov 15, 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
26 changes: 4 additions & 22 deletions demo/pages/previewTest.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import Link from 'next/link'

const StaticTest = ({ number }) => {
const StaticTest = ({ isPreview }) => {
return (
<div>
<p>
This page uses getStaticProps() and is SSRed when in preview mode.
<br />
<br />
By default, it shows the TV show by ID (as static HTML).
<br />
But when in preview mode, it shows person by ID instead (SSRed).
</p>

<hr />

<h1>Number: {number}</h1>

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

export const getStaticProps = async ({ preview }) => {
let number

// In preview mode, use odd number
if (preview) {
number = 3
}
// In normal mode, use even number
else {
number = 4
}

return {
props: {
number,
isPreview: Boolean(preview),
},
}
}
Expand Down
16 changes: 1 addition & 15 deletions src/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,6 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
}

const staticManifest = join(netlifyConfig.build.publish, 'static-manifest.json')
if (process.env.EXPERIMENTAL_MOVE_STATIC_PAGES && existsSync(staticManifest)) {
// Static page files need to have a forced redirect for preview mode. Otherwise it's non-forced
const staticFiles = await readJSON(staticManifest)
netlifyConfig.redirects.push(
...staticFiles.map((file) => ({
from: `${basePath}/${file}`,
to: HANDLER_FUNCTION_PATH,
status: 200,
force: true,
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
})),
)
}

// This is only used in prod, so dev uses `next dev` directly
netlifyConfig.redirects.push(
{ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 },
Expand All @@ -103,6 +88,7 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
to: HANDLER_FUNCTION_PATH,
status: 200,
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
force: true,
},
...redirects.map((redirect) => ({
from: `${basePath}${redirect}`,
Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Array [
"__next_preview_data",
],
},
"force": true,
"from": "/*",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
Expand Down