Commit 4bcf6aab authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Allow manually setting amphtml and canonical links (#8129)

parent 78775954
...@@ -257,6 +257,9 @@ export class Head extends Component< ...@@ -257,6 +257,9 @@ export class Head extends Component<
) )
} }
let hasAmphtmlRel = false
let hasCanonicalRel = false
// show warning and remove conflicting amp head tags // show warning and remove conflicting amp head tags
head = !inAmpMode head = !inAmpMode
? head ? head
...@@ -268,7 +271,9 @@ export class Head extends Component< ...@@ -268,7 +271,9 @@ export class Head extends Component<
if (type === 'meta' && props.name === 'viewport') { if (type === 'meta' && props.name === 'viewport') {
badProp = 'name="viewport"' badProp = 'name="viewport"'
} else if (type === 'link' && props.rel === 'canonical') { } 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') { } else if (type === 'script') {
// only block if // only block if
// 1. it has a src and isn't pointing to ampproject's CDN // 1. it has a src and isn't pointing to ampproject's CDN
...@@ -341,10 +346,12 @@ export class Head extends Component< ...@@ -341,10 +346,12 @@ export class Head extends Component<
name="viewport" name="viewport"
content="width=device-width,minimum-scale=1,initial-scale=1" content="width=device-width,minimum-scale=1,initial-scale=1"
/> />
<link {!hasCanonicalRel && (
rel="canonical" <link
href={canonicalBase + cleanAmpPath(dangerousAsPath)} rel="canonical"
/> href={canonicalBase + cleanAmpPath(dangerousAsPath)}
/>
)}
{/* https://www.ampproject.org/docs/fundamentals/optimize_amp#optimize-the-amp-runtime-loading */} {/* https://www.ampproject.org/docs/fundamentals/optimize_amp#optimize-the-amp-runtime-loading */}
<link <link
rel="preload" rel="preload"
...@@ -383,7 +390,7 @@ export class Head extends Component< ...@@ -383,7 +390,7 @@ export class Head extends Component<
)} )}
{!inAmpMode && ( {!inAmpMode && (
<> <>
{hybridAmp && ( {!hasAmphtmlRel && hybridAmp && (
<link <link
rel="amphtml" rel="amphtml"
href={canonicalBase + getAmpPath(ampPath, dangerousAsPath)} href={canonicalBase + getAmpPath(ampPath, dangerousAsPath)}
......
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>
</>
)
...@@ -188,6 +188,19 @@ describe('AMP Usage', () => { ...@@ -188,6 +188,19 @@ describe('AMP Usage', () => {
await validateAMP(html) await validateAMP(html)
expect($('meta[name=viewport]').attr('content')).not.toBe('something :p') 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', () => { describe('combined styles', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment