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

Commit

Permalink
pr updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsaylevine committed Oct 14, 2020
1 parent 8461ee3 commit 7f6a0eb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 43 deletions.
27 changes: 14 additions & 13 deletions lib/helpers/setupNetlifyFunctionForPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { copySync } = require("fs-extra");
const { copySync, existsSync } = require("fs-extra");
const { join } = require("path");
const {
NEXT_DIST_DIR,
Expand All @@ -14,24 +14,25 @@ const setupNetlifyFunctionForPage = (filePath) => {
const functionDirectory = join(NETLIFY_FUNCTIONS_PATH, functionName);

// Copy function template
copySync(
FUNCTION_TEMPLATE_PATH,
join(functionDirectory, `${functionName}.js`),
{
const functionTemplateCopyPath = join(
functionDirectory,
`${functionName}.js`
);
if (!existsSync(functionTemplateCopyPath)) {
copySync(FUNCTION_TEMPLATE_PATH, functionTemplateCopyPath, {
overwrite: false,
errorOnExist: true,
}
);
});
}

// Copy page
copySync(
join(NEXT_DIST_DIR, "serverless", filePath),
join(functionDirectory, "nextJsPage.js"),
{
const nextPageCopyPath = join(functionDirectory, "nextJsPage.js");
if (!existsSync(nextPageCopyPath)) {
copySync(join(NEXT_DIST_DIR, "serverless", filePath), nextPageCopyPath, {
overwrite: false,
errorOnExist: true,
}
);
});
}
};

module.exports = setupNetlifyFunctionForPage;
2 changes: 1 addition & 1 deletion lib/pages/getStaticProps/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Object.entries(routes).forEach(
pages.push({
route,
dataRoute,
srcRoute
srcRoute,
});
}
);
Expand Down
39 changes: 22 additions & 17 deletions lib/pages/getStaticProps/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
const pages = require("./pages");

// Pages with getStaticProps should not need redirects, unless they are using
// fallback: true or a revalidation interval. Both are handled by other files.
// However, we now support preview mode for pre-rendered pages (without fallback
// or revalidation), so we create redirects specifically for when preview mode
// is enabled.

// Pages with getStaticProps (without fallback or revalidation) only need
// redirects for handling preview mode.
const redirects = [];

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

// Add one redirect for the page, but only when the NextJS
// preview mode cookies are present
redirects.push({
route,
target: `/.netlify/functions/${functionName}`,
force: true,
conditions: [
"Cookie=__prerender_bypass,__next_preview_data"
]
});
const conditions = ["Cookie=__prerender_bypass,__next_preview_data"];
const target = `/.netlify/functions/${functionName}`;

if (route !== "/" && route !== "/static") {
// Add one redirect for the page, but only when the NextJS
// preview mode cookies are present
redirects.push({
route,
target,
force: true,
conditions,
});

// @finn - not sure this needs a date route redirect?
// Add one redirect for the data route, same conditions
redirects.push({
route: dataRoute,
target,
force: true,
conditions,
});
}
});

module.exports = redirects;
14 changes: 5 additions & 9 deletions lib/pages/getStaticProps/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ const setup = () => {
const jsonPath = getFilePathForRoute(route, "json");
setupStaticFileForPage(jsonPath, dataRoute);

// Set up the Netlify function (this is ONLY for preview mode)
if (srcRoute) {
// is this taken care of in the other getStaticProps steps? unclear
} else if (!srcRoute) {
const relativePath = getFilePathForRoute(route, "js");
const filePath = join("pages", relativePath);
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
}
// // Set up the Netlify function (this is ONLY for preview mode)
const relativePath = getFilePathForRoute(srcRoute || route, "js");
const filePath = join("pages", relativePath);
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
});
};

Expand Down
11 changes: 8 additions & 3 deletions lib/steps/setupRedirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ const setupRedirects = () => {
// One route may map to multiple Netlify routes: e.g., catch-all pages
// require two Netlify routes in the _redirects file
getNetlifyRoutes(route).map((netlifyRoute) => {
const { conditions, force, statusCode, target } = nextRedirect;
const {
conditions = [],
force = false,
statusCode = "200",
target,
} = nextRedirect;
const redirectPieces = [
netlifyRoute,
target,
`${statusCode || "200"}${force ? "!" : ""}`
`${statusCode}${force ? "!" : ""}`,
];
if (conditions && conditions.length > 0) {
if (conditions.length > 0) {
redirectPieces.push(conditions.join(" "));
}
const redirect = redirectPieces.join(" ");
Expand Down
14 changes: 14 additions & 0 deletions tests/__snapshots__/defaults.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ exports[`Routing creates Netlify redirects 1`] = `
/_next/data/%BUILD_ID%/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
/_next/data/%BUILD_ID%/getServerSideProps/:id.json /.netlify/functions/next_getServerSideProps_id 200
/_next/data/%BUILD_ID%/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
/_next/data/%BUILD_ID%/getStaticProps/withFallback/3.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/withFallback/4.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/1.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/withFallback/my/path/2.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:id.json /.netlify/functions/next_getStaticProps_withFallback_id 200
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
Expand All @@ -20,7 +27,14 @@ exports[`Routing creates Netlify redirects 1`] = `
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
/getStaticProps/withFallback/3 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/4 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/my/path/1 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
Expand Down

0 comments on commit 7f6a0eb

Please sign in to comment.