Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
e608c86b
Unverified
Commit
e608c86b
authored
Feb 29, 2020
by
Joe Haddad
Committed by
GitHub
Feb 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate Low Priority Files from Main Files (#10756)
* Separate Low Priority Files from Main Files * Fix tests
parent
5a828125
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
40 additions
and
38 deletions
+40
-38
build-manifest-plugin.ts
packages/next/build/webpack/plugins/build-manifest-plugin.ts
+9
-13
utils.ts
packages/next/next-server/lib/utils.ts
+1
-0
get-page-files.ts
packages/next/next-server/server/get-page-files.ts
+2
-0
render.tsx
packages/next/next-server/server/render.tsx
+7
-2
_document.tsx
packages/next/pages/_document.tsx
+5
-20
index.test.js
test/integration/chunking/test/index.test.js
+16
-3
No files found.
packages/next/build/webpack/plugins/build-manifest-plugin.ts
View file @
e608c86b
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`
)
)
}
}
...
...
packages/next/next-server/lib/utils.ts
View file @
e608c86b
...
@@ -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
...
...
packages/next/next-server/server/get-page-files.ts
View file @
e608c86b
...
@@ -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
[]
}
}
}
}
...
...
packages/next/next-server/server/render.tsx
View file @
e608c86b
...
@@ -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
,
})
})
...
...
packages/next/pages/_document.tsx
View file @
e608c86b
...
@@ -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
=>
{
...
...
test/integration/chunking/test/index.test.js
View file @
e608c86b
/* 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
')) {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment