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<
)
}
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)}
......
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', () => {
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', () => {
......
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