Commit b77f77ee authored by Jiwon Choi's avatar Jiwon Choi Committed by Jiachi Liu

perf: improve retrieving `versionInfo` on Turbo HMR (#67309)

Identified the bottleneck of Turbopack HMR, one of the reason is that we
run `execSync` to get user's package manager and fetch their registry to
get the latest & canary version of Next.js.
This process was located at the initial of HMR, which could have been
delayed to the initial of the error handling.

- Remove getting user's package manager and just fetch from NPM
regardless the user uses Yarn.
- Used an async IIFE to await the promise of `getVerionInfo` value
inside the synchronous `ws.handleUpgrade`.

> Benchmarked with console inside try-finally

| Version                              | Ready |
|-------------------------------------|---------|
| Canary | 1185ms |
| Delta | 896ms |
| Delta Webpack vs Canary Webpack | -24.39% |

| Version                              | Ready |
|-------------------------------------|---------|
| Canary | 1002ms |
| Delta (Turbopack) | 509ms |
| Delta Turbopack vs Canary Turbopack | -49.20% |

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 7f401a8f
...@@ -41,7 +41,6 @@ import { ...@@ -41,7 +41,6 @@ import {
} from '../lib/render-server' } from '../lib/render-server'
import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page-path'
import { trace } from '../../trace' import { trace } from '../../trace'
import type { VersionInfo } from './parse-version-info'
import { import {
AssetMapper, AssetMapper,
type ChangeSubscriptions, type ChangeSubscriptions,
...@@ -494,7 +493,7 @@ export async function createHotReloaderTurbopack( ...@@ -494,7 +493,7 @@ export async function createHotReloaderTurbopack(
) )
) )
const overlayMiddleware = getOverlayMiddleware(project) const overlayMiddleware = getOverlayMiddleware(project)
const versionInfo: VersionInfo = await getVersionInfo( const versionInfoPromise = getVersionInfo(
isTestMode || opts.telemetry.isEnabled isTestMode || opts.telemetry.isEnabled
) )
...@@ -656,15 +655,19 @@ export async function createHotReloaderTurbopack( ...@@ -656,15 +655,19 @@ export async function createHotReloaderTurbopack(
} }
} }
const sync: SyncAction = { ;(async function () {
action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC, const versionInfo = await versionInfoPromise
errors,
warnings: [], const sync: SyncAction = {
hash: '', action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC,
versionInfo, errors,
} warnings: [],
hash: '',
versionInfo,
}
sendToClient(client, sync) sendToClient(client, sync)
})()
}) })
}, },
......
...@@ -62,7 +62,6 @@ import { getProperError } from '../../lib/is-error' ...@@ -62,7 +62,6 @@ import { getProperError } from '../../lib/is-error'
import ws from 'next/dist/compiled/ws' import ws from 'next/dist/compiled/ws'
import { existsSync, promises as fs } from 'fs' import { existsSync, promises as fs } from 'fs'
import type { UnwrapPromise } from '../../lib/coalesced-function' import type { UnwrapPromise } from '../../lib/coalesced-function'
import { getRegistry } from '../../lib/helpers/get-registry'
import { parseVersionInfo } from './parse-version-info' import { parseVersionInfo } from './parse-version-info'
import type { VersionInfo } from './parse-version-info' import type { VersionInfo } from './parse-version-info'
import { isAPIRoute } from '../../lib/is-api-route' import { isAPIRoute } from '../../lib/is-api-route'
...@@ -210,10 +209,16 @@ export async function getVersionInfo(enabled: boolean): Promise<VersionInfo> { ...@@ -210,10 +209,16 @@ export async function getVersionInfo(enabled: boolean): Promise<VersionInfo> {
try { try {
installed = require('next/package.json').version installed = require('next/package.json').version
const registry = getRegistry() let res
const res = await fetch(`${registry}-/package/next/dist-tags`)
if (!res.ok) return { installed, staleness: 'unknown' } try {
// use NPM registry regardless user using Yarn
res = await fetch('https://registry.npmjs.org/-/package/next/dist-tags')
} catch {
// ignore fetch errors
}
if (!res || !res.ok) return { installed, staleness: 'unknown' }
const { latest, canary } = await res.json() const { latest, canary } = await res.json()
......
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