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

perf: lazy evaluate more modules #41354

Merged
merged 2 commits into from Oct 13, 2022
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
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