Skip to content

Commit

Permalink
add more lazy modules
Browse files Browse the repository at this point in the history
  • Loading branch information
feedthejim committed Oct 12, 2022
1 parent 65da9a9 commit bb8e5fe
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 bb8e5fe

Please sign in to comment.