Unverified Commit e608c86b authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Separate Low Priority Files from Main Files (#10756)

* Separate Low Priority Files from Main Files

* Fix tests
parent 5a828125
import devalue from 'devalue' import devalue from 'devalue'
import { Compiler } from 'webpack' import { Compiler } from 'webpack'
import { RawSource } from 'webpack-sources' import { RawSource } from 'webpack-sources'
import { import {
BUILD_MANIFEST, BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
...@@ -10,19 +9,12 @@ import { ...@@ -10,19 +9,12 @@ import {
IS_BUNDLED_PAGE_REGEX, IS_BUNDLED_PAGE_REGEX,
ROUTE_NAME_REGEX, ROUTE_NAME_REGEX,
} from '../../../next-server/lib/constants' } from '../../../next-server/lib/constants'
import { BuildManifest } from '../../../next-server/server/get-page-files'
interface AssetMap {
devFiles: string[]
pages: {
'/_app': string[]
[s: string]: string[]
}
}
// This function takes the asset map generated in BuildManifestPlugin and creates a // This function takes the asset map generated in BuildManifestPlugin and creates a
// reduced version to send to the client. // reduced version to send to the client.
const generateClientManifest = ( const generateClientManifest = (
assetMap: AssetMap, assetMap: BuildManifest,
isModern: boolean isModern: boolean
): string => { ): string => {
const clientManifest: { [s: string]: string[] } = {} const clientManifest: { [s: string]: string[] } = {}
...@@ -68,7 +60,11 @@ export default class BuildManifestPlugin { ...@@ -68,7 +60,11 @@ export default class BuildManifestPlugin {
'NextJsBuildManifest', 'NextJsBuildManifest',
(compilation, callback) => { (compilation, callback) => {
const { chunks } = compilation const { chunks } = compilation
const assetMap: AssetMap = { devFiles: [], pages: { '/_app': [] } } const assetMap: BuildManifest = {
devFiles: [],
lowPriorityFiles: [],
pages: { '/_app': [] },
}
const mainJsChunk = chunks.find( const mainJsChunk = chunks.find(
c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
...@@ -141,11 +137,11 @@ export default class BuildManifestPlugin { ...@@ -141,11 +137,11 @@ export default class BuildManifestPlugin {
// as a dependency for the app. If the flag is false, the file won't be // as a dependency for the app. If the flag is false, the file won't be
// downloaded by the client. // downloaded by the client.
if (this.clientManifest) { if (this.clientManifest) {
assetMap.pages['/_app'].push( assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js` `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
) )
if (this.modern) { if (this.modern) {
assetMap.pages['/_app'].push( assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.module.js` `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.module.js`
) )
} }
......
...@@ -153,6 +153,7 @@ export type DocumentProps = DocumentInitialProps & { ...@@ -153,6 +153,7 @@ export type DocumentProps = DocumentInitialProps & {
hasCssMode: boolean hasCssMode: boolean
devFiles: string[] devFiles: string[]
files: string[] files: string[]
lowPriorityFiles: string[]
polyfillFiles: string[] polyfillFiles: string[]
dynamicImports: ManifestItem[] dynamicImports: ManifestItem[]
assetPrefix?: string assetPrefix?: string
......
...@@ -2,7 +2,9 @@ import { normalizePagePath } from './normalize-page-path' ...@@ -2,7 +2,9 @@ import { normalizePagePath } from './normalize-page-path'
export type BuildManifest = { export type BuildManifest = {
devFiles: string[] devFiles: string[]
lowPriorityFiles: string[]
pages: { pages: {
'/_app': string[]
[page: string]: string[] [page: string]: string[]
} }
} }
......
...@@ -174,6 +174,7 @@ function renderDocument( ...@@ -174,6 +174,7 @@ function renderDocument(
staticMarkup, staticMarkup,
devFiles, devFiles,
files, files,
lowPriorityFiles,
polyfillFiles, polyfillFiles,
dynamicImports, dynamicImports,
htmlProps, htmlProps,
...@@ -191,8 +192,9 @@ function renderDocument( ...@@ -191,8 +192,9 @@ function renderDocument(
hybridAmp: boolean hybridAmp: boolean
dynamicImportsIds: string[] dynamicImportsIds: string[]
dynamicImports: ManifestItem[] dynamicImports: ManifestItem[]
files: string[]
devFiles: string[] devFiles: string[]
files: string[]
lowPriorityFiles: string[]
polyfillFiles: string[] polyfillFiles: string[]
htmlProps: any htmlProps: any
bodyTags: any bodyTags: any
...@@ -229,6 +231,7 @@ function renderDocument( ...@@ -229,6 +231,7 @@ function renderDocument(
staticMarkup, staticMarkup,
devFiles, devFiles,
files, files,
lowPriorityFiles,
polyfillFiles, polyfillFiles,
dynamicImports, dynamicImports,
assetPrefix, assetPrefix,
...@@ -579,6 +582,7 @@ export async function renderToHTML( ...@@ -579,6 +582,7 @@ export async function renderToHTML(
...getPageFiles(buildManifest, pathname), ...getPageFiles(buildManifest, pathname),
]), ]),
] ]
const lowPriorityFiles = buildManifest.lowPriorityFiles
const polyfillFiles = getPageFiles(buildManifest, '/_polyfills') const polyfillFiles = getPageFiles(buildManifest, '/_polyfills')
const renderElementToString = staticMarkup const renderElementToString = staticMarkup
...@@ -674,8 +678,9 @@ export async function renderToHTML( ...@@ -674,8 +678,9 @@ export async function renderToHTML(
hybridAmp, hybridAmp,
dynamicImportsIds, dynamicImportsIds,
dynamicImports, dynamicImports,
files,
devFiles, devFiles,
files,
lowPriorityFiles,
polyfillFiles, polyfillFiles,
}) })
......
...@@ -44,10 +44,6 @@ function getOptionalModernScriptVariant(path: string) { ...@@ -44,10 +44,6 @@ function getOptionalModernScriptVariant(path: string) {
return path return path
} }
function isLowPriority(file: string) {
return file.includes('_buildManifest')
}
/** /**
* `Document` component handles the initial `document` markup and renders only on the server side. * `Document` component handles the initial `document` markup and renders only on the server side.
* Commonly used for implementing server side rendering for `css-in-js` libraries. * Commonly used for implementing server side rendering for `css-in-js` libraries.
...@@ -260,13 +256,7 @@ export class Head extends Component< ...@@ -260,13 +256,7 @@ export class Head extends Component<
// `dynamicImports` will contain both `.js` and `.module.js` when // `dynamicImports` will contain both `.js` and `.module.js` when
// the feature is enabled. This clause will filter down to the // the feature is enabled. This clause will filter down to the
// modern variants only. // modern variants only.
// return file.endsWith(getOptionalModernScriptVariant('.js'))
// Also filter out low priority files because they should not be
// preloaded for performance reasons.
return (
file.endsWith(getOptionalModernScriptVariant('.js')) &&
!isLowPriority(file)
)
}) })
: [] : []
...@@ -589,17 +579,12 @@ export class NextScript extends Component<OriginProps> { ...@@ -589,17 +579,12 @@ export class NextScript extends Component<OriginProps> {
} }
getScripts() { getScripts() {
const { assetPrefix, files } = this.context._documentProps const { assetPrefix, files, lowPriorityFiles } = this.context._documentProps
if (!files || files.length === 0) {
return null
}
const { _devOnlyInvalidateCacheQueryString } = this.context const { _devOnlyInvalidateCacheQueryString } = this.context
const normalScripts = files.filter( const normalScripts = files?.filter(file => file.endsWith('.js'))
file => file.endsWith('.js') && !isLowPriority(file) const lowPriorityScripts = lowPriorityFiles?.filter(file =>
) file.endsWith('.js')
const lowPriorityScripts = files.filter(
file => file.endsWith('.js') && isLowPriority(file)
) )
return [...normalScripts, ...lowPriorityScripts].map(file => { return [...normalScripts, ...lowPriorityScripts].map(file => {
......
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import { join } from 'path' import cheerio from 'cheerio'
import express from 'express' import express from 'express'
import { access, readdir, readFile, unlink } from 'fs-extra'
import http from 'http' import http from 'http'
import { nextBuild, nextServer, promiseCall, stopApp } from 'next-test-utils' import { nextBuild, nextServer, promiseCall, stopApp } from 'next-test-utils'
import { readdir, readFile, unlink, access } from 'fs-extra'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
...@@ -90,6 +90,19 @@ describe('Chunking', () => { ...@@ -90,6 +90,19 @@ describe('Chunking', () => {
).toBe(false) ).toBe(false)
}) })
it('should execute the build manifest', async () => {
const indexPage = await readFile(
join(appDir, '.next', 'server', 'static', buildId, 'pages', 'index.html')
)
const $ = cheerio.load(indexPage)
expect(
Array.from($('script'))
.map(e => e.attribs.src)
.some(entry => entry && entry.includes('_buildManifest'))
).toBe(true)
})
it('should not include more than one instance of react-dom', async () => { it('should not include more than one instance of react-dom', async () => {
const misplacedReactDom = stats.chunks.some(chunk => { const misplacedReactDom = stats.chunks.some(chunk => {
if (chunk.names.includes('framework')) { if (chunk.names.includes('framework')) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment