Skip to content

Fix #9847 Add shouldRenderAsyncScripts option #10794

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
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/server/create-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type RenderOptions = {
basedir?: string;
shouldPreload?: Function;
shouldPrefetch?: Function;
shouldRenderAsyncScripts?: boolean;
clientManifest?: ClientManifest;
serializer?: Function;
runInNewContext?: boolean | 'once';
Expand All @@ -42,6 +43,7 @@ export function createRenderer ({
cache,
shouldPreload,
shouldPrefetch,
shouldRenderAsyncScripts,
clientManifest,
serializer
}: RenderOptions = {}): Renderer {
Expand All @@ -51,6 +53,7 @@ export function createRenderer ({
inject,
shouldPreload,
shouldPrefetch,
shouldRenderAsyncScripts,
clientManifest,
serializer
})
Expand Down
5 changes: 4 additions & 1 deletion src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TemplateRendererOptions = {
clientManifest?: ClientManifest;
shouldPreload?: (file: string, type: string) => boolean;
shouldPrefetch?: (file: string, type: string) => boolean;
shouldRenderAsyncScripts?: boolean;
serializer?: Function;
};

Expand Down Expand Up @@ -221,9 +222,11 @@ export default class TemplateRenderer {
}

renderScripts (context: Object): string {
const shouldRenderAsyncScripts = this.options.shouldRenderAsyncScripts !== false

if (this.clientManifest) {
const initial = this.preloadFiles.filter(({ file }) => isJS(file))
const async = (this.getUsedAsyncFiles(context) || []).filter(({ file }) => isJS(file))
const async = ((shouldRenderAsyncScripts && 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>`
Expand Down
21 changes: 20 additions & 1 deletion test/ssr/ssr-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe('SSR: template option', () => {
// manifest chunk should be first
`<script src="/manifest.js" defer></script>` +
// async chunks should be before main chunk
`<script src="/0.js" defer></script>` +
(options.noAsyncScripts ? ``: `<script src="/0.js" defer></script>`) +
`<script src="/main.js" defer></script>` +
`</body></html>`

Expand Down Expand Up @@ -442,6 +442,25 @@ describe('SSR: template option', () => {
})
})

it('bundleRenderer + renderToStream + clientManifest + shouldRenderAsyncScripts', done => {
createRendererWithManifest('split.js', {
runInNewContext,
shouldRenderAsyncScripts: false
}, renderer => {
const stream = renderer.renderToStream({ state: { a: 1 }})
let res = ''
stream.on('data', chunk => {
res += chunk.toString()
})
stream.on('end', () => {
expect(res).toContain(expectedHTMLWithManifest({
noAsyncScripts: true
}))
done()
})
})
})

it('bundleRenderer + renderToString + clientManifest + inject: false', done => {
createRendererWithManifest('split.js', {
runInNewContext,
Expand Down