Skip to content

Commit

Permalink
feat: support custom response headers (#51)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ericapisani authored Aug 10, 2022
1 parent e9513a5 commit 857eb75
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ remotePatterns: [

- Clone repository
- Install dependencies with `yarn install`
- Install netlify development server with `yarn dev`
- Build the project with `yarn build`
- Run netlify development server with `yarn dev`.
- Open http://localhost:8888

## License
Expand Down
6 changes: 5 additions & 1 deletion example/netlify/functions/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ export const handler = createIPXHandler({
domains: [
'www.netlify.com'
],
basePath: '/.netlify/builders/ipx/'
basePath: '/.netlify/builders/ipx/',
responseHeaders: {
'Strict-Transport-Security': 'max-age=31536000',
'X-Test': 'foobar'
}
})
Binary file added netlify-ipx-v1.1.4.tgz
Binary file not shown.
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IPXHandlerOptions extends Partial<IPXOptions> {
propsEncoding?: 'base64' | undefined
bypassDomainCheck?: boolean
remotePatterns?: RemotePattern[]
responseHeaders?: Record<string, string>
}

export function createIPXHandler ({
Expand All @@ -20,6 +21,7 @@ export function createIPXHandler ({
propsEncoding,
bypassDomainCheck,
remotePatterns,
responseHeaders,
...opts
}: IPXHandlerOptions = {}) {
const ipx = createIPX({ ...opts, dir: join(cacheDir, 'cache') })
Expand Down Expand Up @@ -98,6 +100,7 @@ export function createIPXHandler ({
}

if (!domainAllowed) {
// eslint-disable-next-line no-console
console.log(`URL not on allowlist. Values provided are:
domains: ${JSON.stringify(domains)}
remotePatterns: ${JSON.stringify(remoteURLPatterns)}
Expand Down Expand Up @@ -143,6 +146,13 @@ export function createIPXHandler ({
message: 'Not Modified'
}
}

if (responseHeaders) {
for (const [header, value] of Object.entries(responseHeaders)) {
res.headers[header] = value
}
}

return {
statusCode: res.statusCode,
message: res.statusMessage,
Expand Down

0 comments on commit 857eb75

Please sign in to comment.