Unverified Commit 2fa26a2f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Allow disabling runtime JS in production for certain pages (#11949)

This allows a page to be fully static (no runtime JavaScript) on a per-page basis.

The initial implementation does not disable JS in development mode as we need to figure out a way to inject CSS from CSS imports / CSS modules without executing the component JS. This restriction is somewhat similar to https://www.gatsbyjs.org/packages/gatsby-plugin-no-javascript/. All things considered that plugin only has a usage of 600 downloads per week though, hence why I've made this option unstable/experimental initially as I'd like to see adoption patterns for it first.

Having a built-in way to do this makes sense however as the people that do want to adopt this pattern are overriding Next.js internals currently and that'll break between versions.

Related issue: #5054 - Not adding `fixes` right now as this implementation needs more work. If anyone wants to work on this feel free to reach out on https://twitter.com/timneutkens
parent 22e3c699
...@@ -169,6 +169,7 @@ export type DocumentProps = DocumentInitialProps & { ...@@ -169,6 +169,7 @@ export type DocumentProps = DocumentInitialProps & {
htmlProps: any htmlProps: any
bodyTags: any[] bodyTags: any[]
headTags: any[] headTags: any[]
unstable_runtimeJS?: false
} }
/** /**
......
...@@ -159,6 +159,7 @@ export type RenderOptsPartial = { ...@@ -159,6 +159,7 @@ export type RenderOptsPartial = {
params?: ParsedUrlQuery params?: ParsedUrlQuery
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
basePath: string basePath: string
unstable_runtimeJS?: false
} }
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
...@@ -200,6 +201,7 @@ function renderDocument( ...@@ -200,6 +201,7 @@ function renderDocument(
customServer, customServer,
gip, gip,
appGip, appGip,
unstable_runtimeJS,
}: RenderOpts & { }: RenderOpts & {
props: any props: any
docProps: DocumentInitialProps docProps: DocumentInitialProps
...@@ -268,6 +270,7 @@ function renderDocument( ...@@ -268,6 +270,7 @@ function renderDocument(
htmlProps, htmlProps,
bodyTags, bodyTags,
headTags, headTags,
unstable_runtimeJS,
...docProps, ...docProps,
})} })}
</AmpStateContext.Provider> </AmpStateContext.Provider>
...@@ -772,6 +775,11 @@ export async function renderToHTML( ...@@ -772,6 +775,11 @@ export async function renderToHTML(
let html = renderDocument(Document, { let html = renderDocument(Document, {
...renderOpts, ...renderOpts,
// Only enabled in production as development mode has features relying on HMR (style injection for example)
unstable_runtimeJS:
process.env.NODE_ENV === 'production'
? pageConfig.unstable_runtimeJS
: undefined,
dangerousAsPath: router.asPath, dangerousAsPath: router.asPath,
ampState, ampState,
props, props,
......
...@@ -304,7 +304,9 @@ export class Head extends Component< ...@@ -304,7 +304,9 @@ export class Head extends Component<
__NEXT_DATA__, __NEXT_DATA__,
dangerousAsPath, dangerousAsPath,
headTags, headTags,
unstable_runtimeJS,
} = this.context._documentProps } = this.context._documentProps
const disableRuntimeJS = unstable_runtimeJS === false
const { _devOnlyInvalidateCacheQueryString } = this.context const { _devOnlyInvalidateCacheQueryString } = this.context
const { page, buildId } = __NEXT_DATA__ const { page, buildId } = __NEXT_DATA__
...@@ -490,7 +492,7 @@ export class Head extends Component< ...@@ -490,7 +492,7 @@ export class Head extends Component<
/> />
)} )}
{this.getCssLinks()} {this.getCssLinks()}
{page !== '/_error' && ( {!disableRuntimeJS && page !== '/_error' && (
<link <link
rel="preload" rel="preload"
href={ href={
...@@ -507,6 +509,7 @@ export class Head extends Component< ...@@ -507,6 +509,7 @@ export class Head extends Component<
crossOrigin={this.props.crossOrigin || process.crossOrigin} crossOrigin={this.props.crossOrigin || process.crossOrigin}
/> />
)} )}
{!disableRuntimeJS && (
<link <link
rel="preload" rel="preload"
href={ href={
...@@ -520,8 +523,9 @@ export class Head extends Component< ...@@ -520,8 +523,9 @@ export class Head extends Component<
nonce={this.props.nonce} nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || process.crossOrigin} crossOrigin={this.props.crossOrigin || process.crossOrigin}
/> />
{this.getPreloadDynamicChunks()} )}
{this.getPreloadMainLinks()} {!disableRuntimeJS && this.getPreloadDynamicChunks()}
{!disableRuntimeJS && this.getPreloadMainLinks()}
{this.context._documentProps.isDevelopment && {this.context._documentProps.isDevelopment &&
this.context._documentProps.hasCssMode && ( this.context._documentProps.hasCssMode && (
// this element is used to mount development styles so the // this element is used to mount development styles so the
...@@ -532,7 +536,7 @@ export class Head extends Component< ...@@ -532,7 +536,7 @@ export class Head extends Component<
{styles || null} {styles || null}
</> </>
)} )}
{this.getFidPolyfill()} {!disableRuntimeJS && this.getFidPolyfill()}
{React.createElement(React.Fragment, {}, ...(headTags || []))} {React.createElement(React.Fragment, {}, ...(headTags || []))}
</head> </head>
) )
...@@ -669,7 +673,9 @@ export class NextScript extends Component<OriginProps> { ...@@ -669,7 +673,9 @@ export class NextScript extends Component<OriginProps> {
devFiles, devFiles,
__NEXT_DATA__, __NEXT_DATA__,
bodyTags, bodyTags,
unstable_runtimeJS,
} = this.context._documentProps } = this.context._documentProps
const disableRuntimeJS = unstable_runtimeJS === false
const { _devOnlyInvalidateCacheQueryString } = this.context const { _devOnlyInvalidateCacheQueryString } = this.context
...@@ -790,7 +796,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -790,7 +796,7 @@ export class NextScript extends Component<OriginProps> {
return ( return (
<> <>
{devFiles {!disableRuntimeJS && devFiles
? devFiles.map( ? devFiles.map(
(file: string) => (file: string) =>
!file.match(/\.js\.map/) && ( !file.match(/\.js\.map/) && (
...@@ -818,7 +824,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -818,7 +824,7 @@ export class NextScript extends Component<OriginProps> {
}} }}
/> />
)} )}
{process.env.__NEXT_MODERN_BUILD ? ( {process.env.__NEXT_MODERN_BUILD && !disableRuntimeJS ? (
<script <script
nonce={this.props.nonce} nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || process.crossOrigin} crossOrigin={this.props.crossOrigin || process.crossOrigin}
...@@ -828,11 +834,11 @@ export class NextScript extends Component<OriginProps> { ...@@ -828,11 +834,11 @@ export class NextScript extends Component<OriginProps> {
}} }}
/> />
) : null} ) : null}
{this.getPolyfillScripts()} {!disableRuntimeJS && this.getPolyfillScripts()}
{page !== '/_error' && pageScript} {!disableRuntimeJS && page !== '/_error' && pageScript}
{appScript} {!disableRuntimeJS && appScript}
{staticMarkup ? null : this.getDynamicChunks()} {disableRuntimeJS || staticMarkup ? null : this.getDynamicChunks()}
{staticMarkup ? null : this.getScripts()} {disableRuntimeJS || staticMarkup ? null : this.getScripts()}
{React.createElement(React.Fragment, {}, ...(bodyTags || []))} {React.createElement(React.Fragment, {}, ...(bodyTags || []))}
</> </>
) )
......
...@@ -55,6 +55,7 @@ export type PageConfig = { ...@@ -55,6 +55,7 @@ export type PageConfig = {
bodyParser?: { sizeLimit?: number | string } | false bodyParser?: { sizeLimit?: number | string } | false
} }
env?: Array<string> env?: Array<string>
unstable_runtimeJS?: false
} }
export { export {
......
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
export const config = {
unstable_runtimeJS: false,
}
export default () => <h1>Hello World!</h1>
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