Unverified Commit 11016221 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure AmpContext is available in _document (#7021)

* Make sure AmpContext is available during _document
render and update filtering of script tags in AMP mode

* Update amphtml test to make sure
AmpContext is set for _document render

* Fix stray comma in render.tsx
parent 5ed51c00
...@@ -126,6 +126,7 @@ type RenderOpts = { ...@@ -126,6 +126,7 @@ type RenderOpts = {
ampPath?: string ampPath?: string
amphtml?: boolean amphtml?: boolean
hasAmp?: boolean, hasAmp?: boolean,
ampMode?: any,
dataOnly?: boolean, dataOnly?: boolean,
buildManifest: BuildManifest buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest reactLoadableManifest: ReactLoadableManifest
...@@ -155,6 +156,7 @@ function renderDocument( ...@@ -155,6 +156,7 @@ function renderDocument(
ampPath, ampPath,
amphtml, amphtml,
hasAmp, hasAmp,
ampMode,
staticMarkup, staticMarkup,
devFiles, devFiles,
files, files,
...@@ -168,6 +170,7 @@ function renderDocument( ...@@ -168,6 +170,7 @@ function renderDocument(
ampPath: string, ampPath: string,
amphtml: boolean amphtml: boolean
hasAmp: boolean, hasAmp: boolean,
ampMode: any,
dynamicImportsIds: string[] dynamicImportsIds: string[]
dynamicImports: ManifestItem[] dynamicImports: ManifestItem[]
files: string[] files: string[]
...@@ -177,6 +180,7 @@ function renderDocument( ...@@ -177,6 +180,7 @@ function renderDocument(
return ( return (
'<!DOCTYPE html>' + '<!DOCTYPE html>' +
renderToStaticMarkup( renderToStaticMarkup(
<AmpModeContext.Provider value={ampMode}>
<Document <Document
__NEXT_DATA__={{ __NEXT_DATA__={{
dataManager: dataManagerData, dataManager: dataManagerData,
...@@ -201,7 +205,8 @@ function renderDocument( ...@@ -201,7 +205,8 @@ function renderDocument(
dynamicImports={dynamicImports} dynamicImports={dynamicImports}
assetPrefix={assetPrefix} assetPrefix={assetPrefix}
{...docProps} {...docProps}
/>, />
</AmpModeContext.Provider>,
) )
) )
} }
...@@ -420,6 +425,7 @@ export async function renderToHTML( ...@@ -420,6 +425,7 @@ export async function renderToHTML(
let html = renderDocument(Document, { let html = renderDocument(Document, {
...renderOpts, ...renderOpts,
dataManagerData, dataManagerData,
ampMode,
props, props,
docProps, docProps,
pathname, pathname,
......
...@@ -186,13 +186,21 @@ export class Head extends Component { ...@@ -186,13 +186,21 @@ 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' && !(props.src && props.src.indexOf('ampproject') > -1)) { } else if (type === 'script') {
// only block if
// 1. it has a src and isn't pointing to ampproject's CDN
// 2. it is using dangerouslySetInnerHTML without a type or
// a type of text/javascript
if ((props.src && props.src.indexOf('ampproject') < -1) ||
(props.dangerouslySetInnerHTML && (!props.type || props.type === 'text/javascript'))
) {
badProp = '<script' badProp = '<script'
Object.keys(props).forEach(prop => { Object.keys(props).forEach(prop => {
badProp += ` ${prop}="${props[prop]}"` badProp += ` ${prop}="${props[prop]}"`
}) })
badProp += '/>' badProp += '/>'
} }
}
if (badProp) { if (badProp) {
console.warn(`Found conflicting amp tag "${child.type}" with conflicting prop ${badProp}. https://err.sh/next.js/conflicting-amp-tag`) console.warn(`Found conflicting amp tag "${child.type}" with conflicting prop ${badProp}. https://err.sh/next.js/conflicting-amp-tag`)
......
import { useAmp } from 'next/amp'
import Document, { Html, Head, Main, NextScript } from 'next/document'
const AmpTst = () => {
const isAmp = useAmp()
return <p>{isAmp ? 'AMP Power!!!' : 'no AMP for you...'}</p>
}
class MyDocument extends Document {
static async getInitialProps (ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render () {
return (
<Html>
<Head />
<body>
<Main />
<AmpTst />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
...@@ -119,12 +119,14 @@ describe('AMP Usage', () => { ...@@ -119,12 +119,14 @@ describe('AMP Usage', () => {
it('should render the normal page that uses the AMP hook', async () => { it('should render the normal page that uses the AMP hook', async () => {
const html = await renderViaHTTP(appPort, '/use-amp-hook') const html = await renderViaHTTP(appPort, '/use-amp-hook')
expect(html).toMatch(/Hello others/) expect(html).toMatch(/Hello others/)
expect(html).toMatch(/no AMP for you\.\.\./)
}) })
it('should render the AMP page that uses the AMP hook', async () => { it('should render the AMP page that uses the AMP hook', async () => {
const html = await renderViaHTTP(appPort, '/use-amp-hook?amp=1') const html = await renderViaHTTP(appPort, '/use-amp-hook?amp=1')
await validateAMP(html) await validateAMP(html)
expect(html).toMatch(/Hello AMP/) expect(html).toMatch(/Hello AMP/)
expect(html).toMatch(/AMP Power!!!/)
}) })
it('should render nested normal page with AMP hook', async () => { it('should render nested normal page with AMP hook', async () => {
......
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