fix(vue-renderer): add crossorigin for styles (#7954)

* Added ability for styles to get crossorigin if set

* refactor to addAttrs

Co-authored-by: Clark Du <clark.duxin@gmail.com>
This commit is contained in:
Per Anders Ljusbäck 2020-08-26 14:03:31 +02:00 committed by GitHub
parent 9cb8a65e8d
commit 2f76702291
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,34 +20,39 @@ export default class SSRRenderer extends BaseRenderer {
}
}
renderScripts (renderContext) {
const scripts = renderContext.renderScripts()
const { render: { crossorigin } } = this.options
if (!crossorigin) {
return scripts
addAttrs (tags, referenceTag, referenceAttr) {
const reference = referenceTag ? `<${referenceTag}` : referenceAttr
if (!reference) {
return tags
}
return scripts.replace(
/<script/g,
`<script crossorigin="${crossorigin}"`
const { render: { crossorigin } } = this.options
if (crossorigin) {
tags = tags.replace(
new RegExp(reference, 'g'),
`${reference} crossorigin="${crossorigin}"`
)
}
return tags
}
renderResourceHints (renderContext) {
return this.addAttrs(renderContext.renderResourceHints(), null, 'rel="preload"')
}
renderScripts (renderContext) {
return this.addAttrs(renderContext.renderScripts(), 'script')
}
renderStyles (renderContext) {
return this.addAttrs(renderContext.renderStyles(), 'link')
}
getPreloadFiles (renderContext) {
return renderContext.getPreloadFiles()
}
renderResourceHints (renderContext) {
const resourceHints = renderContext.renderResourceHints()
const { render: { crossorigin } } = this.options
if (!crossorigin) {
return resourceHints
}
return resourceHints.replace(
/rel="preload"/g,
`rel="preload" crossorigin="${crossorigin}"`
)
}
createRenderer () {
// Create bundle renderer for SSR
return createBundleRenderer(
@ -144,7 +149,7 @@ export default class SSRRenderer extends BaseRenderer {
}
// Inject styles
HEAD += renderContext.renderStyles()
HEAD += this.renderStyles(renderContext)
if (meta) {
const prependInjectorOptions = { pbody: true }