Skip to content

Commit

Permalink
allow scripts to contain a CSP nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Sep 7, 2018
1 parent 99ae39b commit e377515
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/middleware.ts
Expand Up @@ -524,9 +524,12 @@ function get_page_handler(
styles = (css && css.code ? `<style>${css.code}</style>` : '');
}

// users can set a CSP nonce using res.locals.nonce
const nonceAttr = (res.locals && res.locals.nonce) ? ` nonce="${res.locals.nonce}"` : '';

const body = template()
.replace('%sapper.base%', () => `<base href="${req.baseUrl}/">`)
.replace('%sapper.scripts%', () => `<script>${script}</script>`)
.replace('%sapper.scripts%', () => `<script${nonceAttr}>${script}</script>`)
.replace('%sapper.html%', () => html)
.replace('%sapper.head%', () => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`)
.replace('%sapper.styles%', () => styles);
Expand Down

0 comments on commit e377515

Please sign in to comment.