Commit 2d6a73cb authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Remove conflicting <head> tags in amp mode (#6693)

parent 05ddf3e4
# Conflicting AMP Tag
#### Why This Error Occurred
In AMP mode Next.js adds certain necessary tags automatically to comply with the AMP standard. You added a tag using `next/head` that conflicts with one of these automatically added tags.
#### Possible Ways to Fix It
Remove the tag mentioned in the error message from any `<Head></Head>` elements
......@@ -148,7 +148,6 @@ export class Head extends Component {
render() {
const {
ampEnabled,
head,
styles,
amphtml,
assetPrefix,
......@@ -157,6 +156,7 @@ export class Head extends Component {
const { _devOnlyInvalidateCacheQueryString } = this.context
const { page, buildId } = __NEXT_DATA__
let { head } = this.context._documentProps
let children = this.props.children
// show a warning if Head contains <title> (only in development)
if (process.env.NODE_ENV !== 'production') {
......@@ -173,6 +173,24 @@ export class Head extends Component {
'Warning: `Head` attribute `crossOrigin` is deprecated. https://err.sh/next.js/doc-crossorigin-deprecated'
)
}
// show warning and remove conflicting amp head tags
head = !amphtml ? head : React.Children.map(head, child => {
if (!child) return child
const { type, props } = child
let badProp
if (type === 'meta' && props.name === 'viewport') {
badProp = 'name="viewport"'
} else if (type === 'link' && props.rel === 'canonical') {
badProp = 'rel="canonical"'
}
if (badProp) {
console.warn(`Found conflicting amp tag "${child.type}" with conflicting prop ${badProp}. https://err.sh/next.js/conflicting-amp-tag`)
return null
}
return child
})
return (
<head {...this.props}>
{children}
......
import Head from 'next/head'
export default () => (
<amp-layout className='abc' layout='responsive' width='1' height='1'>
<Head>
<meta name='viewport' content='something :p' />
</Head>
<span>Hello World</span>
</amp-layout>
)
......@@ -144,6 +144,15 @@ describe('AMP Usage', () => {
.attr('href')
).toBe('/use-amp-hook')
})
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')
})
})
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