From 5d812d9b5054987ac64df86884488dca2cce59b4 Mon Sep 17 00:00:00 2001 From: Hyunje Jun Date: Fri, 21 Dec 2018 05:15:04 +0900 Subject: [PATCH] feat(ssr): Add 'nonce' option to context for ssr outlet script (#8047) close #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 65887c2591c..0233d157646 100644 --- a/src/server/template-renderer/index.js +++ b/src/server/template-renderer/index.js @@ -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] - ? `` + ? `window.${windowKey}=${state}${autoRemove}` : '' } diff --git a/test/ssr/ssr-template.spec.js b/test/ssr/ssr-template.spec.js index 2491b591d87..cf1ec6579e4 100644 --- a/test/ssr/ssr-template.spec.js +++ b/test/ssr/ssr-template.spec.js @@ -387,5 +387,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() + }) + }) } })