Unverified Commit 56b149f7 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add experimental per-page option to disable JS preloads (#21329)

As discussed with @csswizardry. This is a temporary option in case you know the preloads are not needed. It will likely be a default once the ScriptLoader work from @janicklas-ralph has been proven in partner apps and landed.

```js
// pages/index.js
export const config = {
  unstable_JsPreload: false
}
```
parent 11900431
...@@ -187,6 +187,7 @@ export type DocumentProps = DocumentInitialProps & { ...@@ -187,6 +187,7 @@ export type DocumentProps = DocumentInitialProps & {
canonicalBase: string canonicalBase: string
headTags: any[] headTags: any[]
unstable_runtimeJS?: false unstable_runtimeJS?: false
unstable_JsPreload?: false
devOnlyCacheBusterQueryString: string devOnlyCacheBusterQueryString: string
scriptLoader: { defer?: string[]; eager?: any[] } scriptLoader: { defer?: string[]; eager?: any[] }
locale?: string locale?: string
......
...@@ -170,6 +170,7 @@ export type RenderOptsPartial = { ...@@ -170,6 +170,7 @@ export type RenderOptsPartial = {
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
basePath: string basePath: string
unstable_runtimeJS?: false unstable_runtimeJS?: false
unstable_JsPreload?: false
optimizeFonts: boolean optimizeFonts: boolean
fontManifest?: FontManifest fontManifest?: FontManifest
optimizeImages: boolean optimizeImages: boolean
...@@ -218,6 +219,7 @@ function renderDocument( ...@@ -218,6 +219,7 @@ function renderDocument(
gip, gip,
appGip, appGip,
unstable_runtimeJS, unstable_runtimeJS,
unstable_JsPreload,
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
scriptLoader, scriptLoader,
locale, locale,
...@@ -288,6 +290,7 @@ function renderDocument( ...@@ -288,6 +290,7 @@ function renderDocument(
assetPrefix, assetPrefix,
headTags, headTags,
unstable_runtimeJS, unstable_runtimeJS,
unstable_JsPreload,
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
scriptLoader, scriptLoader,
locale, locale,
...@@ -1019,6 +1022,7 @@ export async function renderToHTML( ...@@ -1019,6 +1022,7 @@ export async function renderToHTML(
process.env.NODE_ENV === 'production' process.env.NODE_ENV === 'production'
? pageConfig.unstable_runtimeJS ? pageConfig.unstable_runtimeJS
: undefined, : undefined,
unstable_JsPreload: pageConfig.unstable_JsPreload,
dangerousAsPath: router.asPath, dangerousAsPath: router.asPath,
ampState, ampState,
props, props,
......
...@@ -342,8 +342,10 @@ export class Head extends Component< ...@@ -342,8 +342,10 @@ export class Head extends Component<
dangerousAsPath, dangerousAsPath,
headTags, headTags,
unstable_runtimeJS, unstable_runtimeJS,
unstable_JsPreload,
} = this.context } = this.context
const disableRuntimeJS = unstable_runtimeJS === false const disableRuntimeJS = unstable_runtimeJS === false
const disableJsPreload = unstable_JsPreload === false
this.context.docComponentsRendered.Head = true this.context.docComponentsRendered.Head = true
...@@ -566,8 +568,12 @@ export class Head extends Component< ...@@ -566,8 +568,12 @@ export class Head extends Component<
{!process.env.__NEXT_OPTIMIZE_CSS && ( {!process.env.__NEXT_OPTIMIZE_CSS && (
<noscript data-n-css={this.props.nonce ?? ''} /> <noscript data-n-css={this.props.nonce ?? ''} />
)} )}
{!disableRuntimeJS && this.getPreloadDynamicChunks()} {!disableRuntimeJS &&
{!disableRuntimeJS && this.getPreloadMainLinks(files)} !disableJsPreload &&
this.getPreloadDynamicChunks()}
{!disableRuntimeJS &&
!disableJsPreload &&
this.getPreloadMainLinks(files)}
{process.env.__NEXT_OPTIMIZE_CSS && this.getCssLinks(files)} {process.env.__NEXT_OPTIMIZE_CSS && this.getCssLinks(files)}
{process.env.__NEXT_OPTIMIZE_CSS && ( {process.env.__NEXT_OPTIMIZE_CSS && (
<noscript data-n-css={this.props.nonce ?? ''} /> <noscript data-n-css={this.props.nonce ?? ''} />
......
...@@ -74,6 +74,7 @@ export type PageConfig = { ...@@ -74,6 +74,7 @@ export type PageConfig = {
} }
env?: Array<string> env?: Array<string>
unstable_runtimeJS?: false unstable_runtimeJS?: false
unstable_JsPreload?: false
} }
export { export {
......
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
export const config = {
unstable_JsPreload: false,
}
export default () => <h1>Hello World!</h1>
/* eslint-env jest */
import { join } from 'path'
import cheerio from 'cheerio'
import {
nextServer,
nextBuild,
startApp,
stopApp,
renderViaHTTP,
findPort,
launchApp,
killApp,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let server
let app
jest.setTimeout(1000 * 60 * 5)
const context = {}
describe('disabled JS preloads', () => {
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
server = await startApp(app)
context.appPort = appPort = server.address().port
})
afterAll(() => stopApp(server))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should not have JS preload links', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('link[rel=preload]').length).toBe(0)
})
})
describe('dev mode', () => {
let appPort
let app
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(join(__dirname, '../'), appPort)
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should not have JS preload links', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('link[rel=preload]').length).toBe(0)
})
})
})
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