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

generate _headers file to override static chunks cache control #141

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const copyNextAssets = require("./lib/steps/copyNextAssets");
const setupPages = require("./lib/steps/setupPages");
const setupImageFunction = require("./lib/steps/setupImageFunction");
const setupRedirects = require("./lib/steps/setupRedirects");
const setupHeaders = require("./lib/steps/setupHeaders");
const {
NETLIFY_PUBLISH_PATH,
NETLIFY_FUNCTIONS_PATH,
Expand All @@ -30,6 +31,8 @@ const nextOnNetlify = (options = {}) => {
setupImageFunction(functionsPath);

setupRedirects(publishPath);

setupHeaders(publishPath);
};

module.exports = nextOnNetlify;
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const FUNCTION_TEMPLATE_PATH = join(TEMPLATES_DIR, "netlifyFunction.js");
// This is the file where custom redirects can be configured
const CUSTOM_REDIRECTS_PATH = join(".", "_redirects");

// This is the file where custom headers can be configured
const CUSTOM_HEADERS_PATH = join(".", "_headers");

// This is the name used for copying our imageFunction template and for
// creating the next/image redirect
const NEXT_IMAGE_FUNCTION_NAME = "next_image";
Expand All @@ -41,5 +44,6 @@ module.exports = {
TEMPLATES_DIR,
FUNCTION_TEMPLATE_PATH,
CUSTOM_REDIRECTS_PATH,
CUSTOM_HEADERS_PATH,
NEXT_IMAGE_FUNCTION_NAME,
};
33 changes: 33 additions & 0 deletions lib/steps/setupHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { join } = require("path");
const { existsSync, readFileSync, writeFileSync } = require("fs-extra");
const { logTitle, logItem } = require("../helpers/logger");
const { CUSTOM_HEADERS_PATH } = require("../config");

// Setup _headers file to override specific header rules for next assets
const setupHeaders = (publishPath) => {
logTitle("🔀 Setting up headers");

// Collect custom redirects defined by the user
const headers = [];
if (existsSync(CUSTOM_HEADERS_PATH)) {
logItem("# Prepending custom headers");
headers.push(readFileSync(CUSTOM_HEADERS_PATH, "utf8"));
}

// Add NoN section heading
headers.push("# Next-on-Netlify Headers");

// Add rule to override cache control for static chunks
const staticChunkRule = [
`/_next/static/chunks/*:`,
`\n`,
` `,
`cache-control: public,max-age=31536000,immutable`,
].join("");
headers.push(staticChunkRule);

// Write redirects to _redirects file
writeFileSync(join(publishPath, "_headers"), headers.join("\n"));
};

module.exports = setupHeaders;
6 changes: 6 additions & 0 deletions tests/__snapshots__/defaults.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Headers creates Netlify headers 1`] = `
"# Next-on-Netlify Headers
/_next/static/chunks/*:
cache-control: public,max-age=31536000,immutable"
`;

exports[`Routing creates Netlify redirects 1`] = `
"# Next-on-Netlify Redirects
/ /.netlify/functions/next_index 200
Expand Down
13 changes: 13 additions & 0 deletions tests/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,16 @@ describe("Routing", () => {
expect(redirects).toMatchSnapshot();
});
});

describe("Headers", () => {
test("creates Netlify headers", async () => {
// Read _headers file
const contents = readFileSync(
join(PROJECT_PATH, "out_publish", "_headers")
);
let headers = contents.toString();

// Check that headers match
expect(headers).toMatchSnapshot();
});
});