From 8aa84e31149dfc22e6909dca3c101889764a969a Mon Sep 17 00:00:00 2001 From: Lindsay Levine Date: Sun, 17 Jan 2021 01:31:07 -0500 Subject: [PATCH 1/2] fix incorrect _headers syntax & broken local cypress --- cypress/plugins/deployProject.js | 4 ++++ lib/steps/setupHeaders.js | 10 ++++++++-- tests/__snapshots__/defaults.test.js.snap | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cypress/plugins/deployProject.js b/cypress/plugins/deployProject.js index bc9ff39..95f0141 100644 --- a/cypress/plugins/deployProject.js +++ b/cypress/plugins/deployProject.js @@ -1,3 +1,4 @@ +const { removeSync } = require("fs-extra"); const waitOn = require("wait-on"); const execa = require("execa"); const { join } = require("path"); @@ -7,6 +8,9 @@ const getBaseUrl = require("./getBaseUrl"); const deployLocally = ({ project }, config) => { process.stdout.write(`Deploying project: ${project}...`); + // _headers breaks netlify dev + removeSync(join(config.buildsFolder, project, "out_publish", "_headers")); + // Start server. Must start in detached mode, so that we can kill it later. // Otherwise, we seem unable to kill it. // See: https://medium.com/@almenon214/killing-processes-with-node-772ffdd19aad diff --git a/lib/steps/setupHeaders.js b/lib/steps/setupHeaders.js index c30319d..9fae42c 100644 --- a/lib/steps/setupHeaders.js +++ b/lib/steps/setupHeaders.js @@ -19,10 +19,16 @@ const setupHeaders = (publishPath) => { // Add rule to override cache control for static chunks const staticChunkRule = [ - `/_next/static/chunks/*:`, + `/*/_next/static/chunks/*`, `\n`, ` `, - `cache-control: public,max-age=31536000,immutable`, + `cache-control: public`, + `\n`, + ` `, + `cache-control: max-age=31536000`, + `\n`, + ` `, + `cache-control: immutable`, ].join(""); headers.push(staticChunkRule); diff --git a/tests/__snapshots__/defaults.test.js.snap b/tests/__snapshots__/defaults.test.js.snap index 70a5665..65b473f 100644 --- a/tests/__snapshots__/defaults.test.js.snap +++ b/tests/__snapshots__/defaults.test.js.snap @@ -2,8 +2,10 @@ exports[`Headers creates Netlify headers 1`] = ` "# Next-on-Netlify Headers -/_next/static/chunks/*: - cache-control: public,max-age=31536000,immutable" +/*/_next/static/chunks/* + cache-control: public + cache-control: max-age=31536000 + cache-control: immutable" `; exports[`Routing creates Netlify redirects 1`] = ` From 3fa286df3957219ab1f95cb080a7e0509886d458 Mon Sep 17 00:00:00 2001 From: Lindsay Levine Date: Sun, 17 Jan 2021 01:39:40 -0500 Subject: [PATCH 2/2] add custom headers test --- lib/steps/setupHeaders.js | 13 +++------- tests/customHeaders.test.js | 51 +++++++++++++++++++++++++++++++++++++ tests/fixtures/_headers | 8 ++++++ 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 tests/customHeaders.test.js create mode 100644 tests/fixtures/_headers diff --git a/lib/steps/setupHeaders.js b/lib/steps/setupHeaders.js index 9fae42c..ed20a95 100644 --- a/lib/steps/setupHeaders.js +++ b/lib/steps/setupHeaders.js @@ -18,17 +18,12 @@ const setupHeaders = (publishPath) => { headers.push("# Next-on-Netlify Headers"); // Add rule to override cache control for static chunks + const indentNewLine = (s) => `\n ${s}`; const staticChunkRule = [ `/*/_next/static/chunks/*`, - `\n`, - ` `, - `cache-control: public`, - `\n`, - ` `, - `cache-control: max-age=31536000`, - `\n`, - ` `, - `cache-control: immutable`, + indentNewLine(`cache-control: public`), + indentNewLine(`cache-control: max-age=31536000`), + indentNewLine(`cache-control: immutable`), ].join(""); headers.push(staticChunkRule); diff --git a/tests/customHeaders.test.js b/tests/customHeaders.test.js new file mode 100644 index 0000000..0eb9ac4 --- /dev/null +++ b/tests/customHeaders.test.js @@ -0,0 +1,51 @@ +// Test next-on-netlify when a custom distDir is set in next.config.js + +const { EOL } = require("os"); +const { parse, join } = require("path"); +const { readFileSync } = require("fs-extra"); +const buildNextApp = require("./helpers/buildNextApp"); + +// The name of this test file (without extension) +const FILENAME = parse(__filename).name; + +// The directory which will be used for testing. +// We simulate a NextJS app within that directory, with pages, and a +// package.json file. +const PROJECT_PATH = join(__dirname, "builds", FILENAME); + +// Capture the output to verify successful build +let buildOutput; + +beforeAll( + async () => { + buildOutput = await buildNextApp() + .forTest(__filename) + .withPages("pages-with-static-props-index") + .withNextConfig("next.config.js") + .withPackageJson("package.json") + .withFile("_headers") + .build(); + }, + // time out after 180 seconds + 180 * 1000 +); + +describe("next-on-netlify", () => { + test("builds successfully", () => { + expect(buildOutput).toMatch("Next on Netlify"); + expect(buildOutput).toMatch("Success! All done!"); + }); +}); + +describe("Headers", () => { + test("includes custom header rules", async () => { + // Read _redirects file + const contents = readFileSync( + join(PROJECT_PATH, "out_publish", "_headers"), + "utf8" + ); + + const headers = contents.trim().split(EOL); + expect(headers[0]).toEqual("/templates/index.html"); + }); +}); diff --git a/tests/fixtures/_headers b/tests/fixtures/_headers new file mode 100644 index 0000000..c592167 --- /dev/null +++ b/tests/fixtures/_headers @@ -0,0 +1,8 @@ +/templates/index.html + # headers for that path: + X-Frame-Options: DENY + X-XSS-Protection: 1; mode=block + +/templates/index2.html + # headers for that path: + X-Frame-Options: SAMEORIGIN