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
7f604a50
Unverified
Commit
7f604a50
authored
May 19, 2020
by
Slawek Kolodziej
Committed by
GitHub
May 19, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix building server-side generated AMP pages (#13046)
parent
dde5d3bd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
5 deletions
+75
-5
index.ts
packages/next/build/index.ts
+21
-1
worker.js
packages/next/export/worker.js
+8
-0
next-server.ts
packages/next/next-server/server/next-server.ts
+3
-4
[slug].js
test/integration/amphtml-ssg/pages/blog/[slug].js
+27
-0
index.test.js
test/integration/amphtml-ssg/test/index.test.js
+16
-0
No files found.
packages/next/build/index.ts
View file @
7f604a50
...
@@ -838,8 +838,12 @@ export default async function build(dir: string, conf = null): Promise<void> {
...
@@ -838,8 +838,12 @@ export default async function build(dir: string, conf = null): Promise<void> {
await
moveExportedPage
(
page
,
file
,
isSsg
,
'html'
)
await
moveExportedPage
(
page
,
file
,
isSsg
,
'html'
)
}
}
if
(
hasAmp
)
{
if
(
hasAmp
&&
(
!
isSsg
||
(
isSsg
&&
!
isDynamic
))
)
{
await
moveExportedPage
(
`
${
page
}
.amp`
,
`
${
file
}
.amp`
,
isSsg
,
'html'
)
await
moveExportedPage
(
`
${
page
}
.amp`
,
`
${
file
}
.amp`
,
isSsg
,
'html'
)
if
(
isSsg
)
{
await
moveExportedPage
(
`
${
page
}
.amp`
,
`
${
file
}
.amp`
,
isSsg
,
'json'
)
}
}
}
if
(
isSsg
)
{
if
(
isSsg
)
{
...
@@ -862,6 +866,22 @@ export default async function build(dir: string, conf = null): Promise<void> {
...
@@ -862,6 +866,22 @@ export default async function build(dir: string, conf = null): Promise<void> {
for
(
const
route
of
extraRoutes
)
{
for
(
const
route
of
extraRoutes
)
{
await
moveExportedPage
(
route
,
route
,
true
,
'html'
)
await
moveExportedPage
(
route
,
route
,
true
,
'html'
)
await
moveExportedPage
(
route
,
route
,
true
,
'json'
)
await
moveExportedPage
(
route
,
route
,
true
,
'json'
)
if
(
hasAmp
)
{
await
moveExportedPage
(
`
${
route
}
.amp`
,
`
${
route
}
.amp`
,
true
,
'html'
)
await
moveExportedPage
(
`
${
route
}
.amp`
,
`
${
route
}
.amp`
,
true
,
'json'
)
}
finalPrerenderRoutes
[
route
]
=
{
finalPrerenderRoutes
[
route
]
=
{
initialRevalidateSeconds
:
initialRevalidateSeconds
:
exportConfig
.
initialPageRevalidationMap
[
route
],
exportConfig
.
initialPageRevalidationMap
[
route
],
...
...
packages/next/export/worker.js
View file @
7f604a50
...
@@ -269,6 +269,14 @@ export default async function ({
...
@@ -269,6 +269,14 @@ export default async function ({
JSON
.
stringify
(
curRenderOpts
.
pageData
),
JSON
.
stringify
(
curRenderOpts
.
pageData
),
'utf8'
'utf8'
)
)
if
(
curRenderOpts
.
hybridAmp
)
{
await
promises
.
writeFile
(
dataFile
.
replace
(
/
\.
json$/
,
'.amp.json'
),
JSON
.
stringify
(
curRenderOpts
.
pageData
),
'utf8'
)
}
}
}
results
.
fromBuildExportRevalidate
=
curRenderOpts
.
revalidate
results
.
fromBuildExportRevalidate
=
curRenderOpts
.
revalidate
...
...
packages/next/next-server/server/next-server.ts
View file @
7f604a50
...
@@ -1025,9 +1025,7 @@ export default class Server {
...
@@ -1025,9 +1025,7 @@ export default class Server {
}
}
// Compute the iSSG cache key
// Compute the iSSG cache key
let
urlPathname
=
`
${
parseUrl
(
req
.
url
||
''
).
pathname
!
}${
let
urlPathname
=
`
${
parseUrl
(
req
.
url
||
''
).
pathname
!
}
`
query
.
amp
?
'.amp'
:
''
}
`
// remove /_next/data prefix from urlPathname so it matches
// remove /_next/data prefix from urlPathname so it matches
// for direct page visit and /_next/data visit
// for direct page visit and /_next/data visit
...
@@ -1039,10 +1037,11 @@ export default class Server {
...
@@ -1039,10 +1037,11 @@ export default class Server {
const
ssgCacheKey
=
isPreviewMode
const
ssgCacheKey
=
isPreviewMode
?
undefined
// Preview mode bypasses the cache
?
undefined
// Preview mode bypasses the cache
:
urlPathname
:
`
${
urlPathname
}${
query
.
amp
?
'.amp'
:
''
}
`
// Complete the response with cached data if its present
// Complete the response with cached data if its present
const
cachedData
=
ssgCacheKey
?
await
getSprCache
(
ssgCacheKey
)
:
undefined
const
cachedData
=
ssgCacheKey
?
await
getSprCache
(
ssgCacheKey
)
:
undefined
if
(
cachedData
)
{
if
(
cachedData
)
{
const
data
=
isDataReq
const
data
=
isDataReq
?
JSON
.
stringify
(
cachedData
.
pageData
)
?
JSON
.
stringify
(
cachedData
.
pageData
)
...
...
test/integration/amphtml-ssg/pages/blog/[slug].js
0 → 100644
View file @
7f604a50
import
{
useAmp
}
from
'next/amp'
export
const
config
=
{
amp
:
'hybrid'
,
}
export
const
getStaticProps
=
()
=>
{
return
{
props
:
{
hello
:
'hello'
,
random
:
Math
.
random
(),
},
}
}
export
const
getStaticPaths
=
()
=>
({
paths
:
[
'/blog/post-1'
,
'/blog/post-2'
],
fallback
:
false
,
})
export
default
({
hello
,
random
})
=>
(
<>
<
p
id
=
"use-amp"
>
useAmp
:
{
useAmp
()
?
'yes'
:
'no'
}
<
/p
>
<
p
id
=
"hello"
>
{
hello
}
<
/p
>
<
p
id
=
"random"
>
{
random
}
<
/p
>
<
/
>
)
test/integration/amphtml-ssg/test/index.test.js
View file @
7f604a50
...
@@ -46,6 +46,20 @@ const runTests = (isDev = false) => {
...
@@ -46,6 +46,20 @@ const runTests = (isDev = false) => {
expect
(
$
(
'#hello'
).
text
()).
toContain
(
'hello'
)
expect
(
$
(
'#hello'
).
text
()).
toContain
(
'hello'
)
})
})
it
(
'should load dynamic hybrid SSG/AMP page'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/blog/post-1'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'#use-amp'
).
text
()).
toContain
(
'no'
)
expect
(
$
(
'#hello'
).
text
()).
toContain
(
'hello'
)
})
it
(
'should load dynamic hybrid SSG/AMP page with query'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/blog/post-1?amp=1'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'#use-amp'
).
text
()).
toContain
(
'yes'
)
expect
(
$
(
'#hello'
).
text
()).
toContain
(
'hello'
)
})
it
(
'should load a hybrid amp page with query correctly'
,
async
()
=>
{
it
(
'should load a hybrid amp page with query correctly'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/hybrid?amp=1'
)
const
html
=
await
renderViaHTTP
(
appPort
,
'/hybrid?amp=1'
)
...
@@ -139,6 +153,8 @@ describe('AMP SSG Support', () => {
...
@@ -139,6 +153,8 @@ describe('AMP SSG Support', () => {
expect
(
await
fsExists
(
outFile
(
'hybrid.html'
))).
toBe
(
true
)
expect
(
await
fsExists
(
outFile
(
'hybrid.html'
))).
toBe
(
true
)
expect
(
await
fsExists
(
outFile
(
'amp.amp.html'
))).
toBe
(
false
)
expect
(
await
fsExists
(
outFile
(
'amp.amp.html'
))).
toBe
(
false
)
expect
(
await
fsExists
(
outFile
(
'hybrid.amp.html'
))).
toBe
(
true
)
expect
(
await
fsExists
(
outFile
(
'hybrid.amp.html'
))).
toBe
(
true
)
expect
(
await
fsExists
(
outFile
(
'blog/post-1.html'
))).
toBe
(
true
)
expect
(
await
fsExists
(
outFile
(
'blog/post-1.amp.html'
))).
toBe
(
true
)
expect
(
expect
(
await
fsExists
(
outFile
(
join
(
'_next/data'
,
buildId
,
'amp.json'
)))
await
fsExists
(
outFile
(
join
(
'_next/data'
,
buildId
,
'amp.json'
)))
...
...
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