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

Commit cd1cf9e

Browse files
fix incorrect _headers syntax & broken local cypress (#144)
* fix incorrect _headers syntax & broken local cypress * add custom headers test
1 parent dd70dc9 commit cd1cf9e

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

cypress/plugins/deployProject.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { removeSync } = require("fs-extra");
12
const waitOn = require("wait-on");
23
const execa = require("execa");
34
const { join } = require("path");
@@ -7,6 +8,9 @@ const getBaseUrl = require("./getBaseUrl");
78
const deployLocally = ({ project }, config) => {
89
process.stdout.write(`Deploying project: ${project}...`);
910

11+
// _headers breaks netlify dev
12+
removeSync(join(config.buildsFolder, project, "out_publish", "_headers"));
13+
1014
// Start server. Must start in detached mode, so that we can kill it later.
1115
// Otherwise, we seem unable to kill it.
1216
// See: https://medium.com/@almenon214/killing-processes-with-node-772ffdd19aad

lib/steps/setupHeaders.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ const setupHeaders = (publishPath) => {
1818
headers.push("# Next-on-Netlify Headers");
1919

2020
// Add rule to override cache control for static chunks
21+
const indentNewLine = (s) => `\n ${s}`;
2122
const staticChunkRule = [
22-
`/_next/static/chunks/*:`,
23-
`\n`,
24-
` `,
25-
`cache-control: public,max-age=31536000,immutable`,
23+
`/*/_next/static/chunks/*`,
24+
indentNewLine(`cache-control: public`),
25+
indentNewLine(`cache-control: max-age=31536000`),
26+
indentNewLine(`cache-control: immutable`),
2627
].join("");
2728
headers.push(staticChunkRule);
2829

tests/__snapshots__/defaults.test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
exports[`Headers creates Netlify headers 1`] = `
44
"# Next-on-Netlify Headers
5-
/_next/static/chunks/*:
6-
cache-control: public,max-age=31536000,immutable"
5+
/*/_next/static/chunks/*
6+
cache-control: public
7+
cache-control: max-age=31536000
8+
cache-control: immutable"
79
`;
810

911
exports[`Routing creates Netlify redirects 1`] = `

tests/customHeaders.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Test next-on-netlify when a custom distDir is set in next.config.js
2+
3+
const { EOL } = require("os");
4+
const { parse, join } = require("path");
5+
const { readFileSync } = require("fs-extra");
6+
const buildNextApp = require("./helpers/buildNextApp");
7+
8+
// The name of this test file (without extension)
9+
const FILENAME = parse(__filename).name;
10+
11+
// The directory which will be used for testing.
12+
// We simulate a NextJS app within that directory, with pages, and a
13+
// package.json file.
14+
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
15+
16+
// Capture the output to verify successful build
17+
let buildOutput;
18+
19+
beforeAll(
20+
async () => {
21+
buildOutput = await buildNextApp()
22+
.forTest(__filename)
23+
.withPages("pages-with-static-props-index")
24+
.withNextConfig("next.config.js")
25+
.withPackageJson("package.json")
26+
.withFile("_headers")
27+
.build();
28+
},
29+
// time out after 180 seconds
30+
180 * 1000
31+
);
32+
33+
describe("next-on-netlify", () => {
34+
test("builds successfully", () => {
35+
expect(buildOutput).toMatch("Next on Netlify");
36+
expect(buildOutput).toMatch("Success! All done!");
37+
});
38+
});
39+
40+
describe("Headers", () => {
41+
test("includes custom header rules", async () => {
42+
// Read _redirects file
43+
const contents = readFileSync(
44+
join(PROJECT_PATH, "out_publish", "_headers"),
45+
"utf8"
46+
);
47+
48+
const headers = contents.trim().split(EOL);
49+
expect(headers[0]).toEqual("/templates/index.html");
50+
});
51+
});

tests/fixtures/_headers

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/templates/index.html
2+
# headers for that path:
3+
X-Frame-Options: DENY
4+
X-XSS-Protection: 1; mode=block
5+
6+
/templates/index2.html
7+
# headers for that path:
8+
X-Frame-Options: SAMEORIGIN

0 commit comments

Comments
 (0)