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
bce75ae9
Unverified
Commit
bce75ae9
authored
Mar 19, 2019
by
JJ Kasper
Committed by
GitHub
Mar 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add checking for corresponding pages for link tags (#6719)
parent
2cc8365d
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
85 additions
and
19 deletions
+85
-19
load-components.ts
packages/next-server/server/load-components.ts
+6
-3
next-server.ts
packages/next-server/server/next-server.ts
+7
-4
render.tsx
packages/next-server/server/render.tsx
+6
-0
require.ts
packages/next-server/server/require.ts
+16
-2
_document.js
packages/next/pages/_document.js
+3
-2
next-dev-server.js
packages/next/server/next-dev-server.js
+1
-0
on-demand-entry-handler.js
packages/next/server/on-demand-entry-handler.js
+5
-1
normal.js
test/integration/amphtml/pages/normal.js
+1
-0
only-amp.amp.js
test/integration/amphtml/pages/only-amp.amp.js
+1
-0
index.test.js
test/integration/amphtml/test/index.test.js
+36
-4
require-page.test.js
test/isolated/require-page.test.js
+3
-3
No files found.
packages/next-server/server/load-components.ts
View file @
bce75ae9
...
...
@@ -11,13 +11,16 @@ export async function loadComponents(distDir: string, buildId: string, pathname:
const
documentPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_document'
)
const
appPath
=
join
(
distDir
,
SERVER_DIRECTORY
,
CLIENT_STATIC_FILES_PATH
,
buildId
,
'pages'
,
'_app'
)
const
[
buildManifest
,
reactLoadableManifest
,
Component
,
Document
,
App
]
=
await
Promise
.
all
([
const
[
buildManifest
,
reactLoadableManifest
,
pageData
,
Document
,
App
]
=
await
Promise
.
all
([
require
(
join
(
distDir
,
BUILD_MANIFEST
)),
require
(
join
(
distDir
,
REACT_LOADABLE_MANIFEST
)),
interopDefault
(
requirePage
(
pathname
,
distDir
,
opts
)
),
requirePage
(
pathname
,
distDir
,
opts
),
interopDefault
(
require
(
documentPath
)),
interopDefault
(
require
(
appPath
)),
])
return
{
buildManifest
,
reactLoadableManifest
,
Component
,
Document
,
App
}
const
Component
=
interopDefault
(
pageData
.
mod
)
const
{
hasAmp
}
=
pageData
return
{
buildManifest
,
reactLoadableManifest
,
Component
,
Document
,
App
,
hasAmp
}
}
packages/next-server/server/next-server.ts
View file @
bce75ae9
...
...
@@ -285,8 +285,8 @@ export default class Server {
query
:
ParsedUrlQuery
=
{},
opts
:
any
,
)
{
const
result
=
await
loadComponents
(
this
.
distDir
,
this
.
buildId
,
pathname
,
{
amphtml
:
!!
opts
.
amphtml
}
)
return
renderToHTML
(
req
,
res
,
pathname
,
query
,
{
...
result
,
...
opts
})
const
result
=
await
loadComponents
(
this
.
distDir
,
this
.
buildId
,
pathname
,
opts
)
return
renderToHTML
(
req
,
res
,
pathname
,
query
,
{
...
result
,
...
opts
,
hasAmp
:
result
.
hasAmp
})
}
public
async
renderToHTML
(
...
...
@@ -294,7 +294,10 @@ export default class Server {
res
:
ServerResponse
,
pathname
:
string
,
query
:
ParsedUrlQuery
=
{},
{
amphtml
}:
{
amphtml
?:
boolean
}
=
{},
{
amphtml
,
hasAmp
}:
{
amphtml
?:
boolean
,
hasAmp
?:
boolean
,
}
=
{},
):
Promise
<
string
|
null
>
{
try
{
// To make sure the try/catch is executed
...
...
@@ -303,7 +306,7 @@ export default class Server {
res
,
pathname
,
query
,
{
...
this
.
renderOpts
,
amphtml
},
{
...
this
.
renderOpts
,
amphtml
,
hasAmp
},
)
return
html
}
catch
(
err
)
{
...
...
packages/next-server/server/render.tsx
View file @
bce75ae9
...
...
@@ -116,6 +116,7 @@ type RenderOpts = {
nextExport
?:
boolean
dev
?:
boolean
amphtml
?:
boolean
hasAmp
?:
boolean
buildManifest
:
BuildManifest
reactLoadableManifest
:
ReactLoadableManifest
Component
:
React
.
ComponentType
...
...
@@ -140,6 +141,7 @@ function renderDocument(
err
,
dev
,
amphtml
,
hasAmp
,
staticMarkup
,
devFiles
,
files
,
...
...
@@ -150,6 +152,7 @@ function renderDocument(
pathname
:
string
query
:
ParsedUrlQuery
amphtml
:
boolean
hasAmp
:
boolean
,
dynamicImportsIds
:
string
[]
dynamicImports
:
ManifestItem
[]
files
:
string
[]
...
...
@@ -175,6 +178,7 @@ function renderDocument(
}
}
ampEnabled=
{
ampEnabled
}
amphtml=
{
amphtml
}
hasAmp=
{
hasAmp
}
staticMarkup=
{
staticMarkup
}
devFiles=
{
devFiles
}
files=
{
files
}
...
...
@@ -200,6 +204,7 @@ export async function renderToHTML(
dev
=
false
,
staticMarkup
=
false
,
amphtml
=
false
,
hasAmp
=
false
,
App
,
Document
,
Component
,
...
...
@@ -310,6 +315,7 @@ export async function renderToHTML(
docProps
,
pathname
,
amphtml
,
hasAmp
,
query
,
dynamicImportsIds
,
dynamicImports
,
...
...
packages/next-server/server/require.ts
View file @
bce75ae9
...
...
@@ -51,7 +51,21 @@ export function getPagePath(page: string, distDir: string, opts: PagePathOptions
return
join
(
serverBuildPath
,
pagesManifest
[
page
])
}
export
function
requirePage
(
page
:
string
,
distDir
:
string
,
opts
?:
PagePathOptions
):
any
{
export
function
requirePage
(
page
:
string
,
distDir
:
string
,
opts
:
PagePathOptions
=
{}
):
any
{
const
pagePath
=
getPagePath
(
page
,
distDir
,
opts
)
return
require
(
pagePath
)
const
isAmp
=
pagePath
.
indexOf
(
'.amp.'
)
>
-
1
let
hasAmp
=
false
if
(
!
isAmp
)
{
try
{
const
ampPage
=
getPagePath
(
page
,
distDir
,
{
amphtml
:
true
})
hasAmp
=
Boolean
(
ampPage
&&
ampPage
.
indexOf
(
'.amp'
)
>
-
1
)
}
catch
(
_
)
{}
}
opts
.
amphtml
=
opts
.
amphtml
||
isAmp
return
{
hasAmp
,
mod
:
require
(
pagePath
),
}
}
packages/next/pages/_document.js
View file @
bce75ae9
...
...
@@ -150,6 +150,7 @@ export class Head extends Component {
ampEnabled
,
styles
,
amphtml
,
hasAmp
,
assetPrefix
,
__NEXT_DATA__
,
}
=
this
.
context
.
_documentProps
...
...
@@ -201,7 +202,7 @@ export class Head extends Component {
name
=
"viewport"
content
=
"width=device-width,minimum-scale=1,initial-scale=1"
/>
<
link
rel
=
"canonical"
href
=
{
page
}
/
>
<
link
rel
=
"canonical"
href
=
{
page
.
split
(
'.amp'
)[
0
]
}
/
>
{
/* https://www.ampproject.org/docs/fundamentals/optimize_amp#optimize-the-amp-runtime-loading */
}
<
link
rel
=
"preload"
...
...
@@ -240,7 +241,7 @@ export class Head extends Component {
)}
{
!
amphtml
&&
(
<>
{
ampEnabled
&&
<
link
rel
=
"amphtml"
href
=
{
`
${
page
}
?amp=1`
}
/>
}
{
ampEnabled
&&
hasAmp
&&
<
link
rel
=
"amphtml"
href
=
{
`
${
page
}
?amp=1`
}
/>
}
{
page
!==
'/_error'
&&
(
<
link
rel
=
"preload"
...
...
packages/next/server/next-dev-server.js
View file @
bce75ae9
...
...
@@ -126,6 +126,7 @@ export default class DevServer extends Server {
const
result
=
await
this
.
hotReloader
.
ensurePage
(
pathname
,
options
.
amphtml
,
this
.
nextConfig
.
experimental
.
amp
)
pathname
=
result
.
pathname
options
.
amphtml
=
options
.
amphtml
||
result
.
isAmp
options
.
hasAmp
=
result
.
hasAmp
}
catch
(
err
)
{
if
(
err
.
code
===
'ENOENT'
)
{
res
.
statusCode
=
404
...
...
packages/next/server/on-demand-entry-handler.js
View file @
bce75ae9
...
...
@@ -234,7 +234,11 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
const
absolutePagePath
=
pagePath
.
startsWith
(
'next/dist/pages'
)
?
require
.
resolve
(
pagePath
)
:
join
(
pagesDir
,
pagePath
)
page
=
posix
.
normalize
(
pageUrl
)
const
result
=
{
isAmp
,
pathname
:
page
}
const
result
=
{
isAmp
,
pathname
:
page
,
hasAmp
:
!
isAmp
&&
await
findPageFile
(
pagesDir
,
normalizedPagePath
,
pageExtensions
,
!
isAmp
,
ampEnabled
)
}
await
new
Promise
((
resolve
,
reject
)
=>
{
// Makes sure the page that is being kept in on-demand-entries matches the webpack output
...
...
test/integration/amphtml/pages/normal.js
0 → 100644
View file @
bce75ae9
export
default
()
=>
<
div
>
No
AMP
for
me
...
<
/div
>
test/integration/amphtml/pages/only-amp.amp.js
0 → 100644
View file @
bce75ae9
export
default
()
=>
<
div
>
Only
AMP
for
me
...
<
/div
>
test/integration/amphtml/test/index.test.js
View file @
bce75ae9
...
...
@@ -156,13 +156,43 @@ describe('AMP Usage', () => {
).
toBe
(
'/use-amp-hook'
)
})
it
(
'should render a canonical regardless of amp-only status (implicit)'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/only-amp'
)
const
$
=
cheerio
.
load
(
html
)
await
validateAMP
(
html
)
expect
(
$
(
'link[rel=canonical]'
)
.
first
()
.
attr
(
'href'
)
).
toBe
(
'/only-amp'
)
})
it
(
'should render a canonical regardless of amp-only status (explicit)'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/only-amp?amp=1'
)
const
$
=
cheerio
.
load
(
html
)
await
validateAMP
(
html
)
expect
(
$
(
'link[rel=canonical]'
)
.
first
()
.
attr
(
'href'
)
).
toBe
(
'/only-amp'
)
})
it
(
'should not render amphtml link tag with no AMP page'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/normal'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'link[rel=amphtml]'
)
.
first
()
.
attr
(
'href'
)
).
not
.
toBeTruthy
()
})
it
(
'should remove conflicting amp tags'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/conflicting-tag?amp=1'
)
const
$
=
cheerio
.
load
(
html
)
await
validateAMP
(
html
)
expect
(
$
(
'meta[name=viewport]'
).
attr
(
'content'
)
).
not
.
toBe
(
'something :p'
)
expect
(
$
(
'meta[name=viewport]'
).
attr
(
'content'
)).
not
.
toBe
(
'something :p'
)
})
})
...
...
@@ -182,7 +212,9 @@ describe('AMP Usage', () => {
it
(
'should remove sourceMaps from styles'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/styled?amp=1'
)
const
$
=
cheerio
.
load
(
html
)
const
styles
=
$
(
'style[amp-custom]'
).
first
().
text
()
const
styles
=
$
(
'style[amp-custom]'
)
.
first
()
.
text
()
expect
(
styles
).
not
.
toMatch
(
/
\/\*
@ sourceURL=.*
?\*\/
/
)
expect
(
styles
).
not
.
toMatch
(
/
\/\*
# sourceMappingURL=.*
\*\/
/
)
...
...
test/isolated/require-page.test.js
View file @
bce75ae9
...
...
@@ -59,17 +59,17 @@ describe('getPagePath', () => {
describe
(
'requirePage'
,
()
=>
{
it
(
'Should require /index.js when using /'
,
async
()
=>
{
const
page
=
await
requirePage
(
'/'
,
distDir
)
const
page
=
(
await
requirePage
(
'/'
,
distDir
)).
mod
expect
(
page
.
test
).
toBe
(
'hello'
)
})
it
(
'Should require /index.js when using /index'
,
async
()
=>
{
const
page
=
await
requirePage
(
'/index'
,
distDir
)
const
page
=
(
await
requirePage
(
'/index'
,
distDir
)).
mod
expect
(
page
.
test
).
toBe
(
'hello'
)
})
it
(
'Should require /world.js when using /world'
,
async
()
=>
{
const
page
=
await
requirePage
(
'/world'
,
distDir
)
const
page
=
(
await
requirePage
(
'/world'
,
distDir
)).
mod
expect
(
page
.
test
).
toBe
(
'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