Skip to content

Commit

Permalink
perf: lazy evaluate more modules (vercel#41354)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
feedthejim authored and Kikobeats committed Oct 24, 2022
1 parent ce6cfac commit f8dbc0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
10 changes: 8 additions & 2 deletions packages/next/server/api-utils/node.ts
Expand Up @@ -11,8 +11,6 @@ import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import type { PreviewData } from 'next/types'

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 { sendEtagResponse } from '../send-payload'
import { Stream } from 'stream'
Expand Down Expand Up @@ -88,6 +86,8 @@ export function tryGetPreviewData(
data: string
}
try {
const jsonwebtoken =
require('next/dist/compiled/jsonwebtoken') as typeof import('next/dist/compiled/jsonwebtoken')
encryptedPreviewData = jsonwebtoken.verify(
tokenPreviewData,
options.previewModeSigningKey
Expand All @@ -98,6 +98,8 @@ export function tryGetPreviewData(
return false
}

const { decryptWithSecret } =
require('../crypto-utils') as typeof import('../crypto-utils')
const decryptedPreviewData = decryptWithSecret(
Buffer.from(options.previewModeEncryptionKey),
encryptedPreviewData.data
Expand Down Expand Up @@ -286,6 +288,10 @@ function setPreviewData<T>(
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(
{
data: encryptWithSecret(
Expand Down
22 changes: 7 additions & 15 deletions packages/next/server/post-process.ts
Expand Up @@ -4,21 +4,6 @@ import type { HTMLElement } from 'next/dist/compiled/node-html-parser'
import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants'
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 = {
optimizeFonts: any
}
Expand Down Expand Up @@ -56,6 +41,9 @@ async function processHTML(
if (!middlewareRegistry[0]) {
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)
let document = html

Expand Down Expand Up @@ -190,6 +178,8 @@ async function postProcessHTML(
const postProcessors: Array<(html: string) => Promise<string>> = [
process.env.NEXT_RUNTIME !== 'edge' && inAmpMode
? async (html: string) => {
const optimizeAmp = require('./optimize-amp')
.default as typeof import('./optimize-amp').default
html = await optimizeAmp!(html, renderOpts.ampOptimizerConfig)
if (!renderOpts.ampSkipValidation && renderOpts.ampValidator) {
await renderOpts.ampValidator(html, pathname)
Expand All @@ -201,6 +191,8 @@ async function postProcessHTML(
? async (html: string) => {
const getFontDefinition = (url: string): string => {
if (renderOpts.fontManifest) {
const { getFontDefinitionFromManifest } =
require('./font-utils') as typeof import('./font-utils')
return getFontDefinitionFromManifest!(
url,
renderOpts.fontManifest
Expand Down

0 comments on commit f8dbc0e

Please sign in to comment.