Commit b7efb3ff authored by Liran Cohen's avatar Liran Cohen Committed by JJ Kasper

Allow manually setting amphtml (#9098)

* allow to remove amphtml from desktop page

* only 1 link with amphtml rel

* clean up

* remove checkup for amphtml link

* Dedupe looping
parent 499808fa
...@@ -273,49 +273,53 @@ export class Head extends Component< ...@@ -273,49 +273,53 @@ export class Head extends Component<
let hasCanonicalRel = false let hasCanonicalRel = false
// show warning and remove conflicting amp head tags // show warning and remove conflicting amp head tags
head = !inAmpMode head = React.Children.map(head || [], child => {
? head if (!child) return child
: React.Children.map(head || [], child => { const { type, props } = child
if (!child) return child
const { type, props } = child if (inAmpMode) {
let badProp: string = '' let badProp: string = ''
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') {
hasCanonicalRel = true hasCanonicalRel = true
} else if (type === 'link' && props.rel === 'amphtml') { } else if (type === 'script') {
hasAmphtmlRel = true // only block if
} else if (type === 'script') { // 1. it has a src and isn't pointing to ampproject's CDN
// only block if // 2. it is using dangerouslySetInnerHTML without a type or
// 1. it has a src and isn't pointing to ampproject's CDN // a type of text/javascript
// 2. it is using dangerouslySetInnerHTML without a type or if (
// a type of text/javascript (props.src && props.src.indexOf('ampproject') < -1) ||
if ( (props.dangerouslySetInnerHTML &&
(props.src && props.src.indexOf('ampproject') < -1) || (!props.type || props.type === 'text/javascript'))
(props.dangerouslySetInnerHTML && ) {
(!props.type || props.type === 'text/javascript')) badProp = '<script'
) { Object.keys(props).forEach(prop => {
badProp = '<script' badProp += ` ${prop}="${props[prop]}"`
Object.keys(props).forEach(prop => { })
badProp += ` ${prop}="${props[prop]}"` badProp += '/>'
})
badProp += '/>'
}
} }
}
if (badProp) { if (badProp) {
console.warn( console.warn(
`Found conflicting amp tag "${ `Found conflicting amp tag "${
child.type child.type
}" with conflicting prop ${badProp} in ${ }" with conflicting prop ${badProp} in ${
__NEXT_DATA__.page __NEXT_DATA__.page
}. https://err.sh/next.js/conflicting-amp-tag` }. https://err.sh/next.js/conflicting-amp-tag`
) )
return null return null
} }
return child } else {
}) // non-amp mode
if (type === 'link' && props.rel === 'amphtml') {
hasAmphtmlRel = true
}
}
return child
})
// try to parse styles from fragment for backwards compat // try to parse styles from fragment for backwards compat
const curStyles: React.ReactElement[] = Array.isArray(styles) const curStyles: React.ReactElement[] = Array.isArray(styles)
......
...@@ -200,6 +200,7 @@ describe('AMP Usage', () => { ...@@ -200,6 +200,7 @@ describe('AMP Usage', () => {
const html = await renderViaHTTP(appPort, '/manual-rels') const html = await renderViaHTTP(appPort, '/manual-rels')
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('link[rel=amphtml]').attr('href')).toBe('/my-custom-amphtml') expect($('link[rel=amphtml]').attr('href')).toBe('/my-custom-amphtml')
expect($('link[rel=amphtml]')).toHaveLength(1)
}) })
}) })
......
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