Skip to content

Commit

Permalink
feat(ssr): Add 'nonce' option to context for ssr outlet script (#8047)
Browse files Browse the repository at this point in the history
close #7479
  • Loading branch information
Hyunje Jun authored and yyx990803 committed Dec 20, 2018
1 parent 9432737 commit f036cce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ export default class TemplateRenderer {
const autoRemove = process.env.NODE_ENV === 'production'
? ';(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
: ''
const nonceAttr = context.nonce ? ` nonce="${context.nonce}"` : ''
return context[contextKey]
? `<script>window.${windowKey}=${state}${autoRemove}</script>`
? `<script${nonceAttr}>window.${windowKey}=${state}${autoRemove}</script>`
: ''
}

Expand Down
27 changes: 27 additions & 0 deletions test/ssr/ssr-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,32 @@ describe('SSR: template option', () => {
done()
})
})

it('renderToString + nonce', done => {
const interpolateTemplate = `<html><head><title>hello</title></head><body><!--vue-ssr-outlet--></body></html>`
const renderer = createRenderer({
template: interpolateTemplate
})

const context = {
state: { a: 1 },
nonce: '4AEemGb0xJptoIGFP3Nd'
}

renderer.renderToString(new Vue({
template: '<div>hi</div>'
}), context, (err, res) => {
expect(err).toBeNull()
expect(res).toContain(
`<html><head>` +
`<title>hello</title>` +
`</head><body>` +
`<div data-server-rendered="true">hi</div>` +
`<script nonce="4AEemGb0xJptoIGFP3Nd">window.__INITIAL_STATE__={"a":1}</script>` +
`</body></html>`
)
done()
})
})
}
})

0 comments on commit f036cce

Please sign in to comment.