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
afa73ae4
Unverified
Commit
afa73ae4
authored
Oct 01, 2019
by
Joe Haddad
Committed by
GitHub
Oct 01, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Prerender Manifest (#8918)
* Update Prerender Manifest * Fix typescript
parent
0bcd1fc3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
21 deletions
+78
-21
index.ts
packages/next/build/index.ts
+47
-16
spr-cache.ts
packages/next/next-server/server/spr-cache.ts
+9
-1
index.test.js
test/integration/prerender/test/index.test.js
+22
-4
No files found.
packages/next/build/index.ts
View file @
afa73ae4
import
chalk
from
'chalk'
import
chalk
from
'chalk'
import
fs
from
'fs'
import
fs
from
'fs'
import
Worker
from
'jest-worker'
import
mkdirpOrig
from
'mkdirp'
import
mkdirpOrig
from
'mkdirp'
import
nanoid
from
'next/dist/compiled/nanoid/index.js'
import
path
from
'path'
import
{
promisify
}
from
'util'
import
formatWebpackMessages
from
'../client/dev/error-overlay/format-webpack-messages'
import
{
PUBLIC_DIR_MIDDLEWARE_CONFLICT
}
from
'../lib/constants'
import
{
findPagesDir
}
from
'../lib/find-pages-dir'
import
{
recursiveDelete
}
from
'../lib/recursive-delete'
import
{
recursiveReadDir
}
from
'../lib/recursive-readdir'
import
{
verifyTypeScriptSetup
}
from
'../lib/verifyTypeScriptSetup'
import
{
import
{
PAGES_MANIFEST
,
BUILD_MANIFEST
,
BUILD_MANIFEST
,
PAGES_MANIFEST
,
PHASE_PRODUCTION_BUILD
,
PHASE_PRODUCTION_BUILD
,
PRERENDER_MANIFEST
,
PRERENDER_MANIFEST
,
SERVER_DIRECTORY
,
SERVER_DIRECTORY
,
SERVERLESS_DIRECTORY
,
SERVERLESS_DIRECTORY
,
}
from
'../next-server/lib/constants'
}
from
'../next-server/lib/constants'
import
{
getRouteRegex
,
isDynamicRoute
}
from
'../next-server/lib/router/utils'
import
loadConfig
,
{
import
loadConfig
,
{
isTargetLikeServerless
,
isTargetLikeServerless
,
}
from
'../next-server/server/config'
}
from
'../next-server/server/config'
import
nanoid
from
'next/dist/compiled/nanoid/index.js'
import
path
from
'path'
import
{
promisify
}
from
'util'
import
Worker
from
'jest-worker'
import
formatWebpackMessages
from
'../client/dev/error-overlay/format-webpack-messages'
import
{
recursiveDelete
}
from
'../lib/recursive-delete'
import
{
recursiveReadDir
}
from
'../lib/recursive-readdir'
import
{
verifyTypeScriptSetup
}
from
'../lib/verifyTypeScriptSetup'
import
{
import
{
recordBuildDuration
,
recordBuildDuration
,
recordBuildOptimize
,
recordBuildOptimize
,
...
@@ -31,6 +34,7 @@ import { CompilerResult, runCompiler } from './compiler'
...
@@ -31,6 +34,7 @@ import { CompilerResult, runCompiler } from './compiler'
import
{
createEntrypoints
,
createPagesMapping
}
from
'./entries'
import
{
createEntrypoints
,
createPagesMapping
}
from
'./entries'
import
{
generateBuildId
}
from
'./generate-build-id'
import
{
generateBuildId
}
from
'./generate-build-id'
import
{
isWriteable
}
from
'./is-writeable'
import
{
isWriteable
}
from
'./is-writeable'
import
createSpinner
from
'./spinner'
import
{
import
{
collectPages
,
collectPages
,
getPageSizeInKb
,
getPageSizeInKb
,
...
@@ -41,10 +45,6 @@ import {
...
@@ -41,10 +45,6 @@ import {
import
getBaseWebpackConfig
from
'./webpack-config'
import
getBaseWebpackConfig
from
'./webpack-config'
import
{
getPageChunks
}
from
'./webpack/plugins/chunk-graph-plugin'
import
{
getPageChunks
}
from
'./webpack/plugins/chunk-graph-plugin'
import
{
writeBuildId
}
from
'./write-build-id'
import
{
writeBuildId
}
from
'./write-build-id'
import
{
findPagesDir
}
from
'../lib/find-pages-dir'
import
createSpinner
from
'./spinner'
import
{
PUBLIC_DIR_MIDDLEWARE_CONFLICT
}
from
'../lib/constants'
import
{
isDynamicRoute
}
from
'../next-server/lib/router/utils'
const
fsUnlink
=
promisify
(
fs
.
unlink
)
const
fsUnlink
=
promisify
(
fs
.
unlink
)
const
fsRmdir
=
promisify
(
fs
.
rmdir
)
const
fsRmdir
=
promisify
(
fs
.
rmdir
)
...
@@ -58,12 +58,20 @@ const staticCheckWorker = require.resolve('./utils')
...
@@ -58,12 +58,20 @@ const staticCheckWorker = require.resolve('./utils')
export
type
SprRoute
=
{
export
type
SprRoute
=
{
initialRevalidateSeconds
:
number
|
false
initialRevalidateSeconds
:
number
|
false
dataRoute
:
string
}
export
type
DynamicSprRoute
=
{
routeRegex
:
string
dataRoute
:
string
dataRouteRegex
:
string
}
}
export
type
PrerenderManifest
=
{
export
type
PrerenderManifest
=
{
version
:
number
version
:
number
routes
:
{
[
route
:
string
]:
SprRoute
}
routes
:
{
[
route
:
string
]:
SprRoute
}
dynamicRoutes
:
string
[]
dynamicRoutes
:
{
[
route
:
string
]:
DynamicSprRoute
}
}
}
export
default
async
function
build
(
dir
:
string
,
conf
=
null
):
Promise
<
void
>
{
export
default
async
function
build
(
dir
:
string
,
conf
=
null
):
Promise
<
void
>
{
...
@@ -513,6 +521,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
...
@@ -513,6 +521,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
finalPrerenderRoutes
[
page
]
=
{
finalPrerenderRoutes
[
page
]
=
{
initialRevalidateSeconds
:
initialRevalidateSeconds
:
exportConfig
.
initialPageRevalidationMap
[
page
],
exportConfig
.
initialPageRevalidationMap
[
page
],
dataRoute
:
path
.
posix
.
join
(
'/_next/data'
,
`
${
page
===
'/'
?
'/index'
:
page
}
.json`
),
}
}
}
else
{
}
else
{
// For a dynamic SPR page, we did not copy its html nor data exports.
// For a dynamic SPR page, we did not copy its html nor data exports.
...
@@ -525,6 +537,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
...
@@ -525,6 +537,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
finalPrerenderRoutes
[
route
]
=
{
finalPrerenderRoutes
[
route
]
=
{
initialRevalidateSeconds
:
initialRevalidateSeconds
:
exportConfig
.
initialPageRevalidationMap
[
route
],
exportConfig
.
initialPageRevalidationMap
[
route
],
dataRoute
:
path
.
posix
.
join
(
'/_next/data'
,
`
${
route
===
'/'
?
'/index'
:
route
}
.json`
),
}
}
}
}
}
}
...
@@ -550,10 +566,25 @@ export default async function build(dir: string, conf = null): Promise<void> {
...
@@ -550,10 +566,25 @@ export default async function build(dir: string, conf = null): Promise<void> {
)
)
if
(
sprPages
.
size
>
0
)
{
if
(
sprPages
.
size
>
0
)
{
const
finalDynamicRoutes
:
PrerenderManifest
[
'dynamicRoutes'
]
=
{}
tbdPrerenderRoutes
.
forEach
(
tbdRoute
=>
{
const
dataRoute
=
path
.
posix
.
join
(
'/_next/data'
,
`
${
tbdRoute
===
'/'
?
'/index'
:
tbdRoute
}
.json`
)
finalDynamicRoutes
[
tbdRoute
]
=
{
routeRegex
:
getRouteRegex
(
tbdRoute
).
re
.
source
,
dataRoute
,
dataRouteRegex
:
getRouteRegex
(
dataRoute
.
replace
(
/
\.
json$/
,
''
)
).
re
.
source
.
replace
(
/
\(\?
:
\\\/\)\?\$
$/
,
'
\\
.json$'
),
}
})
const
prerenderManifest
:
PrerenderManifest
=
{
const
prerenderManifest
:
PrerenderManifest
=
{
version
:
1
,
version
:
1
,
routes
:
finalPrerenderRoutes
,
routes
:
finalPrerenderRoutes
,
dynamicRoutes
:
tbdPrerenderRoutes
.
sort
()
,
dynamicRoutes
:
finalDynamicRoutes
,
}
}
await
fsWriteFile
(
await
fsWriteFile
(
...
...
packages/next/next-server/server/spr-cache.ts
View file @
afa73ae4
...
@@ -77,7 +77,7 @@ export function initializeSprCache({
...
@@ -77,7 +77,7 @@ export function initializeSprCache({
fs
.
readFileSync
(
path
.
join
(
distDir
,
PRERENDER_MANIFEST
),
'utf8'
)
fs
.
readFileSync
(
path
.
join
(
distDir
,
PRERENDER_MANIFEST
),
'utf8'
)
)
)
}
catch
(
_
)
{
}
catch
(
_
)
{
prerenderManifest
=
{
version
:
1
,
routes
:
{},
dynamicRoutes
:
[]
}
prerenderManifest
=
{
version
:
1
,
routes
:
{},
dynamicRoutes
:
{}
}
}
}
cache
=
new
LRUCache
({
cache
=
new
LRUCache
({
...
@@ -144,7 +144,13 @@ export async function setSprCache(
...
@@ -144,7 +144,13 @@ export async function setSprCache(
)
{
)
{
if
(
sprOptions
.
dev
)
return
if
(
sprOptions
.
dev
)
return
if
(
typeof
revalidateSeconds
!==
'undefined'
)
{
if
(
typeof
revalidateSeconds
!==
'undefined'
)
{
// TODO: This is really bad. We shouldn't be mutating the manifest from the
// build.
prerenderManifest
.
routes
[
pathname
]
=
{
prerenderManifest
.
routes
[
pathname
]
=
{
dataRoute
:
path
.
posix
.
join
(
'/_next/data'
,
`
${
pathname
===
'/'
?
'/index'
:
pathname
}
.json`
),
initialRevalidateSeconds
:
revalidateSeconds
,
initialRevalidateSeconds
:
revalidateSeconds
,
}
}
}
}
...
@@ -155,6 +161,8 @@ export async function setSprCache(
...
@@ -155,6 +161,8 @@ export async function setSprCache(
revalidateAfter
:
calculateRevalidate
(
pathname
),
revalidateAfter
:
calculateRevalidate
(
pathname
),
})
})
// TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest.
if
(
sprOptions
.
flushToDisk
)
{
if
(
sprOptions
.
flushToDisk
)
{
try
{
try
{
await
writeFile
(
getSeedPath
(
pathname
,
'html'
),
data
.
html
,
'utf8'
)
await
writeFile
(
getSeedPath
(
pathname
,
'html'
),
data
.
html
,
'utf8'
)
...
...
test/integration/prerender/test/index.test.js
View file @
afa73ae4
...
@@ -152,37 +152,55 @@ const runTests = (dev = false) => {
...
@@ -152,37 +152,55 @@ const runTests = (dev = false) => {
expect
(
manifest
.
version
).
toBe
(
1
)
expect
(
manifest
.
version
).
toBe
(
1
)
expect
(
manifest
.
routes
).
toEqual
({
expect
(
manifest
.
routes
).
toEqual
({
'/'
:
{
'/'
:
{
dataRoute
:
'/_next/data/index.json'
,
initialRevalidateSeconds
:
1
initialRevalidateSeconds
:
1
},
},
'/blog/[post3]'
:
{
'/blog/[post3]'
:
{
dataRoute
:
'/_next/data/blog/[post3].json'
,
initialRevalidateSeconds
:
10
initialRevalidateSeconds
:
10
},
},
'/blog/post-1'
:
{
'/blog/post-1'
:
{
dataRoute
:
'/_next/data/blog/post-1.json'
,
initialRevalidateSeconds
:
10
initialRevalidateSeconds
:
10
},
},
'/blog/post-2'
:
{
'/blog/post-2'
:
{
dataRoute
:
'/_next/data/blog/post-2.json'
,
initialRevalidateSeconds
:
10
initialRevalidateSeconds
:
10
},
},
'/blog/post-1/comment-1'
:
{
'/blog/post-1/comment-1'
:
{
dataRoute
:
'/_next/data/blog/post-1/comment-1.json'
,
initialRevalidateSeconds
:
2
initialRevalidateSeconds
:
2
},
},
'/blog/post-2/comment-2'
:
{
'/blog/post-2/comment-2'
:
{
dataRoute
:
'/_next/data/blog/post-2/comment-2.json'
,
initialRevalidateSeconds
:
2
initialRevalidateSeconds
:
2
},
},
'/another'
:
{
'/another'
:
{
dataRoute
:
'/_next/data/another.json'
,
initialRevalidateSeconds
:
0
initialRevalidateSeconds
:
0
},
},
'/default-revalidate'
:
{
'/default-revalidate'
:
{
dataRoute
:
'/_next/data/default-revalidate.json'
,
initialRevalidateSeconds
:
1
initialRevalidateSeconds
:
1
},
},
'/something'
:
{
'/something'
:
{
dataRoute
:
'/_next/data/something.json'
,
initialRevalidateSeconds
:
false
initialRevalidateSeconds
:
false
}
}
})
})
expect
(
manifest
.
dynamicRoutes
).
toEqual
([
expect
(
manifest
.
dynamicRoutes
).
toEqual
({
'/blog/[post]'
,
'/blog/[post]'
:
{
'/blog/[post]/[comment]'
dataRoute
:
'/_next/data/blog/[post].json'
,
])
dataRouteRegex
:
'^
\\
/_next
\\
/data
\\
/blog
\\
/([^
\\
/]+?)
\\
.json$'
,
routeRegex
:
'^
\\
/blog
\\
/([^
\\
/]+?)(?:
\\
/)?$'
},
'/blog/[post]/[comment]'
:
{
dataRoute
:
'/_next/data/blog/[post]/[comment].json'
,
dataRouteRegex
:
'^
\\
/_next
\\
/data
\\
/blog
\\
/([^
\\
/]+?)
\\
/([^
\\
/]+?)
\\
.json$'
,
routeRegex
:
'^
\\
/blog
\\
/([^
\\
/]+?)
\\
/([^
\\
/]+?)(?:
\\
/)?$'
}
})
})
})
it
(
'outputs prerendered files correctly'
,
async
()
=>
{
it
(
'outputs prerendered files correctly'
,
async
()
=>
{
...
...
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