Unverified Commit 70d7d683 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

perf: lazy evaluate more modules (#41354)

In this PR, I'm inlining the require for some of the modules that I
noticed take a bit of time to evaluate on a cold boot and are not needed
most of the time.

With this, it's an extra ~15ms win on cold boots (tested locally) and we
are now at around 68ms (started from 110+).

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent a1d830ee
...@@ -11,8 +11,6 @@ import type { CookieSerializeOptions } from 'next/dist/compiled/cookie' ...@@ -11,8 +11,6 @@ import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import bytes from 'next/dist/compiled/bytes' import bytes from 'next/dist/compiled/bytes'
import jsonwebtoken from 'next/dist/compiled/jsonwebtoken'
import { decryptWithSecret, encryptWithSecret } from '../crypto-utils'
import { generateETag } from '../lib/etag' import { generateETag } from '../lib/etag'
import { sendEtagResponse } from '../send-payload' import { sendEtagResponse } from '../send-payload'
import { Stream } from 'stream' import { Stream } from 'stream'
...@@ -88,6 +86,8 @@ export function tryGetPreviewData( ...@@ -88,6 +86,8 @@ export function tryGetPreviewData(
data: string data: string
} }
try { try {
const jsonwebtoken =
require('next/dist/compiled/jsonwebtoken') as typeof import('next/dist/compiled/jsonwebtoken')
encryptedPreviewData = jsonwebtoken.verify( encryptedPreviewData = jsonwebtoken.verify(
tokenPreviewData, tokenPreviewData,
options.previewModeSigningKey options.previewModeSigningKey
...@@ -98,6 +98,8 @@ export function tryGetPreviewData( ...@@ -98,6 +98,8 @@ export function tryGetPreviewData(
return false return false
} }
const { decryptWithSecret } =
require('../crypto-utils') as typeof import('../crypto-utils')
const decryptedPreviewData = decryptWithSecret( const decryptedPreviewData = decryptWithSecret(
Buffer.from(options.previewModeEncryptionKey), Buffer.from(options.previewModeEncryptionKey),
encryptedPreviewData.data encryptedPreviewData.data
...@@ -286,6 +288,10 @@ function setPreviewData<T>( ...@@ -286,6 +288,10 @@ function setPreviewData<T>(
throw new Error('invariant: invalid previewModeSigningKey') throw new Error('invariant: invalid previewModeSigningKey')
} }
const jsonwebtoken =
require('next/dist/compiled/jsonwebtoken') as typeof import('next/dist/compiled/jsonwebtoken')
const { encryptWithSecret } =
require('../crypto-utils') as typeof import('../crypto-utils')
const payload = jsonwebtoken.sign( const payload = jsonwebtoken.sign(
{ {
data: encryptWithSecret( data: encryptWithSecret(
......
...@@ -4,21 +4,6 @@ import type { HTMLElement } from 'next/dist/compiled/node-html-parser' ...@@ -4,21 +4,6 @@ import type { HTMLElement } from 'next/dist/compiled/node-html-parser'
import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants' import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants'
import { nonNullable } from '../lib/non-nullable' import { nonNullable } from '../lib/non-nullable'
let optimizeAmp: typeof import('./optimize-amp').default | undefined
let getFontDefinitionFromManifest:
| typeof import('./font-utils').getFontDefinitionFromManifest
| undefined
let parse: typeof import('next/dist/compiled/node-html-parser').parse
if (process.env.NEXT_RUNTIME !== 'edge') {
optimizeAmp = require('./optimize-amp').default
getFontDefinitionFromManifest =
require('./font-utils').getFontDefinitionFromManifest
parse = (
require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser')
).parse
}
type postProcessOptions = { type postProcessOptions = {
optimizeFonts: any optimizeFonts: any
} }
...@@ -56,6 +41,9 @@ async function processHTML( ...@@ -56,6 +41,9 @@ async function processHTML(
if (!middlewareRegistry[0]) { if (!middlewareRegistry[0]) {
return html return html
} }
const { parse } =
require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser')
const root: HTMLElement = parse(html) const root: HTMLElement = parse(html)
let document = html let document = html
...@@ -190,6 +178,8 @@ async function postProcessHTML( ...@@ -190,6 +178,8 @@ async function postProcessHTML(
const postProcessors: Array<(html: string) => Promise<string>> = [ const postProcessors: Array<(html: string) => Promise<string>> = [
process.env.NEXT_RUNTIME !== 'edge' && inAmpMode process.env.NEXT_RUNTIME !== 'edge' && inAmpMode
? async (html: string) => { ? async (html: string) => {
const optimizeAmp = require('./optimize-amp')
.default as typeof import('./optimize-amp').default
html = await optimizeAmp!(html, renderOpts.ampOptimizerConfig) html = await optimizeAmp!(html, renderOpts.ampOptimizerConfig)
if (!renderOpts.ampSkipValidation && renderOpts.ampValidator) { if (!renderOpts.ampSkipValidation && renderOpts.ampValidator) {
await renderOpts.ampValidator(html, pathname) await renderOpts.ampValidator(html, pathname)
...@@ -201,6 +191,8 @@ async function postProcessHTML( ...@@ -201,6 +191,8 @@ async function postProcessHTML(
? async (html: string) => { ? async (html: string) => {
const getFontDefinition = (url: string): string => { const getFontDefinition = (url: string): string => {
if (renderOpts.fontManifest) { if (renderOpts.fontManifest) {
const { getFontDefinitionFromManifest } =
require('./font-utils') as typeof import('./font-utils')
return getFontDefinitionFromManifest!( return getFontDefinitionFromManifest!(
url, url,
renderOpts.fontManifest renderOpts.fontManifest
......
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