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
4bcf6aab
Commit
4bcf6aab
authored
Jul 29, 2019
by
JJ Kasper
Committed by
Tim Neutkens
Jul 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow manually setting amphtml and canonical links (#8129)
parent
78775954
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
6 deletions
+41
-6
_document.tsx
packages/next/pages/_document.tsx
+13
-6
manual-rels.js
test/integration/amphtml/pages/manual-rels.js
+15
-0
index.test.js
test/integration/amphtml/test/index.test.js
+13
-0
No files found.
packages/next/pages/_document.tsx
View file @
4bcf6aab
...
...
@@ -257,6 +257,9 @@ export class Head extends Component<
)
}
let
hasAmphtmlRel
=
false
let
hasCanonicalRel
=
false
// show warning and remove conflicting amp head tags
head
=
!
inAmpMode
?
head
...
...
@@ -268,7 +271,9 @@ export class Head extends Component<
if
(
type
===
'meta'
&&
props
.
name
===
'viewport'
)
{
badProp
=
'name="viewport"'
}
else
if
(
type
===
'link'
&&
props
.
rel
===
'canonical'
)
{
badProp
=
'rel="canonical"'
hasCanonicalRel
=
true
}
else
if
(
type
===
'link'
&&
props
.
rel
===
'amphtml'
)
{
hasAmphtmlRel
=
true
}
else
if
(
type
===
'script'
)
{
// only block if
// 1. it has a src and isn't pointing to ampproject's CDN
...
...
@@ -341,10 +346,12 @@ export class Head extends Component<
name=
"viewport"
content=
"width=device-width,minimum-scale=1,initial-scale=1"
/>
<
link
rel=
"canonical"
href=
{
canonicalBase
+
cleanAmpPath
(
dangerousAsPath
)
}
/>
{
!
hasCanonicalRel
&&
(
<
link
rel=
"canonical"
href=
{
canonicalBase
+
cleanAmpPath
(
dangerousAsPath
)
}
/>
)
}
{
/* https://www.ampproject.org/docs/fundamentals/optimize_amp#optimize-the-amp-runtime-loading */
}
<
link
rel=
"preload"
...
...
@@ -383,7 +390,7 @@ export class Head extends Component<
)
}
{
!
inAmpMode
&&
(
<>
{
hybridAmp
&&
(
{
!
hasAmphtmlRel
&&
hybridAmp
&&
(
<
link
rel=
"amphtml"
href=
{
canonicalBase
+
getAmpPath
(
ampPath
,
dangerousAsPath
)
}
...
...
test/integration/amphtml/pages/manual-rels.js
0 → 100644
View file @
4bcf6aab
import
Head
from
'next/head'
import
{
useAmp
}
from
'next/amp'
export
const
config
=
{
amp
:
'hybrid'
}
export
default
()
=>
(
<>
<
Head
>
{
!
useAmp
()
&&
<
link
rel
=
'amphtml'
href
=
'/my-custom-amphtml'
/>
}
<
link
rel
=
'canonical'
href
=
'/my-custom-canonical'
/>
<
/Head
>
<
p
>
Hello
world
<
/p
>
<
/
>
)
test/integration/amphtml/test/index.test.js
View file @
4bcf6aab
...
...
@@ -188,6 +188,19 @@ describe('AMP Usage', () => {
await
validateAMP
(
html
)
expect
(
$
(
'meta[name=viewport]'
).
attr
(
'content'
)).
not
.
toBe
(
'something :p'
)
})
it
(
'should allow manually setting canonical'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/manual-rels?amp=1'
)
const
$
=
cheerio
.
load
(
html
)
await
validateAMP
(
html
)
expect
(
$
(
'link[rel=canonical]'
).
attr
(
'href'
)).
toBe
(
'/my-custom-canonical'
)
})
it
(
'should allow manually setting amphtml rel'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/manual-rels'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'link[rel=amphtml]'
).
attr
(
'href'
)).
toBe
(
'/my-custom-amphtml'
)
})
})
describe
(
'combined styles'
,
()
=>
{
...
...
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