diff --git a/.eslintrc.js b/.eslintrc.js index 1f5fb72e..0e206fbe 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,7 +12,6 @@ module.exports = { 'no-param-reassign': 0, 'no-promise-executor-return': 0, 'no-prototype-builtins': 0, - 'no-unused-vars': ['error', { ignoreRestSiblings: true }], 'unicorn/filename-case': 0, 'unicorn/numeric-separators-style': 0, 'unicorn/no-empty-file': 0, diff --git a/CHANGELOG.md b/CHANGELOG.md index 13d8311f..b0966a57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [5.0.0](https://www.github.com/netlify/gatsby-plugin-netlify/compare/v4.4.0...v5.0.0) (2022-05-24) + + +### ⚠ BREAKING CHANGES + +* Remove script preloading (#134) + +### Features + +* Remove script preloading ([#134](https://www.github.com/netlify/gatsby-plugin-netlify/issues/134)) ([5a0397a](https://www.github.com/netlify/gatsby-plugin-netlify/commit/5a0397a8c80914070de03445538d7449ca085cad)) + ## [4.4.0](https://www.github.com/netlify/gatsby-plugin-netlify/compare/v4.3.1...v4.4.0) (2022-05-18) diff --git a/README.md b/README.md index 559bf91d..15ae8f52 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ plugins: [ headers: {}, // option to add more headers. `Link` headers are transformed by the below criteria allPageHeaders: [], // option to add headers for all pages. `Link` headers are transformed by the below criteria mergeSecurityHeaders: true, // boolean to turn off the default security headers - mergeLinkHeaders: true, // boolean to turn off the default gatsby js headers mergeCachingHeaders: true, // boolean to turn off the default caching headers transformHeaders: (headers, path) => headers, // optional transform for manipulating headers under each path (e.g.sorting), etc. generateMatchPathRewrites: true, // boolean to turn off automatic creation of redirect rules for client only paths @@ -76,28 +75,6 @@ file hash), the plugin will transform any base file names to the hashed variants ensure the path is valid relative to the output `public` folder. You should be able to reference assets imported through javascript in the `static` folder. -When `mergeLinkHeaders` is true, as it is by default, this plugin will generate HTTP preload headers for the asset paths -for all of your application's pages. - -An example: - -``` -/my-page - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -``` - -Therefore, expect the size of the `_headers` file to grow linearly with the number of pages in your application. - -> **Note:** Gatsby also adds these preload tags in your pages' index.html files, whether or not you are using this -> plugin. - Do not specify the public path in the config, as the plugin will provide it for you. The Netlify `_headers` file does not inherit headers, and it will replace any matching headers it finds in more specific diff --git a/package.json b/package.json index 3744411a..94f0b87b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gatsby-plugin-netlify", "description": "A Gatsby plugin for sites deployed to Netlify", - "version": "4.4.0", + "version": "5.0.0", "author": "Kyle Mathews ", "bugs": { "url": "https://github.com/netlify/gatsby-plugin-netlify/issues" @@ -17,7 +17,7 @@ "fs-extra": "^10.0.0", "gatsby-core-utils": "^3.5.2", "kebab-hash": "^0.1.2", - "lodash": "^4.17.21", + "lodash.mergewith": "^4.6.2", "webpack-assets-manifest": "^5.0.6" }, "devDependencies": { diff --git a/src/__tests__/__snapshots__/build-headers-program.ts.snap b/src/__tests__/__snapshots__/build-headers-program.ts.snap index 109968fd..17c2d33d 100644 --- a/src/__tests__/__snapshots__/build-headers-program.ts.snap +++ b/src/__tests__/__snapshots__/build-headers-program.ts.snap @@ -1,93 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`build-headers-program with caching headers 1`] = ` -"## Created with gatsby-plugin-netlify - -/* - X-Frame-Options: DENY - X-XSS-Protection: 1; mode=block - X-Content-Type-Options: nosniff - Referrer-Policy: same-origin -/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js - Cache-Control: public, max-age=31536000, immutable -/0-0180cd94ef2497ac7db8.js - Cache-Control: public, max-age=31536000, immutable -/component---src-templates-blog-post-js-517987eae96e75cddbe7.js - Cache-Control: public, max-age=31536000, immutable -/component---src-pages-404-js-53e6c51a5a7e73090f50.js - Cache-Control: public, max-age=31536000, immutable -/component---src-pages-index-js-0bdd01c77ee09ef0224c.js - Cache-Control: public, max-age=31536000, immutable -/webpack-runtime-acaa8994f1f704475e21.js - Cache-Control: public, max-age=31536000, immutable -/styles.1025963f4f2ec7abbad4.css - Cache-Control: public, max-age=31536000, immutable -/styles-565f081c8374bbda155f.js - Cache-Control: public, max-age=31536000, immutable -/app-f33c13590352da20930f.js - Cache-Control: public, max-age=31536000, immutable -/static/* - Cache-Control: public, max-age=31536000, immutable -/sw.js - Cache-Control: no-cache -/offline-plugin-app-shell-fallback/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hi-folks/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/my-second-post/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hello-world/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404.html - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -" -`; - exports[`build-headers-program with manifest['pages-manifest'] 1`] = ` "## Created with gatsby-plugin-netlify @@ -120,47 +32,6 @@ exports[`build-headers-program with manifest['pages-manifest'] 1`] = ` Cache-Control: public, max-age=31536000, immutable /sw.js Cache-Control: no-cache -/offline-plugin-app-shell-fallback/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/hi-folks/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/my-second-post/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/hello-world/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/404/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script -/404.html - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script " `; @@ -197,61 +68,6 @@ exports[`build-headers-program with security headers 1`] = ` Cache-Control: public, max-age=31536000, immutable /sw.js Cache-Control: no-cache -/offline-plugin-app-shell-fallback/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hi-folks/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/my-second-post/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hello-world/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404.html - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin " `; @@ -263,60 +79,5 @@ exports[`build-headers-program without caching headers 1`] = ` X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Referrer-Policy: same-origin -/offline-plugin-app-shell-fallback/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hi-folks/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/my-second-post/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/hello-world/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/ - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin -/404.html - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=script - Link: ; rel=preload; as=fetch; crossorigin - Link: ; rel=preload; as=fetch; crossorigin " `; diff --git a/src/__tests__/build-headers-program.ts b/src/__tests__/build-headers-program.ts index 5b680bcb..19901ad9 100644 --- a/src/__tests__/build-headers-program.ts +++ b/src/__tests__/build-headers-program.ts @@ -24,25 +24,6 @@ describe(`build-headers-program`, () => { existsSync.mockReturnValue(true) }) - it(`with caching headers`, async () => { - const pluginData = await createPluginData() - - const pluginOptions = { - ...DEFAULT_OPTIONS, - mergeCachingHeaders: true, - } - - await buildHeadersProgram(pluginData, pluginOptions, reporter) - - expect(reporter.panic).not.toHaveBeenCalled() - const output = await readFile(pluginData.publicFolder(`_headers`), `utf8`) - expect(output).toMatchSnapshot() - expect(output).toMatch(/app-data\.json/) - expect(output).toMatch(/page-data\.json/) - // we should only check page-data & app-data once which leads to 2 times - expect(existsSync).toBeCalledTimes(2) - }) - it(`with manifest['pages-manifest']`, async () => { const pluginData = await createPluginData() @@ -62,8 +43,6 @@ describe(`build-headers-program`, () => { const output = await readFile(pluginData.publicFolder(`_headers`), `utf8`) expect(output).toMatchSnapshot() expect(output).toMatch(/\/pages-manifest-ab11f09e0ca7ecd3b43e\.js/g) - expect(output).not.toMatch(/\/app-data\.json/g) - expect(output).not.toMatch(/\/page-data\.json/g) expect(output).not.toMatch(/\/undefined/g) }) diff --git a/src/__tests__/gatsby-node.ts b/src/__tests__/gatsby-node.ts index f90d65e8..4b885e61 100644 --- a/src/__tests__/gatsby-node.ts +++ b/src/__tests__/gatsby-node.ts @@ -32,7 +32,6 @@ describe(`gatsby-node.js`, () => { `"headers" must be of type object`, `"allPageHeaders" must be an array`, `"mergeSecurityHeaders" must be a boolean`, - `"mergeLinkHeaders" must be a boolean`, `"mergeCachingHeaders" must be a boolean`, `"transformHeaders" must have an arity lesser or equal to 2`, `"generateMatchPathRewrites" must be a boolean`, @@ -42,7 +41,6 @@ describe(`gatsby-node.js`, () => { headers: `this should be an object`, allPageHeaders: `this should be an array`, mergeSecurityHeaders: `this should be a boolean`, - mergeLinkHeaders: `this should be a boolean`, mergeCachingHeaders: `this should be a boolean`, transformHeaders: (too, many, args) => [too, many, args], generateMatchPathRewrites: `this should be a boolean`, @@ -59,7 +57,6 @@ describe(`gatsby-node.js`, () => { }, allPageHeaders: [`First header`, `Second header`], mergeSecurityHeaders: true, - mergeLinkHeaders: false, mergeCachingHeaders: true, transformHeaders: () => null, generateMatchPathRewrites: false, diff --git a/src/build-headers-program.ts b/src/build-headers-program.ts index a7709696..84f1ed6c 100644 --- a/src/build-headers-program.ts +++ b/src/build-headers-program.ts @@ -1,20 +1,16 @@ /* eslint-disable max-lines */ -import { parse, posix } from 'path' - import { writeFile, existsSync } from 'fs-extra' -import kebabHash from 'kebab-hash' -import _ from 'lodash' +import mergeWith from 'lodash.mergewith' import { HEADER_COMMENT, IMMUTABLE_CACHING_HEADER, - COMMON_BUNDLES, SECURITY_HEADERS, CACHING_HEADERS, LINK_REGEX, NETLIFY_HEADERS_FILENAME, - PAGE_DATA_DIR, } from './constants' +import { isBoolean, flow } from './util' const getHeaderName = (header: any) => { const matches = header.match(/^([^:]+):/) @@ -22,7 +18,7 @@ const getHeaderName = (header: any) => { } const validHeaders = (headers: any, reporter: any) => { - if (!headers || !_.isObject(headers)) { + if (!headers || typeof headers !== 'object') { return false } @@ -40,100 +36,16 @@ const validHeaders = (headers: any, reporter: any) => { ) } -const linkTemplate = (assetPath: any, type = `script`) => - `Link: <${assetPath}>; rel=preload; as=${type}${type === `fetch` ? `; crossorigin` : ``}` - -const pathChunkName = (path: any) => { - const name = path === `/` ? `index` : kebabHash(path) - return `path---${name}` -} - -const getPageDataPath = (path: any) => { - const fixedPagePath = path === `/` ? `index` : path - return posix.join(`page-data`, fixedPagePath, `page-data.json`) -} - -const getScriptPath = (file: any, manifest: any) => { - const chunk = manifest[file] - - if (!chunk) { - return [] - } - - // convert to array if it's not already - const chunks = Array.isArray(chunk) ? chunk : [chunk] - - return chunks.filter((script) => { - const parsed = parse(script) - // handle only .js, .css content is inlined already - // and doesn't need to be pushed - return parsed.ext === `.js` - }) -} - -const getLinkHeaders = (filesByType: any, pathPrefix: any) => - Object.entries(filesByType).flatMap(([type, files]: [string, Array]) => - files.map((file) => linkTemplate(`${pathPrefix}/${file}`, type)), - ) - const headersPath = (pathPrefix: any, path: any) => `${pathPrefix}${path}` -const preloadHeadersByPage = ({ - pages, - manifest, - pathPrefix, - publicFolder -}: any) => { - const linksByPage = {} - - const appDataPath = publicFolder(PAGE_DATA_DIR, `app-data.json`) - const hasAppData = existsSync(appDataPath) - - let hasPageData = false - if (pages.size !== 0) { - // test if 1 page-data file exists, if it does we know we're on a gatsby version that supports page-data - const pageDataPath = publicFolder(getPageDataPath(pages.get(pages.keys().next().value).path)) - hasPageData = existsSync(pageDataPath) +const unionMerge = (objValue: any, srcValue: any) => { + if (Array.isArray(objValue)) { + return [...new Set([...objValue, ...srcValue])] } - - pages.forEach((page: any) => { - const scripts = _.flatMap(COMMON_BUNDLES, (file) => getScriptPath(file, manifest)) - scripts.push( - ...getScriptPath(pathChunkName(page.path), manifest), - ...getScriptPath(page.componentChunkName, manifest), - ) - - const json = [] - if (hasAppData) { - json.push(posix.join(PAGE_DATA_DIR, `app-data.json`)) - } - - if (hasPageData) { - json.push(getPageDataPath(page.path)) - } - - const filesByResourceType = { - script: scripts.filter(Boolean), - fetch: json, - } - - const pathKey = headersPath(pathPrefix, page.path) - linksByPage[pathKey] = getLinkHeaders(filesByResourceType, pathPrefix) - }) - - return linksByPage + // opt into default merge behavior } -const defaultMerge = (...headers: any[]) => { - const unionMerge = (objValue: any, srcValue: any) => { - if (Array.isArray(objValue)) { - return _.union(objValue, srcValue) - } - // opt into default merge behavior - } - - return _.mergeWith({}, ...headers, unionMerge) -} +const defaultMerge = (...headers: any[]) => mergeWith({}, ...headers, unionMerge) const headersMerge = (userHeaders: any, defaultHeaders: any) => { const merged = {} @@ -195,8 +107,8 @@ const validateUserOptions = (pluginOptions: any, reporter: any) => (headers: any ) } - [`mergeSecurityHeaders`, `mergeLinkHeaders`, `mergeCachingHeaders`].forEach((mergeOption) => { - if (!_.isBoolean(pluginOptions[mergeOption])) { + [`mergeSecurityHeaders`, `mergeCachingHeaders`].forEach((mergeOption) => { + if (!isBoolean(pluginOptions[mergeOption])) { throw new TypeError( `The "${mergeOption}" option to gatsby-plugin-netlify must be a boolean. Check your gatsby-config.js.`, ) @@ -249,26 +161,6 @@ const mapUserLinkAllPageHeaders = return defaultMerge(headers, duplicateHeadersByPage) } -const applyLinkHeaders = - (pluginData: any, { - mergeLinkHeaders - }: any) => - (headers: any) => { - if (!mergeLinkHeaders) { - return headers - } - - const { pages, manifest, pathPrefix, publicFolder } = pluginData - const perPageHeaders = preloadHeadersByPage({ - pages, - manifest, - pathPrefix, - publicFolder, - }) - - return defaultMerge(headers, perPageHeaders) - } - const applySecurityHeaders = ({ mergeSecurityHeaders @@ -314,11 +206,16 @@ const applyCachingHeaders = return defaultMerge(headers, cachingHeaders, CACHING_HEADERS) } -const applyTransfromHeaders = +const applyTransformHeaders = ({ transformHeaders }: any) => - (headers: any) => _.mapValues(headers, transformHeaders) + (headers: any) => + Object.entries(headers).reduce((temp, [key, value]) => { + temp[key] = transformHeaders(value) + return temp + }, {}) + const transformToString = (headers: any) => `${HEADER_COMMENT}\n\n${stringifyHeaders(headers)}` @@ -328,18 +225,19 @@ const writeHeadersFile = }: any) => (contents: any) => writeFile(publicFolder(NETLIFY_HEADERS_FILENAME), contents) -const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) => - _.flow( +const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) => + flow( + [ validateUserOptions(pluginOptions, reporter), mapUserLinkHeaders(pluginData), applySecurityHeaders(pluginOptions), applyCachingHeaders(pluginData, pluginOptions), mapUserLinkAllPageHeaders(pluginData, pluginOptions), - applyLinkHeaders(pluginData, pluginOptions), - applyTransfromHeaders(pluginOptions), + applyTransformHeaders(pluginOptions), transformToString, writeHeadersFile(pluginData), - )(pluginOptions.headers) + ])(pluginOptions.headers) + export default buildHeadersProgram /* eslint-enable max-lines */ diff --git a/src/constants.ts b/src/constants.ts index 4731e5fc..e5c06daf 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -import _ from 'lodash' - // Gatsby values export const BUILD_HTML_STAGE = `build-html` export const BUILD_CSS_STAGE = `build-css` @@ -14,7 +12,7 @@ export const DEFAULT_OPTIONS = { mergeSecurityHeaders: true, mergeLinkHeaders: true, mergeCachingHeaders: true, - transformHeaders: _.identity, + transformHeaders: (value) => value, generateMatchPathRewrites: true, } @@ -37,10 +35,6 @@ export const CACHING_HEADERS = { export const LINK_REGEX = /^(Link: <\/)(.+)(>;.+)/ export const ROOT_WILDCARD = `/*` -export const COMMON_BUNDLES = [`commons`, `app`] - export const HEADER_COMMENT = `## Created with gatsby-plugin-netlify` -export const PAGE_DATA_DIR = `page-data/` - export const PAGE_COUNT_WARN = 1000 diff --git a/src/gatsby-node.ts b/src/gatsby-node.ts index e022c578..25faf494 100644 --- a/src/gatsby-node.ts +++ b/src/gatsby-node.ts @@ -25,7 +25,9 @@ export const pluginOptionsSchema = ({ Joi }: any) => { headers: headersSchema, allPageHeaders: Joi.array().items(Joi.string()).description(`Add more headers to all the pages`), mergeSecurityHeaders: Joi.boolean().description(`When set to false, turns off the default security headers`), - mergeLinkHeaders: Joi.boolean().description(`When set to false, turns off the default gatsby js headers`), + mergeLinkHeaders: Joi.boolean().description(`When set to false, turns off the default gatsby js headers`).forbidden().messages({ + "any.unknown": `"mergeLinkHeaders" is no longer supported. Gatsby no longer adds preload headers as they negatively affect load performance` + }), mergeCachingHeaders: Joi.boolean().description(`When set to false, turns off the default caching headers`), transformHeaders: Joi.function() .maxArity(2) @@ -62,9 +64,9 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }: any, userPlug const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions } const { redirects, pages, functions = [], program } = store.getState() - if (pages.size > PAGE_COUNT_WARN && (pluginOptions.mergeCachingHeaders || pluginOptions.mergeLinkHeaders)) { + if (pages.size > PAGE_COUNT_WARN && pluginOptions.mergeCachingHeaders ) { reporter.warn( - `[gatsby-plugin-netlify] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" and "mergeLinkHeaders" in your plugin config`, + `[gatsby-plugin-netlify] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" in your plugin config`, ) } reporter.info(`[gatsby-plugin-netlify] Creating SSR/DSG redirects...`) diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 00000000..67cba94e --- /dev/null +++ b/src/util.ts @@ -0,0 +1,31 @@ +// Accounting for true, false, and new Boolean() +export const isBoolean = (val: any): boolean => typeof val === 'boolean' || + ( + typeof val === 'object' && + val !== null && + typeof val.valueOf() === 'boolean' + ) + +type Header = string +type Headers = Header[] + +// TODO: better return type +type FlowableFunction = (...flowArgs: Headers) => any; +/** + * + * @param functions - takes in an array of functions + * @returns The function documented below + */ +export const flow = +>(functions: FlowableFunction[]) => + /** + * + * @param headers - In our case, headers is only {pluginOptions.headers} (in build-headers-program.ts), + * but in this generic implementation could take in any number of arguments + * + * @returns The evaluated return value of the last function from the array of functions provided in the + * {functions} parameter + */ + (...headers: Headers): FlowReturnType => functions.reduce((resultOfPrev, func) => [func(...resultOfPrev)], headers)[0] + + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 6a2b87ed..37a091d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -81,20 +81,20 @@ semver "^6.3.0" "@babel/core@^7.13.8", "@babel/core@^7.15.5", "@babel/core@^7.16.7": - version "7.17.10" - resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.17.10.tgz#74ef0fbf56b7dfc3f198fc2d927f4f03e12f4b05" - integrity sha512-liKoppandF3ZcBnIYFjfSDHZLKdLHGJRkoWtG8zQyGJBQfIYobpnVGI5+pLBNtS6psFLDzyq8+h5HiVljW9PNA== + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.0.tgz#c58d04d7c6fbfb58ea7681e2b9145cfb62726756" + integrity sha512-Xyw74OlJwDijToNi0+6BBI5mLLR5+5R3bcSH80LXzjzEGEUlvNzujEE71BaD/ApEZHAvFI/Mlmp4M5lIkdeeWw== dependencies: "@ampproject/remapping" "^2.1.0" "@babel/code-frame" "^7.16.7" - "@babel/generator" "^7.17.10" + "@babel/generator" "^7.18.0" "@babel/helper-compilation-targets" "^7.17.10" - "@babel/helper-module-transforms" "^7.17.7" - "@babel/helpers" "^7.17.9" - "@babel/parser" "^7.17.10" + "@babel/helper-module-transforms" "^7.18.0" + "@babel/helpers" "^7.18.0" + "@babel/parser" "^7.18.0" "@babel/template" "^7.16.7" - "@babel/traverse" "^7.17.10" - "@babel/types" "^7.17.10" + "@babel/traverse" "^7.18.0" + "@babel/types" "^7.18.0" convert-source-map "^1.7.0" debug "^4.1.0" gensync "^1.0.0-beta.2" @@ -126,6 +126,15 @@ "@jridgewell/gen-mapping" "^0.1.0" jsesc "^2.5.1" +"@babel/generator@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.0.tgz#46d28e8a18fc737b028efb25ab105d74473af43f" + integrity sha512-81YO9gGx6voPXlvYdZBliFXAZU8vZ9AZ6z+CjlmcnaeOcYSFbMTpdeDUO9xD9dh/68Vq03I8ZspfUTPfitcDHg== + dependencies: + "@babel/types" "^7.18.0" + "@jridgewell/gen-mapping" "^0.3.0" + jsesc "^2.5.1" + "@babel/generator@^7.7.2": version "7.17.9" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.17.9.tgz#f4af9fd38fa8de143c29fce3f71852406fc1e2fc" @@ -252,6 +261,20 @@ "@babel/traverse" "^7.17.3" "@babel/types" "^7.17.0" +"@babel/helper-module-transforms@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.18.0.tgz#baf05dec7a5875fb9235bd34ca18bad4e21221cd" + integrity sha512-kclUYSUBIjlvnzN2++K9f2qzYKFgjmnmjwL4zlmU5f8ZtzgWe8s0rUPSTGy2HmK4P8T52MQsS+HTQAgZd3dMEA== + dependencies: + "@babel/helper-environment-visitor" "^7.16.7" + "@babel/helper-module-imports" "^7.16.7" + "@babel/helper-simple-access" "^7.17.7" + "@babel/helper-split-export-declaration" "^7.16.7" + "@babel/helper-validator-identifier" "^7.16.7" + "@babel/template" "^7.16.7" + "@babel/traverse" "^7.18.0" + "@babel/types" "^7.18.0" + "@babel/helper-optimise-call-expression@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.16.7.tgz#a34e3560605abbd31a18546bd2aad3e6d9a174f2" @@ -334,7 +357,16 @@ "@babel/traverse" "^7.17.9" "@babel/types" "^7.17.0" -"@babel/highlight@^7.10.4", "@babel/highlight@^7.16.7": +"@babel/helpers@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.18.0.tgz#aff37c3590de42102b54842446146d0205946370" + integrity sha512-AE+HMYhmlMIbho9nbvicHyxFwhrO+xhKB6AhRxzl8w46Yj0VXTZjEsAoBVC7rB2I0jzX+yWyVybnO08qkfx6kg== + dependencies: + "@babel/template" "^7.16.7" + "@babel/traverse" "^7.18.0" + "@babel/types" "^7.18.0" + +"@babel/highlight@^7.10.4": version "7.17.9" resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.17.9.tgz#61b2ee7f32ea0454612def4fccdae0de232b73e3" integrity sha512-J9PfEKCbFIv2X5bjTMiZu6Vf341N05QIY+d6FvVKynkG1S7G0j3I0QoRtWIrXhZ+/Nlb5Q0MzqL7TokEJ5BNHg== @@ -343,16 +375,30 @@ chalk "^2.0.0" js-tokens "^4.0.0" +"@babel/highlight@^7.16.7": + version "7.17.12" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.17.12.tgz#257de56ee5afbd20451ac0a75686b6b404257351" + integrity sha512-7yykMVF3hfZY2jsHZEEgLc+3x4o1O+fYyULu11GynEUQNwB6lua+IIQn1FiJxNucd5UlyJryrwsOh8PL9Sn8Qg== + dependencies: + "@babel/helper-validator-identifier" "^7.16.7" + chalk "^2.0.0" + js-tokens "^4.0.0" + "@babel/parser@^7.1.0", "@babel/parser@^7.14.7": version "7.17.9" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.17.9.tgz#9c94189a6062f0291418ca021077983058e171ef" integrity sha512-vqUSBLP8dQHFPdPi9bc5GK9vRkYHJ49fsZdtoJ8EQ8ibpwk5rPKfvNIwChB0KVXcIjcepEBBd2VHC5r9Gy8ueg== -"@babel/parser@^7.15.5", "@babel/parser@^7.16.7", "@babel/parser@^7.17.10", "@babel/parser@^7.17.9": +"@babel/parser@^7.15.5", "@babel/parser@^7.17.10", "@babel/parser@^7.17.9": version "7.17.10" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.17.10.tgz#873b16db82a8909e0fbd7f115772f4b739f6ce78" integrity sha512-n2Q6i+fnJqzOaq2VkdXxy2TCPCWQZHiCo0XqmrCvDWcZQKRyZzYi4Z0yxlBuN0w+r2ZHmre+Q087DSrw3pbJDQ== +"@babel/parser@^7.16.7", "@babel/parser@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.0.tgz#10a8d4e656bc01128d299a787aa006ce1a91e112" + integrity sha512-AqDccGC+m5O/iUStSJy3DGRIUFu7WbY/CppZYwrEUB4N0tZlnI8CSTsgL7v5fHVFmUbRv2sd+yy27o8Ydt4MGg== + "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression/-/plugin-bugfix-safari-id-destructuring-collision-in-function-expression-7.16.7.tgz#4eda6d6c2a0aa79c70fa7b6da67763dfe2141050" @@ -1079,9 +1125,9 @@ regenerator-runtime "^0.13.4" "@babel/runtime@^7.10.0", "@babel/runtime@^7.10.2", "@babel/runtime@^7.15.4", "@babel/runtime@^7.16.3", "@babel/runtime@^7.16.7", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2": - version "7.17.9" - resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.9.tgz#d19fbf802d01a8cb6cf053a64e472d42c434ba72" - integrity sha512-lSiBBvodq29uShpWGNbgFdKYNiFDo5/HIYsaCEY9ff4sb10x9jizo2+pRrSyF4jKZCXqgzuqBOQKbUm90gQwJg== + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.0.tgz#6d77142a19cb6088f0af662af1ada37a604d34ae" + integrity sha512-YMQvx/6nKEaucl0MY56mwIG483xk8SDNdlUwb2Ts6FUpr7fm85DxEmsY18LXBNhcTz6tO6JwZV8w1W06v8UKeg== dependencies: regenerator-runtime "^0.13.4" @@ -1094,7 +1140,7 @@ "@babel/parser" "^7.16.7" "@babel/types" "^7.16.7" -"@babel/traverse@^7.13.0", "@babel/traverse@^7.15.4", "@babel/traverse@^7.16.7", "@babel/traverse@^7.16.8", "@babel/traverse@^7.17.10", "@babel/traverse@^7.17.3", "@babel/traverse@^7.17.9": +"@babel/traverse@^7.13.0", "@babel/traverse@^7.15.4", "@babel/traverse@^7.16.7", "@babel/traverse@^7.16.8", "@babel/traverse@^7.17.3", "@babel/traverse@^7.17.9": version "7.17.10" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.17.10.tgz#1ee1a5ac39f4eac844e6cf855b35520e5eb6f8b5" integrity sha512-VmbrTHQteIdUUQNTb+zE12SHS/xQVIShmBPhlNP12hD5poF2pbITW1Z4172d03HegaQWhLffdkRJYtAzp0AGcw== @@ -1110,6 +1156,22 @@ debug "^4.1.0" globals "^11.1.0" +"@babel/traverse@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.18.0.tgz#0e5ec6db098660b2372dd63d096bf484e32d27ba" + integrity sha512-oNOO4vaoIQoGjDQ84LgtF/IAlxlyqL4TUuoQ7xLkQETFaHkY1F7yazhB4Kt3VcZGL0ZF/jhrEpnXqUb0M7V3sw== + dependencies: + "@babel/code-frame" "^7.16.7" + "@babel/generator" "^7.18.0" + "@babel/helper-environment-visitor" "^7.16.7" + "@babel/helper-function-name" "^7.17.9" + "@babel/helper-hoist-variables" "^7.16.7" + "@babel/helper-split-export-declaration" "^7.16.7" + "@babel/parser" "^7.18.0" + "@babel/types" "^7.18.0" + debug "^4.1.0" + globals "^11.1.0" + "@babel/traverse@^7.7.2": version "7.17.9" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.17.9.tgz#1f9b207435d9ae4a8ed6998b2b82300d83c37a0d" @@ -1134,7 +1196,7 @@ "@babel/helper-validator-identifier" "^7.16.7" to-fast-properties "^2.0.0" -"@babel/types@^7.0.0-beta.49", "@babel/types@^7.15.4", "@babel/types@^7.16.0", "@babel/types@^7.16.7", "@babel/types@^7.16.8", "@babel/types@^7.17.0", "@babel/types@^7.17.10", "@babel/types@^7.4.4": +"@babel/types@^7.0.0-beta.49", "@babel/types@^7.15.4", "@babel/types@^7.16.0", "@babel/types@^7.16.8", "@babel/types@^7.17.10", "@babel/types@^7.4.4": version "7.17.10" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.17.10.tgz#d35d7b4467e439fcf06d195f8100e0fea7fc82c4" integrity sha512-9O26jG0mBYfGkUYCYZRnBwbVLd1UZOICEr2Em6InB6jVfsAv1GKgwXHmrSg+WFWDmeKTA6vyTZiN8tCSM5Oo3A== @@ -1142,6 +1204,14 @@ "@babel/helper-validator-identifier" "^7.16.7" to-fast-properties "^2.0.0" +"@babel/types@^7.16.7", "@babel/types@^7.17.0", "@babel/types@^7.18.0": + version "7.18.0" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.0.tgz#ef523ea349722849cb4bf806e9342ede4d071553" + integrity sha512-vhAmLPAiC8j9K2GnsnLPCIH5wCrPpYIVBCWRBFDCB7Y/BXLqi/O+1RSTTM2bsmg6U/551+FCf9PNPxjABmxHTw== + dependencies: + "@babel/helper-validator-identifier" "^7.16.7" + to-fast-properties "^2.0.0" + "@bcoe/v8-coverage@^0.2.3": version "0.2.3" resolved "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz" @@ -2052,6 +2122,15 @@ "@jridgewell/set-array" "^1.0.0" "@jridgewell/sourcemap-codec" "^1.4.10" +"@jridgewell/gen-mapping@^0.3.0": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.1.tgz#cf92a983c83466b8c0ce9124fadeaf09f7c66ea9" + integrity sha512-GcHwniMlA2z+WFPWuY8lp3fsza0I8xPFMWL5+n8LYyP6PSvPrXf4+n8stDHZY2DM0zy9sVkRDy1jDI4XGzYVqg== + dependencies: + "@jridgewell/set-array" "^1.0.0" + "@jridgewell/sourcemap-codec" "^1.4.10" + "@jridgewell/trace-mapping" "^0.3.9" + "@jridgewell/resolve-uri@^3.0.3": version "3.0.7" resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.0.7.tgz#30cd49820a962aff48c8fffc5cd760151fca61fe" @@ -2076,9 +2155,9 @@ "@jridgewell/sourcemap-codec" "^1.4.10" "@jridgewell/trace-mapping@^0.3.9": - version "0.3.10" - resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.10.tgz#db436f0917d655393851bc258918c00226c9b183" - integrity sha512-Q0YbBd6OTsXm8Y21+YUSDXupHnodNC2M4O18jtd3iwJ3+vMZNdKGols0a9G6JOK0dcJ3IdUUHoh908ZI6qhk8Q== + version "0.3.13" + resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.13.tgz#dcfe3e95f224c8fe97a87a5235defec999aa92ea" + integrity sha512-o1xbKhp9qnIAoHJSWd6KlCZfqslL4valSF81H8ImioOAxluWYWOpWkpyktY2vnt4tbrX9XYaxovq6cgowaJp2w== dependencies: "@jridgewell/resolve-uri" "^3.0.3" "@jridgewell/sourcemap-codec" "^1.4.10" @@ -2941,9 +3020,9 @@ "@types/istanbul-lib-report" "*" "@types/jest@^27.4.1": - version "27.5.0" - resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.5.0.tgz#e04ed1824ca6b1dd0438997ba60f99a7405d4c7b" - integrity sha512-9RBFx7r4k+msyj/arpfaa0WOOEcaAZNmN+j80KFbFCoSqCJGHTz7YMAMGQW9Xmqm5w6l5c25vbSjMwlikJi5+g== + version "27.5.1" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.5.1.tgz#2c8b6dc6ff85c33bcd07d0b62cb3d19ddfdb3ab9" + integrity sha512-fUy7YRpT+rHXto1YlL+J9rs0uLGyiqVt3ZOTQR+4ROc47yNl8WLdVLgUloBRhOxP1PZvguHl44T3H0wAWxahYQ== dependencies: jest-matcher-utils "^27.0.0" pretty-format "^27.0.0" @@ -4334,11 +4413,16 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001125, caniuse-lite@^1.0.30001332, caniuse-lite@^1.0.30001335: +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001125, caniuse-lite@^1.0.30001335: version "1.0.30001338" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001338.tgz#b5dd7a7941a51a16480bdf6ff82bded1628eec0d" integrity sha512-1gLHWyfVoRDsHieO+CaeYe7jSo/MT7D7lhaXUiwwbuR5BwQxORs0f1tAwUSQr3YbxRXJvxHM/PA5FfPQRnsPeQ== +caniuse-lite@^1.0.30001332: + version "1.0.30001342" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001342.tgz#87152b1e3b950d1fbf0093e23f00b6c8e8f1da96" + integrity sha512-bn6sOCu7L7jcbBbyNhLg0qzXdJ/PMbybZTH/BA6Roet9wxYRm6Tr9D0s0uhLkOZ6MSG+QU6txUgdpr3MXIVqjA== + chalk@2.4.2, chalk@^2.0.0, chalk@^2.4.1, chalk@^2.4.2: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" @@ -9158,6 +9242,11 @@ lodash.merge@^4.6.2: resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== +lodash.mergewith@^4.6.2: + version "4.6.2" + resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55" + integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ== + lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" @@ -9185,7 +9274,7 @@ lodash.zip@^4.2.0: lodash@4.17.21, lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.7.0: version "4.17.21" - resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== loose-envify@^1.0.0, loose-envify@^1.4.0: