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

Commit 9fbbaef

Browse files
committed
pr updates
1 parent 8461ee3 commit 9fbbaef

File tree

6 files changed

+57
-41
lines changed

6 files changed

+57
-41
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { copySync } = require("fs-extra");
1+
const { copySync, existsSync } = require("fs-extra");
22
const { join } = require("path");
33
const {
44
NEXT_DIST_DIR,
@@ -14,24 +14,25 @@ const setupNetlifyFunctionForPage = (filePath) => {
1414
const functionDirectory = join(NETLIFY_FUNCTIONS_PATH, functionName);
1515

1616
// Copy function template
17-
copySync(
18-
FUNCTION_TEMPLATE_PATH,
19-
join(functionDirectory, `${functionName}.js`),
20-
{
17+
const functionTemplateCopyPath = join(
18+
functionDirectory,
19+
`${functionName}.js`
20+
);
21+
if (!existsSync(functionTemplateCopyPath)) {
22+
copySync(FUNCTION_TEMPLATE_PATH, functionTemplateCopyPath, {
2123
overwrite: false,
2224
errorOnExist: true,
23-
}
24-
);
25+
});
26+
}
2527

2628
// Copy page
27-
copySync(
28-
join(NEXT_DIST_DIR, "serverless", filePath),
29-
join(functionDirectory, "nextJsPage.js"),
30-
{
29+
const nextPageCopyPath = join(functionDirectory, "nextJsPage.js");
30+
if (!existsSync(nextPageCopyPath)) {
31+
copySync(join(NEXT_DIST_DIR, "serverless", filePath), nextPageCopyPath, {
3132
overwrite: false,
3233
errorOnExist: true,
33-
}
34-
);
34+
});
35+
}
3536
};
3637

3738
module.exports = setupNetlifyFunctionForPage;

lib/pages/getStaticProps/pages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Object.entries(routes).forEach(
1515
pages.push({
1616
route,
1717
dataRoute,
18-
srcRoute
18+
srcRoute,
1919
});
2020
}
2121
);

lib/pages/getStaticProps/redirects.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
33
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
44
const pages = require("./pages");
55

6-
// Pages with getStaticProps should not need redirects, unless they are using
7-
// 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-
6+
// Pages with getStaticProps (without fallback or revalidation) only need
7+
// redirects for handling preview mode.
128
const redirects = [];
139

1410
pages.forEach(({ route, dataRoute, srcRoute }) => {
1511
const relativePath = getFilePathForRoute(srcRoute || route, "js");
1612
const filePath = join("pages", relativePath);
1713
const functionName = getNetlifyFunctionName(filePath);
1814

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-
});
15+
const conditions = ["Cookie=__prerender_bypass,__next_preview_data"];
16+
const target = `/.netlify/functions/${functionName}`;
17+
18+
if (route !== "/" && route !== "/static") {
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,
24+
force: true,
25+
conditions,
26+
});
2927

30-
// @finn - not sure this needs a date route redirect?
28+
// Add one redirect for the data route, same conditions
29+
redirects.push({
30+
route: dataRoute,
31+
target,
32+
force: true,
33+
conditions,
34+
});
35+
}
3136
});
3237

3338
module.exports = redirects;

lib/pages/getStaticProps/setup.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ const setup = () => {
2424
const jsonPath = getFilePathForRoute(route, "json");
2525
setupStaticFileForPage(jsonPath, dataRoute);
2626

27-
// Set up the Netlify function (this is ONLY for preview mode)
28-
if (srcRoute) {
29-
// is this taken care of in the other getStaticProps steps? unclear
30-
} else if (!srcRoute) {
31-
const relativePath = getFilePathForRoute(route, "js");
32-
const filePath = join("pages", relativePath);
33-
logItem(filePath);
34-
setupNetlifyFunctionForPage(filePath);
35-
}
27+
// // Set up the Netlify function (this is ONLY for preview mode)
28+
const relativePath = getFilePathForRoute(srcRoute || route, "js");
29+
const filePath = join("pages", relativePath);
30+
logItem(filePath);
31+
setupNetlifyFunctionForPage(filePath);
3632
});
3733
};
3834

lib/steps/setupRedirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const setupRedirects = () => {
4949
const redirectPieces = [
5050
netlifyRoute,
5151
target,
52-
`${statusCode || "200"}${force ? "!" : ""}`
52+
`${statusCode || "200"}${force ? "!" : ""}`,
5353
];
5454
if (conditions && conditions.length > 0) {
5555
redirectPieces.push(conditions.join(" "));

tests/__snapshots__/defaults.test.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ exports[`Routing creates Netlify redirects 1`] = `
77
/_next/data/%BUILD_ID%/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
88
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
99
/_next/data/%BUILD_ID%/getServerSideProps/:id.json /.netlify/functions/next_getServerSideProps_id 200
10+
/_next/data/%BUILD_ID%/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
11+
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
12+
/_next/data/%BUILD_ID%/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
1013
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
14+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
15+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
16+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
17+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
1118
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:id.json /.netlify/functions/next_getStaticProps_withFallback_id 200
1219
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
1320
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
@@ -20,7 +27,14 @@ exports[`Routing creates Netlify redirects 1`] = `
2027
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
2128
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
2229
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
30+
/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
31+
/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
32+
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
2333
/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
2438
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
2539
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
2640
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200

0 commit comments

Comments
 (0)