fa45fa0a60
* Add export const config support and make withAmp a no-op * Use babel plugin for PageConfig * Fix serverless-loader exports * Add backwards compatibility for withAmp
33 lines
909 B
TypeScript
33 lines
909 B
TypeScript
import React from 'react'
|
|
import { AmpModeContext } from './amphtml-context'
|
|
|
|
export function isAmp({
|
|
enabled = false,
|
|
hybrid = false,
|
|
hasQuery = false,
|
|
} = {}) {
|
|
return enabled && (!hybrid || (hybrid && hasQuery))
|
|
}
|
|
|
|
export function useAmp() {
|
|
const ampMode = React.useContext(AmpModeContext)
|
|
// un-comment below to not be considered AMP in dirty mode
|
|
return isAmp(ampMode) // && ampMode.hasQuery
|
|
}
|
|
|
|
/**
|
|
* @deprecated This is no longer required, use export const config = { amp: true }
|
|
*/
|
|
export function withAmp(Component: any, { hybrid = false } = {}): any {
|
|
function WithAmpWrapper(props = {}) {
|
|
const ampMode = React.useContext(AmpModeContext)
|
|
ampMode.enabled = true
|
|
ampMode.hybrid = hybrid
|
|
|
|
return React.createElement(Component, props)
|
|
}
|
|
|
|
WithAmpWrapper.__nextAmpOnly = !hybrid
|
|
WithAmpWrapper.getInitialProps = Component.getInitialProps
|
|
return WithAmpWrapper
|
|
}
|