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

Commit c0deace

Browse files
authoredJan 13, 2021
generate _headers file to override static chunks cache control (#141)
1 parent b4c85b9 commit c0deace

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed
 

‎index.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const copyNextAssets = require("./lib/steps/copyNextAssets");
44
const setupPages = require("./lib/steps/setupPages");
55
const setupImageFunction = require("./lib/steps/setupImageFunction");
66
const setupRedirects = require("./lib/steps/setupRedirects");
7+
const setupHeaders = require("./lib/steps/setupHeaders");
78
const {
89
NETLIFY_PUBLISH_PATH,
910
NETLIFY_FUNCTIONS_PATH,
@@ -30,6 +31,8 @@ const nextOnNetlify = (options = {}) => {
3031
setupImageFunction(functionsPath);
3132

3233
setupRedirects(publishPath);
34+
35+
setupHeaders(publishPath);
3336
};
3437

3538
module.exports = nextOnNetlify;

‎lib/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const FUNCTION_TEMPLATE_PATH = join(TEMPLATES_DIR, "netlifyFunction.js");
2828
// This is the file where custom redirects can be configured
2929
const CUSTOM_REDIRECTS_PATH = join(".", "_redirects");
3030

31+
// This is the file where custom headers can be configured
32+
const CUSTOM_HEADERS_PATH = join(".", "_headers");
33+
3134
// This is the name used for copying our imageFunction template and for
3235
// creating the next/image redirect
3336
const NEXT_IMAGE_FUNCTION_NAME = "next_image";
@@ -41,5 +44,6 @@ module.exports = {
4144
TEMPLATES_DIR,
4245
FUNCTION_TEMPLATE_PATH,
4346
CUSTOM_REDIRECTS_PATH,
47+
CUSTOM_HEADERS_PATH,
4448
NEXT_IMAGE_FUNCTION_NAME,
4549
};

‎lib/steps/setupHeaders.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { join } = require("path");
2+
const { existsSync, readFileSync, writeFileSync } = require("fs-extra");
3+
const { logTitle, logItem } = require("../helpers/logger");
4+
const { CUSTOM_HEADERS_PATH } = require("../config");
5+
6+
// Setup _headers file to override specific header rules for next assets
7+
const setupHeaders = (publishPath) => {
8+
logTitle("🔀 Setting up headers");
9+
10+
// Collect custom redirects defined by the user
11+
const headers = [];
12+
if (existsSync(CUSTOM_HEADERS_PATH)) {
13+
logItem("# Prepending custom headers");
14+
headers.push(readFileSync(CUSTOM_HEADERS_PATH, "utf8"));
15+
}
16+
17+
// Add NoN section heading
18+
headers.push("# Next-on-Netlify Headers");
19+
20+
// Add rule to override cache control for static chunks
21+
const staticChunkRule = [
22+
`/_next/static/chunks/*:`,
23+
`\n`,
24+
` `,
25+
`cache-control: public,max-age=31536000,immutable`,
26+
].join("");
27+
headers.push(staticChunkRule);
28+
29+
// Write redirects to _redirects file
30+
writeFileSync(join(publishPath, "_headers"), headers.join("\n"));
31+
};
32+
33+
module.exports = setupHeaders;

‎tests/__snapshots__/defaults.test.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Headers creates Netlify headers 1`] = `
4+
"# Next-on-Netlify Headers
5+
/_next/static/chunks/*:
6+
cache-control: public,max-age=31536000,immutable"
7+
`;
8+
39
exports[`Routing creates Netlify redirects 1`] = `
410
"# Next-on-Netlify Redirects
511
/ /.netlify/functions/next_index 200

‎tests/defaults.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,16 @@ describe("Routing", () => {
317317
expect(redirects).toMatchSnapshot();
318318
});
319319
});
320+
321+
describe("Headers", () => {
322+
test("creates Netlify headers", async () => {
323+
// Read _headers file
324+
const contents = readFileSync(
325+
join(PROJECT_PATH, "out_publish", "_headers")
326+
);
327+
let headers = contents.toString();
328+
329+
// Check that headers match
330+
expect(headers).toMatchSnapshot();
331+
});
332+
});

0 commit comments

Comments
 (0)