Skip to content

feat(ssr): support custom attributes in render scripts #11094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type TemplateRendererOptions = {
template?: string | (content: string, context: any) => string;
inject?: boolean;
clientManifest?: ClientManifest;
getPrefetchLinkAttrs?: (ref: Resource) => Resource;
getPreloadLinkAttrs?: (ref: Resource) => Resource;
getScriptAttrs?: (ref: Resource) => Resource;
shouldPreload?: (file: string, type: string) => boolean;
shouldPrefetch?: (file: string, type: string) => boolean;
serializer?: Function;
Expand All @@ -37,6 +40,7 @@ type Resource = {
extension: string;
fileWithoutQuery: string;
asType: string;
attrs?: string;
};

export default class TemplateRenderer {
Expand All @@ -55,7 +59,7 @@ export default class TemplateRenderer {
this.inject = options.inject !== false
// if no template option is provided, the renderer is created
// as a utility object for rendering assets like preload links and scripts.

const { template } = options
this.parsedTemplate = template
? typeof template === 'string'
Expand Down Expand Up @@ -156,10 +160,13 @@ export default class TemplateRenderer {

renderPreloadLinks (context: Object): string {
const files = this.getPreloadFiles(context)
const shouldPreload = this.options.shouldPreload
if (files.length) {
return files.map(({ file, extension, fileWithoutQuery, asType }) => {
let extra = ''
const { getPreloadLinkAttrs, shouldPreload } = this.options
return files.map(ref => {
if(typeof getPreloadLinkAttrs === 'function') {
ref = getPreloadLinkAttrs(ref)
}
const { file, extension, fileWithoutQuery, asType, attrs } = ref
// by default, we only preload scripts or css
if (!shouldPreload && asType !== 'script' && asType !== 'style') {
return ''
Expand All @@ -168,6 +175,9 @@ export default class TemplateRenderer {
if (shouldPreload && !shouldPreload(fileWithoutQuery, asType)) {
return ''
}

let extra = attrs || ''

if (asType === 'font') {
extra = ` type="font/${extension}" crossorigin`
}
Expand All @@ -185,20 +195,25 @@ export default class TemplateRenderer {
}

renderPrefetchLinks (context: Object): string {
const shouldPrefetch = this.options.shouldPrefetch
const { getPrefetchLinkAttrs, shouldPrefetch } = this.options
if (this.prefetchFiles) {
const usedAsyncFiles = this.getUsedAsyncFiles(context)
const alreadyRendered = file => {
return usedAsyncFiles && usedAsyncFiles.some(f => f.file === file)
}
return this.prefetchFiles.map(({ file, fileWithoutQuery, asType }) => {
return this.prefetchFiles.map(ref => {
if(typeof getPrefetchLinkAttrs === 'function') {
ref = getPrefetchLinkAttrs(ref)
}
const { file, fileWithoutQuery, asType } = ref
if (shouldPrefetch && !shouldPrefetch(fileWithoutQuery, asType)) {
return ''
}
if (alreadyRendered(file)) {
return ''
}
return `<link rel="prefetch" href="${this.publicPath}${file}">`
const { attrs='rel="prefetch"' } = ref
return `<link ${attrs} href="${this.publicPath}${file}">`
}).join('')
} else {
return ''
Expand All @@ -225,8 +240,13 @@ export default class TemplateRenderer {
const initial = this.preloadFiles.filter(({ file }) => isJS(file))
const async = (this.getUsedAsyncFiles(context) || []).filter(({ file }) => isJS(file))
const needed = [initial[0]].concat(async, initial.slice(1))
return needed.map(({ file }) => {
return `<script src="${this.publicPath}${file}" defer></script>`
const { getScriptAttrs } = this.options
return needed.map((ref) => {
if(typeof getScriptAttrs === 'function') {
ref = getScriptAttrs(ref)
}
const { file, attrs = 'defer' } = ref
return `<script src="${this.publicPath}${file}" ${attrs}></script>`
}).join('')
} else {
return ''
Expand Down