Skip to content

Commit

Permalink
add path to transformHeaders - resolves #275 (#276)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Kane <matt.kane@netlify.com>
  • Loading branch information
techfg and ascorbic committed Sep 25, 2023
1 parent 2dc646e commit ca15fcd
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/__tests__/__snapshots__/build-headers-program.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;
37 changes: 37 additions & 0 deletions src/__tests__/build-headers-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
2 changes: 1 addition & 1 deletion src/build-headers-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {})

Expand Down

0 comments on commit ca15fcd

Please sign in to comment.