Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dropping of custom scripts in AMP mode #6830

Merged
merged 3 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/next/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export class Head extends Component {
badProp = 'name="viewport"'
} else if (type === 'link' && props.rel === 'canonical') {
badProp = 'rel="canonical"'
} else if (type === 'script') {
badProp = '<script'
Object.keys(props).forEach(prop => {
badProp += ` ${prop}="${props[prop]}"`
})
badProp += '/>'
}

if (badProp) {
Expand Down
13 changes: 13 additions & 0 deletions test/integration/amphtml/pages/custom-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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>
)
6 changes: 6 additions & 0 deletions test/integration/amphtml/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ describe('AMP Usage', () => {
'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', () => {
Expand Down