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
ed5dc210
Commit
ed5dc210
authored
Sep 15, 2019
by
JJ Kasper
Committed by
Tim Neutkens
Sep 15, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add autoExport to __NEXT_DATA__ (#8746)
* Add autoExport to __NEXT_DATA__ * Update render variable
parent
00e0281d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
12 deletions
+56
-12
utils.ts
packages/next/next-server/lib/utils.ts
+1
-0
render.tsx
packages/next/next-server/server/render.tsx
+18
-12
index.test.js
test/integration/production/test/index.test.js
+18
-0
index.test.js
test/integration/serverless/test/index.test.js
+19
-0
No files found.
packages/next/next-server/lib/utils.ts
View file @
ed5dc210
...
...
@@ -65,6 +65,7 @@ export type NEXT_DATA = {
assetPrefix
?:
string
runtimeConfig
?:
{
[
key
:
string
]:
any
}
nextExport
?:
boolean
autoExport
?:
boolean
skeleton
?:
boolean
dynamicIds
?:
string
[]
err
?:
Error
&
{
statusCode
?:
number
}
...
...
packages/next/next-server/server/render.tsx
View file @
ed5dc210
...
...
@@ -131,6 +131,7 @@ type RenderOpts = {
dangerousAsPath
:
string
assetPrefix
?:
string
err
?:
Error
|
null
autoExport
?:
boolean
nextExport
?:
boolean
skeleton
?:
boolean
dev
?:
boolean
...
...
@@ -163,6 +164,7 @@ function renderDocument(
assetPrefix
,
runtimeConfig
,
nextExport
,
autoExport
,
skeleton
,
dynamicImportsIds
,
dangerousAsPath
,
...
...
@@ -207,6 +209,7 @@ function renderDocument(
assetPrefix
:
assetPrefix
===
''
?
undefined
:
assetPrefix
,
// send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML
runtimeConfig
,
// runtimeConfig if provided, otherwise don't sent in the resulting HTML
nextExport
,
// If this is a page exported by `next export`
autoExport
,
// If this is an auto exported page
skeleton
,
// If this is a skeleton page for experimentalPrerender
dynamicIds
:
dynamicImportsIds
.
length
===
0
?
undefined
:
dynamicImportsIds
,
...
...
@@ -255,8 +258,20 @@ export async function renderToHTML(
}
=
renderOpts
await
Loadable
.
preloadAll
()
// Make sure all dynamic imports are loaded
let
isStaticPage
=
pageConfig
.
experimentalPrerender
===
true
let
isSkeleton
=
false
const
defaultAppGetInitialProps
=
App
.
getInitialProps
===
(
App
as
any
).
origGetInitialProps
let
isAutoExport
=
typeof
(
Component
as
any
).
getInitialProps
!==
'function'
&&
defaultAppGetInitialProps
let
isPrerender
=
pageConfig
.
experimentalPrerender
===
true
const
isStaticPage
=
isPrerender
||
isAutoExport
// TODO: revisit `?_nextPreviewSkeleton=(truthy)`
const
isSkeleton
=
isPrerender
&&
!!
query
.
_nextPreviewSkeleton
// remove from query so it doesn't end up in document
delete
query
.
_nextPreviewSkeleton
if
(
dev
)
{
const
{
isValidElementType
}
=
require
(
'react-is'
)
...
...
@@ -278,11 +293,6 @@ export async function renderToHTML(
)
}
isStaticPage
=
typeof
(
Component
as
any
).
getInitialProps
!==
'function'
const
defaultAppGetInitialProps
=
App
.
getInitialProps
===
(
App
as
any
).
origGetInitialProps
isStaticPage
=
isStaticPage
&&
defaultAppGetInitialProps
if
(
isStaticPage
)
{
// remove query values except ones that will be set during export
query
=
{
...
...
@@ -292,12 +302,8 @@ export async function renderToHTML(
renderOpts
.
nextExport
=
true
}
}
// might want to change previewing of skeleton from `?_nextPreviewSkeleton=(truthy)`
isSkeleton
=
pageConfig
.
experimentalPrerender
===
true
&&
!!
query
.
_nextPreviewSkeleton
// remove from query so it doesn't end up in document
delete
query
.
_nextPreviewSkeleton
if
(
isSkeleton
)
renderOpts
.
nextExport
=
true
if
(
isAutoExport
)
renderOpts
.
autoExport
=
true
// @ts-ignore url will always be set
const
asPath
:
string
=
req
.
url
...
...
test/integration/production/test/index.test.js
View file @
ed5dc210
...
...
@@ -427,6 +427,24 @@ describe('Production Usage', () => {
expect
(
script
).
not
.
toMatch
(
/runtimeConfig/
)
})
it
(
'should add autoExport for auto pre-rendered pages'
,
async
()
=>
{
for
(
const
page
of
[
'/'
,
'/about'
])
{
const
html
=
await
renderViaHTTP
(
appPort
,
page
)
const
$
=
cheerio
.
load
(
html
)
const
data
=
JSON
.
parse
(
$
(
'#__NEXT_DATA__'
).
html
())
expect
(
data
.
autoExport
).
toBe
(
true
)
}
})
it
(
'should not add autoExport for non pre-rendered pages'
,
async
()
=>
{
for
(
const
page
of
[
'/query'
])
{
const
html
=
await
renderViaHTTP
(
appPort
,
page
)
const
$
=
cheerio
.
load
(
html
)
const
data
=
JSON
.
parse
(
$
(
'#__NEXT_DATA__'
).
html
())
expect
(
!!
data
.
autoExport
).
toBe
(
false
)
}
})
if
(
browserName
===
'chrome'
)
{
it
(
'should add preload tags when Link prefetch prop is used'
,
async
()
=>
{
const
browser
=
await
webdriver
(
appPort
,
'/prefetch'
)
...
...
test/integration/serverless/test/index.test.js
View file @
ed5dc210
...
...
@@ -2,6 +2,7 @@
/* global jasmine */
import
webdriver
from
'next-webdriver'
import
{
join
}
from
'path'
import
cheerio
from
'cheerio'
import
{
existsSync
,
readdirSync
,
readFileSync
}
from
'fs'
import
{
killApp
,
...
...
@@ -34,6 +35,24 @@ describe('Serverless', () => {
expect
(
html
).
toMatch
(
/Hello World/
)
})
it
(
'should add autoExport for auto pre-rendered pages'
,
async
()
=>
{
for
(
const
page
of
[
'/'
,
'/abc'
])
{
const
html
=
await
renderViaHTTP
(
appPort
,
page
)
const
$
=
cheerio
.
load
(
html
)
const
data
=
JSON
.
parse
(
$
(
'#__NEXT_DATA__'
).
html
())
expect
(
data
.
autoExport
).
toBe
(
true
)
}
})
it
(
'should not add autoExport for non pre-rendered pages'
,
async
()
=>
{
for
(
const
page
of
[
'/fetch'
])
{
const
html
=
await
renderViaHTTP
(
appPort
,
page
)
const
$
=
cheerio
.
load
(
html
)
const
data
=
JSON
.
parse
(
$
(
'#__NEXT_DATA__'
).
html
())
expect
(
!!
data
.
autoExport
).
toBe
(
false
)
}
})
it
(
'should serve file from public folder'
,
async
()
=>
{
const
content
=
await
renderViaHTTP
(
appPort
,
'/hello.txt'
)
expect
(
content
.
trim
()).
toBe
(
'hello world'
)
...
...
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