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

Commit 3fa286d

Browse files
committed
add custom headers test
1 parent 8aa84e3 commit 3fa286d

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

lib/steps/setupHeaders.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +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 = [
2223
`/*/_next/static/chunks/*`,
23-
`\n`,
24-
` `,
25-
`cache-control: public`,
26-
`\n`,
27-
` `,
28-
`cache-control: max-age=31536000`,
29-
`\n`,
30-
` `,
31-
`cache-control: immutable`,
24+
indentNewLine(`cache-control: public`),
25+
indentNewLine(`cache-control: max-age=31536000`),
26+
indentNewLine(`cache-control: immutable`),
3227
].join("");
3328
headers.push(staticChunkRule);
3429

tests/customHeaders.test.js

+51
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

+8
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)