Commit 26a4eb82 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add dropping of custom scripts in AMP mode (#6830)

Drops user `<script>` tags and shows a warning in AMP mode. Right now they are only dropped in production mode and left in dev mode so the validator shows its warning since it looks like conflicting props log messages are being cleared causing them to not show. 

Closes: #6688 
parent b6b0db18
...@@ -186,6 +186,12 @@ export class Head extends Component { ...@@ -186,6 +186,12 @@ export class Head extends Component {
badProp = 'name="viewport"' badProp = 'name="viewport"'
} else if (type === 'link' && props.rel === 'canonical') { } else if (type === 'link' && props.rel === 'canonical') {
badProp = 'rel="canonical"' badProp = 'rel="canonical"'
} else if (type === 'script') {
badProp = '<script'
Object.keys(props).forEach(prop => {
badProp += ` ${prop}="${props[prop]}"`
})
badProp += '/>'
} }
if (badProp) { if (badProp) {
......
import Head from 'next/head'
export default () => (
<div>
<Head>
<script src='/im-not-allowed.js' type='text/javascript' />
<script dangerouslySetInnerHTML={{
__html: `console.log("I'm not either :p")`
}} />
</Head>
<p>We only allow AMP scripts now</p>
</div>
)
...@@ -107,6 +107,12 @@ describe('AMP Usage', () => { ...@@ -107,6 +107,12 @@ describe('AMP Usage', () => {
'amp-boilerplate' 'amp-boilerplate'
]) ])
}) })
it('should drop custom scripts', async () => {
const html = await renderViaHTTP(appPort, '/custom-scripts')
expect(html).not.toMatch(/src='\/im-not-allowed\.js'/)
expect(html).not.toMatch(/console\.log("I'm not either :p")'/)
})
}) })
describe('With AMP context', () => { describe('With AMP context', () => {
......
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