From 71b51662d2b1a3b66ec95d28bf7a4598e1faf8e9 Mon Sep 17 00:00:00 2001 From: techfg Date: Wed, 6 Sep 2023 23:30:29 -0700 Subject: [PATCH] add path to transformHeaders - resolves #275 --- .../build-headers-program.ts.snap | 85 +++++++++++++++++++ src/__tests__/build-headers-program.ts | 37 ++++++++ src/build-headers-program.ts | 2 +- 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/__tests__/__snapshots__/build-headers-program.ts.snap b/src/__tests__/__snapshots__/build-headers-program.ts.snap index d7e0ae05..0632f1af 100644 --- a/src/__tests__/__snapshots__/build-headers-program.ts.snap +++ b/src/__tests__/__snapshots__/build-headers-program.ts.snap @@ -85,3 +85,88 @@ exports[`build-headers-program without caching headers 1`] = ` Referrer-Policy: same-origin " `; + +exports[`build-headers-program with all page headers configuration 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 +/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 +/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 +/711-90491aa56de138c82516.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/ + X-Custom-PageHeader: SomeCustomValue +/hi-folks/ + X-Custom-PageHeader: SomeCustomValue +/my-second-post/ + X-Custom-PageHeader: SomeCustomValue +/hello-world/ + X-Custom-PageHeader: SomeCustomValue +/404/ + X-Custom-PageHeader: SomeCustomValue +/ + X-Custom-PageHeader: SomeCustomValue +/404.html + X-Custom-PageHeader: SomeCustomValue +" +`; + +exports[`build-headers-program with transform headers configuration 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 +/webpack-runtime-acaa8994f1f704475e21.js + Cache-Control: public, max-age=31536000, immutable + X-TransformedPageHeader: webpack-runtime +/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 +/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 +/711-90491aa56de138c82516.js + Cache-Control: public, max-age=31536000, immutable +/static/* + Cache-Control: public, max-age=31536000, immutable +/sw.js + Cache-Control: no-cache +" +`; diff --git a/src/__tests__/build-headers-program.ts b/src/__tests__/build-headers-program.ts index 19901ad9..15b02b28 100644 --- a/src/__tests__/build-headers-program.ts +++ b/src/__tests__/build-headers-program.ts @@ -116,4 +116,41 @@ describe(`build-headers-program`, () => { expect(reporter.panic).toHaveBeenCalled() }) + + it(`with all page headers configuration`, async () => { + const pluginData = await createPluginData() + + const pluginOptions = { + ...DEFAULT_OPTIONS, + allPageHeaders: [ + `X-Custom-PageHeader: SomeCustomValue` + ] + } + + await buildHeadersProgram(pluginData, pluginOptions, reporter) + + expect(reporter.panic).not.toHaveBeenCalled() + expect(await readFile(pluginData.publicFolder(`_headers`), `utf8`)).toMatchSnapshot() + }) + + it(`with transform headers configuration`, async () => { + const pluginData = await createPluginData() + + const pluginOptions = { + ...DEFAULT_OPTIONS, + transformHeaders: (headers, path) => { + if (path !== '/webpack-runtime-acaa8994f1f704475e21.js') { + return headers + } + + headers.push('X-TransformedPageHeader: webpack-runtime') + return headers + } + } + + await buildHeadersProgram(pluginData, pluginOptions, reporter) + + expect(reporter.panic).not.toHaveBeenCalled() + expect(await readFile(pluginData.publicFolder(`_headers`), `utf8`)).toMatchSnapshot() + }) }) diff --git a/src/build-headers-program.ts b/src/build-headers-program.ts index dcdf85f2..578ec8a9 100644 --- a/src/build-headers-program.ts +++ b/src/build-headers-program.ts @@ -210,7 +210,7 @@ const applyTransformHeaders = }: any) => (headers: any) => Object.entries(headers).reduce((temp, [key, value]) => { - temp[key] = transformHeaders(value) + temp[key] = transformHeaders(value, key) return temp }, {})