Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/next-server/lib/utils.ts
Expand Up @@ -187,6 +187,7 @@ export type DocumentProps = DocumentInitialProps & {
canonicalBase: string
headTags: any[]
unstable_runtimeJS?: false
unstable_JsPreload?: false
devOnlyCacheBusterQueryString: string
scriptLoader: { defer?: string[]; eager?: any[] }
locale?: string
Expand Down
4 changes: 4 additions & 0 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -170,6 +170,7 @@ export type RenderOptsPartial = {
previewProps: __ApiPreviewProps
basePath: string
unstable_runtimeJS?: false
unstable_JsPreload?: false
optimizeFonts: boolean
fontManifest?: FontManifest
optimizeImages: boolean
Expand Down Expand Up @@ -218,6 +219,7 @@ function renderDocument(
gip,
appGip,
unstable_runtimeJS,
unstable_JsPreload,
devOnlyCacheBusterQueryString,
scriptLoader,
locale,
Expand Down Expand Up @@ -288,6 +290,7 @@ function renderDocument(
assetPrefix,
headTags,
unstable_runtimeJS,
unstable_JsPreload,
devOnlyCacheBusterQueryString,
scriptLoader,
locale,
Expand Down Expand Up @@ -1019,6 +1022,7 @@ export async function renderToHTML(
process.env.NODE_ENV === 'production'
? pageConfig.unstable_runtimeJS
: undefined,
unstable_JsPreload: pageConfig.unstable_JsPreload,
dangerousAsPath: router.asPath,
ampState,
props,
Expand Down
10 changes: 8 additions & 2 deletions packages/next/pages/_document.tsx
Expand Up @@ -342,8 +342,10 @@ export class Head extends Component<
dangerousAsPath,
headTags,
unstable_runtimeJS,
unstable_JsPreload,
} = this.context
const disableRuntimeJS = unstable_runtimeJS === false
const disableJsPreload = unstable_JsPreload === false

this.context.docComponentsRendered.Head = true

Expand Down Expand Up @@ -566,8 +568,12 @@ export class Head extends Component<
{!process.env.__NEXT_OPTIMIZE_CSS && (
<noscript data-n-css={this.props.nonce ?? ''} />
)}
{!disableRuntimeJS && this.getPreloadDynamicChunks()}
{!disableRuntimeJS && this.getPreloadMainLinks(files)}
{!disableRuntimeJS &&
!disableJsPreload &&
this.getPreloadDynamicChunks()}
{!disableRuntimeJS &&
!disableJsPreload &&
this.getPreloadMainLinks(files)}
{process.env.__NEXT_OPTIMIZE_CSS && this.getCssLinks(files)}
{process.env.__NEXT_OPTIMIZE_CSS && (
<noscript data-n-css={this.props.nonce ?? ''} />
Expand Down
1 change: 1 addition & 0 deletions packages/next/types/index.d.ts
Expand Up @@ -74,6 +74,7 @@ export type PageConfig = {
}
env?: Array<string>
unstable_runtimeJS?: false
unstable_JsPreload?: false
}

export {
Expand Down
6 changes: 6 additions & 0 deletions test/integration/disable-js-preload/next.config.js
@@ -0,0 +1,6 @@
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
5 changes: 5 additions & 0 deletions test/integration/disable-js-preload/pages/index.js
@@ -0,0 +1,5 @@
export const config = {
unstable_JsPreload: false,
}

export default () => <h1>Hello World!</h1>
73 changes: 73 additions & 0 deletions test/integration/disable-js-preload/test/index.test.js
@@ -0,0 +1,73 @@
/* 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)
})
})
})