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,31 +180,33 @@ function renderDocument( ...@@ -177,31 +180,33 @@ function renderDocument(
return ( return (
'<!DOCTYPE html>' + '<!DOCTYPE html>' +
renderToStaticMarkup( renderToStaticMarkup(
<Document <AmpModeContext.Provider value={ampMode}>
__NEXT_DATA__={{ <Document
dataManager: dataManagerData, __NEXT_DATA__={{
props, // The result of getInitialProps dataManager: dataManagerData,
page: pathname, // The rendered page props, // The result of getInitialProps
query, // querystring parsed / passed by the user page: pathname, // The rendered page
buildId, // buildId is used to facilitate caching of page bundles, we send it to the client so that pageloader knows where to load bundles query, // querystring parsed / passed by the user
dynamicBuildId, // Specifies if the buildId should by dynamically fetched buildId, // buildId is used to facilitate caching of page bundles, we send it to the client so that pageloader knows where to load bundles
assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML dynamicBuildId, // Specifies if the buildId should by dynamically fetched
runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML
nextExport, // If this is a page exported by `next export` runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML
dynamicIds: nextExport, // If this is a page exported by `next export`
dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds, dynamicIds:
err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
}} err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
ampPath={ampPath} }}
amphtml={amphtml} ampPath={ampPath}
hasAmp={hasAmp} amphtml={amphtml}
staticMarkup={staticMarkup} hasAmp={hasAmp}
devFiles={devFiles} staticMarkup={staticMarkup}
files={files} devFiles={devFiles}
dynamicImports={dynamicImports} files={files}
assetPrefix={assetPrefix} dynamicImports={dynamicImports}
{...docProps} assetPrefix={assetPrefix}
/>, {...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,12 +186,20 @@ export class Head extends Component { ...@@ -186,12 +186,20 @@ 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') {
badProp = '<script' // only block if
Object.keys(props).forEach(prop => { // 1. it has a src and isn't pointing to ampproject's CDN
badProp += ` ${prop}="${props[prop]}"` // 2. it is using dangerouslySetInnerHTML without a type or
}) // a type of text/javascript
badProp += '/>' if ((props.src && props.src.indexOf('ampproject') < -1) ||
(props.dangerouslySetInnerHTML && (!props.type || props.type === 'text/javascript'))
) {
badProp = '<script'
Object.keys(props).forEach(prop => {
badProp += ` ${prop}="${props[prop]}"`
})
badProp += '/>'
}
} }
if (badProp) { if (badProp) {
......
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