Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit a692cce

Browse files
committed
add support for preview mode of pre-rendered pages using getStaticProps
1 parent 954c4a8 commit a692cce

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

lib/pages/getStaticProps/redirects.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
const { join } = require("path");
2+
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
3+
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
14
const pages = require("./pages");
25

3-
// Pages with getStaticProps do not need redirects, unless they are using
6+
// Pages with getStaticProps should not need redirects, unless they are using
47
// fallback: true or a revalidation interval. Both are handled by other files.
8+
// However, we now support preview mode for pre-rendered pages (without fallback
9+
// or revalidation), so we create redirects specifically for when preview mode
10+
// is enabled.
11+
512
const redirects = [];
613

14+
pages.forEach(({ route, dataRoute }) => {
15+
const relativePath = getFilePathForRoute(route, "js");
16+
const filePath = join("pages", relativePath);
17+
const functionName = getNetlifyFunctionName(filePath);
18+
19+
// Add one redirect for the page, but only when the NextJS
20+
// preview mode cookies are present
21+
redirects.push({
22+
route,
23+
target: `/.netlify/functions/${functionName}`,
24+
force: true,
25+
conditions: [
26+
"Cookie=__prerender_bypass,__next_preview_data"
27+
]
28+
});
29+
30+
// @finn - not sure this needs a date route redirect?
31+
});
32+
733
module.exports = redirects;

lib/pages/getStaticProps/setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const { join } = require("path");
12
const { logTitle, logItem } = require("../../helpers/logger");
23
const { NETLIFY_PUBLISH_PATH } = require("../../config");
34
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
45
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
6+
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
57
const pages = require("./pages");
68

79
// Copy pre-rendered SSG pages
@@ -21,6 +23,12 @@ const setup = () => {
2123
// Copy page's JSON data
2224
const jsonPath = getFilePathForRoute(route, "json");
2325
setupStaticFileForPage(jsonPath, dataRoute);
26+
27+
// Set up the Netlify function (this is ONLY for preview mode)
28+
const relativePath = getFilePathForRoute(route, "js");
29+
const filePath = join("pages", relativePath);
30+
logItem(filePath);
31+
setupNetlifyFunctionForPage(filePath);
2432
});
2533
};
2634

lib/steps/setupRedirects.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ const setupRedirects = () => {
4545
// One route may map to multiple Netlify routes: e.g., catch-all pages
4646
// require two Netlify routes in the _redirects file
4747
getNetlifyRoutes(route).map((netlifyRoute) => {
48-
const redirect = `${netlifyRoute} ${nextRedirect.target} 200`;
48+
const { conditions, force, statusCode, target } = nextRedirect;
49+
const redirectPieces = [
50+
netlifyRoute,
51+
target,
52+
`${statusCode || "200"}${force ? "!" : ""}`
53+
];
54+
if (conditions && conditions.length > 0) {
55+
redirectPieces.push(conditions.join(" "));
56+
}
57+
const redirect = redirectPieces.join(" ");
4958
logItem(redirect);
5059
redirects.push(redirect);
5160
});

lib/templates/netlifyFunction.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const callbackHandler = (callback) =>
1919
].map((value) => String(value));
2020
});
2121

22+
// NextJS by default sets Cache-Control to the max age. Without this
23+
// override, the Netlify function will cache the very first page render.
24+
// TO-DO: set this *only* for SSG'd pages
25+
response.multiValueHeaders["cache-control"] = ["no-cache"];
26+
2227
// Invoke callback
2328
callback(argument, response);
2429
};

0 commit comments

Comments
 (0)