Skip to content

Commit 857eb75

Browse files
authored
feat: support custom response headers (#51)
* feat: support addition of custom response headers * refactor: change logic to accomodate use of netlify custom headers Previously was planning on using environment variables within netlify.toml of project. * style: lint issues * test: commit tarball committing this in order to test in a staging environment in the context of another project
1 parent e9513a5 commit 857eb75

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ remotePatterns: [
8989

9090
- Clone repository
9191
- Install dependencies with `yarn install`
92-
- Install netlify development server with `yarn dev`
92+
- Build the project with `yarn build`
93+
- Run netlify development server with `yarn dev`.
9394
- Open http://localhost:8888
9495

9596
## License

example/netlify/functions/ipx.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ export const handler = createIPXHandler({
1010
domains: [
1111
'www.netlify.com'
1212
],
13-
basePath: '/.netlify/builders/ipx/'
13+
basePath: '/.netlify/builders/ipx/',
14+
responseHeaders: {
15+
'Strict-Transport-Security': 'max-age=31536000',
16+
'X-Test': 'foobar'
17+
}
1418
})

netlify-ipx-v1.1.4.tgz

7.9 KB
Binary file not shown.

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface IPXHandlerOptions extends Partial<IPXOptions> {
1212
propsEncoding?: 'base64' | undefined
1313
bypassDomainCheck?: boolean
1414
remotePatterns?: RemotePattern[]
15+
responseHeaders?: Record<string, string>
1516
}
1617

1718
export function createIPXHandler ({
@@ -20,6 +21,7 @@ export function createIPXHandler ({
2021
propsEncoding,
2122
bypassDomainCheck,
2223
remotePatterns,
24+
responseHeaders,
2325
...opts
2426
}: IPXHandlerOptions = {}) {
2527
const ipx = createIPX({ ...opts, dir: join(cacheDir, 'cache') })
@@ -98,6 +100,7 @@ export function createIPXHandler ({
98100
}
99101

100102
if (!domainAllowed) {
103+
// eslint-disable-next-line no-console
101104
console.log(`URL not on allowlist. Values provided are:
102105
domains: ${JSON.stringify(domains)}
103106
remotePatterns: ${JSON.stringify(remoteURLPatterns)}
@@ -143,6 +146,13 @@ export function createIPXHandler ({
143146
message: 'Not Modified'
144147
}
145148
}
149+
150+
if (responseHeaders) {
151+
for (const [header, value] of Object.entries(responseHeaders)) {
152+
res.headers[header] = value
153+
}
154+
}
155+
146156
return {
147157
statusCode: res.statusCode,
148158
message: res.statusMessage,

0 commit comments

Comments
 (0)