Skip to content

Commit

Permalink
Limit paths to apply the security headers function to, and support a …
Browse files Browse the repository at this point in the history
…shortcut with 304 response.
  • Loading branch information
qubyte committed Jun 3, 2023
1 parent 475c232 commit a2f0707
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion netlify/edge-functions/add-html-security-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ const scriptSrcHashRegex = /^\s*<meta name="script-src-hash" content="(.*)">$/m;
/* eslint-env browser */
export default async function addHtmlSecurityHeaders(_, context) {
/** @type Response */
const response = await context.next();
const response = await context.next({ sendConditionalRequest: true });

// No need to do any work if the response cached by the client hasn't changed.
if (response.status === 304) {
return response;
}

const { headers } = response;
const type = headers.get('content-type');

Expand Down Expand Up @@ -31,3 +37,8 @@ export default async function addHtmlSecurityHeaders(_, context) {

return response;
}

export const config = {
path: '/*',
excludedPath: ['/**/hashed-*', '/icons/*', '/images/*', '/img/*', '/**/*.xml']
};

0 comments on commit a2f0707

Please sign in to comment.