Skip to content

Commit ca15fcd

Browse files
techfgascorbic
andauthored
add path to transformHeaders - resolves #275 (#276)
Co-authored-by: Matt Kane <[email protected]>
1 parent 2dc646e commit ca15fcd

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

src/__tests__/__snapshots__/build-headers-program.ts.snap

+85
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,88 @@ exports[`build-headers-program without caching headers 1`] = `
8585
Referrer-Policy: same-origin
8686
"
8787
`;
88+
89+
exports[`build-headers-program with all page headers configuration 1`] = `
90+
"## Created with gatsby-plugin-netlify
91+
92+
/*
93+
X-Frame-Options: DENY
94+
X-XSS-Protection: 1; mode=block
95+
X-Content-Type-Options: nosniff
96+
Referrer-Policy: same-origin
97+
/webpack-runtime-acaa8994f1f704475e21.js
98+
Cache-Control: public, max-age=31536000, immutable
99+
/styles.1025963f4f2ec7abbad4.css
100+
Cache-Control: public, max-age=31536000, immutable
101+
/styles-565f081c8374bbda155f.js
102+
Cache-Control: public, max-age=31536000, immutable
103+
/app-f33c13590352da20930f.js
104+
Cache-Control: public, max-age=31536000, immutable
105+
/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js
106+
Cache-Control: public, max-age=31536000, immutable
107+
/0-0180cd94ef2497ac7db8.js
108+
Cache-Control: public, max-age=31536000, immutable
109+
/component---src-templates-blog-post-js-517987eae96e75cddbe7.js
110+
Cache-Control: public, max-age=31536000, immutable
111+
/component---src-pages-404-js-53e6c51a5a7e73090f50.js
112+
Cache-Control: public, max-age=31536000, immutable
113+
/component---src-pages-index-js-0bdd01c77ee09ef0224c.js
114+
Cache-Control: public, max-age=31536000, immutable
115+
/711-90491aa56de138c82516.js
116+
Cache-Control: public, max-age=31536000, immutable
117+
/static/*
118+
Cache-Control: public, max-age=31536000, immutable
119+
/sw.js
120+
Cache-Control: no-cache
121+
/offline-plugin-app-shell-fallback/
122+
X-Custom-PageHeader: SomeCustomValue
123+
/hi-folks/
124+
X-Custom-PageHeader: SomeCustomValue
125+
/my-second-post/
126+
X-Custom-PageHeader: SomeCustomValue
127+
/hello-world/
128+
X-Custom-PageHeader: SomeCustomValue
129+
/404/
130+
X-Custom-PageHeader: SomeCustomValue
131+
/
132+
X-Custom-PageHeader: SomeCustomValue
133+
/404.html
134+
X-Custom-PageHeader: SomeCustomValue
135+
"
136+
`;
137+
138+
exports[`build-headers-program with transform headers configuration 1`] = `
139+
"## Created with gatsby-plugin-netlify
140+
141+
/*
142+
X-Frame-Options: DENY
143+
X-XSS-Protection: 1; mode=block
144+
X-Content-Type-Options: nosniff
145+
Referrer-Policy: same-origin
146+
/webpack-runtime-acaa8994f1f704475e21.js
147+
Cache-Control: public, max-age=31536000, immutable
148+
X-TransformedPageHeader: webpack-runtime
149+
/styles.1025963f4f2ec7abbad4.css
150+
Cache-Control: public, max-age=31536000, immutable
151+
/styles-565f081c8374bbda155f.js
152+
Cache-Control: public, max-age=31536000, immutable
153+
/app-f33c13590352da20930f.js
154+
Cache-Control: public, max-age=31536000, immutable
155+
/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js
156+
Cache-Control: public, max-age=31536000, immutable
157+
/0-0180cd94ef2497ac7db8.js
158+
Cache-Control: public, max-age=31536000, immutable
159+
/component---src-templates-blog-post-js-517987eae96e75cddbe7.js
160+
Cache-Control: public, max-age=31536000, immutable
161+
/component---src-pages-404-js-53e6c51a5a7e73090f50.js
162+
Cache-Control: public, max-age=31536000, immutable
163+
/component---src-pages-index-js-0bdd01c77ee09ef0224c.js
164+
Cache-Control: public, max-age=31536000, immutable
165+
/711-90491aa56de138c82516.js
166+
Cache-Control: public, max-age=31536000, immutable
167+
/static/*
168+
Cache-Control: public, max-age=31536000, immutable
169+
/sw.js
170+
Cache-Control: no-cache
171+
"
172+
`;

src/__tests__/build-headers-program.ts

+37
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,41 @@ describe(`build-headers-program`, () => {
116116

117117
expect(reporter.panic).toHaveBeenCalled()
118118
})
119+
120+
it(`with all page headers configuration`, async () => {
121+
const pluginData = await createPluginData()
122+
123+
const pluginOptions = {
124+
...DEFAULT_OPTIONS,
125+
allPageHeaders: [
126+
`X-Custom-PageHeader: SomeCustomValue`
127+
]
128+
}
129+
130+
await buildHeadersProgram(pluginData, pluginOptions, reporter)
131+
132+
expect(reporter.panic).not.toHaveBeenCalled()
133+
expect(await readFile(pluginData.publicFolder(`_headers`), `utf8`)).toMatchSnapshot()
134+
})
135+
136+
it(`with transform headers configuration`, async () => {
137+
const pluginData = await createPluginData()
138+
139+
const pluginOptions = {
140+
...DEFAULT_OPTIONS,
141+
transformHeaders: (headers, path) => {
142+
if (path !== '/webpack-runtime-acaa8994f1f704475e21.js') {
143+
return headers
144+
}
145+
146+
headers.push('X-TransformedPageHeader: webpack-runtime')
147+
return headers
148+
}
149+
}
150+
151+
await buildHeadersProgram(pluginData, pluginOptions, reporter)
152+
153+
expect(reporter.panic).not.toHaveBeenCalled()
154+
expect(await readFile(pluginData.publicFolder(`_headers`), `utf8`)).toMatchSnapshot()
155+
})
119156
})

src/build-headers-program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const applyTransformHeaders =
210210
}: any) =>
211211
(headers: any) =>
212212
Object.entries(headers).reduce((temp, [key, value]) => {
213-
temp[key] = transformHeaders(value)
213+
temp[key] = transformHeaders(value, key)
214214
return temp
215215
}, {})
216216

0 commit comments

Comments
 (0)