From 75e0219a985afa01192ec43455098e394f531473 Mon Sep 17 00:00:00 2001 From: Hyunje Jun Date: Wed, 18 Apr 2018 18:28:19 +0900 Subject: [PATCH] feat(ssr): Add 'nonce' option to context for ssr outlet script re #7479 --- src/server/template-renderer/index.js | 3 ++- test/ssr/ssr-template.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/server/template-renderer/index.js b/src/server/template-renderer/index.js index a6a1570c6b7..cfed53385e4 100644 --- a/src/server/template-renderer/index.js +++ b/src/server/template-renderer/index.js @@ -195,8 +195,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] - ? `` + ? `window.${windowKey}=${state}${autoRemove}` : '' } diff --git a/test/ssr/ssr-template.spec.js b/test/ssr/ssr-template.spec.js index d93528466bb..4fa4f625421 100644 --- a/test/ssr/ssr-template.spec.js +++ b/test/ssr/ssr-template.spec.js @@ -386,5 +386,32 @@ describe('SSR: template option', () => { done() }) }) + + it('renderToString + nonce', done => { + const interpolateTemplate = `hello` + const renderer = createRenderer({ + template: interpolateTemplate + }) + + const context = { + state: { a: 1 }, + nonce: '4AEemGb0xJptoIGFP3Nd' + } + + renderer.renderToString(new Vue({ + template: '
hi
' + }), context, (err, res) => { + expect(err).toBeNull() + expect(res).toContain( + `` + + `hello` + + `` + + `
hi
` + + `` + + `` + ) + done() + }) + }) } })