Skip to content

Commit

Permalink
Dev Service (#56442)
Browse files Browse the repository at this point in the history
This replaces the `(global as any)._nextDevHandlers` invocation with references to a specific service instance while also removing the module scoped `devInstances`. This ensures that correct types are used.

This was done while changing the `match` parameter in `ensurePage` to `definition` which didn't cause a typescript error (it should have).
  • Loading branch information
wyattjoh authored Oct 5, 2023
1 parent d21025c commit a44b4f8
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 191 deletions.
20 changes: 10 additions & 10 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

# Tooling & Telemetry

/packages/next/src/build/ @timneutkens @ijjk @shuding @huozhi @ztanner @feedthejim @vercel/web-tooling
/packages/next/src/server/lib/router-utils/setup-dev.ts @timneutkens @ijjk @shuding @huozhi @feedthejim @ztanner @wyattjoh @vercel/web-tooling
/packages/next/src/telemetry/ @timneutkens @ijjk @shuding @padmaia
/packages/next-swc/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
Cargo.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
Cargo.lock @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.cargo/config.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.config/nextest.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/test/build-turbopack-tests-manifest.js @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/test/turbopack-tests-manifest.json @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/packages/next/src/build/ @timneutkens @ijjk @shuding @huozhi @ztanner @feedthejim @vercel/web-tooling
/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts @timneutkens @ijjk @shuding @huozhi @feedthejim @ztanner @wyattjoh @vercel/web-tooling
/packages/next/src/telemetry/ @timneutkens @ijjk @shuding @padmaia
/packages/next-swc/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
Cargo.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
Cargo.lock @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.cargo/config.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.config/nextest.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/test/build-turbopack-tests-manifest.js @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/test/turbopack-tests-manifest.json @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
6 changes: 3 additions & 3 deletions packages/next/src/server/dev/hot-reloader-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { UrlObject } from 'url'
import type { Duplex } from 'stream'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type getBaseWebpackConfig from '../../build/webpack-config'
import type { RouteMatch } from '../future/route-matches/route-match'
import type { RouteDefinition } from '../future/route-definitions/route-definition'
import type { Project, Update as TurbopackUpdate } from '../../build/swc'
import type { VersionInfo } from './parse-version-info'

Expand Down Expand Up @@ -134,13 +134,13 @@ export interface NextJsHotReloaderInterface {
page,
clientOnly,
appPaths,
match,
definition,
isApp,
}: {
page: string
clientOnly: boolean
appPaths?: ReadonlyArray<string> | null
isApp?: boolean
match?: RouteMatch
definition: RouteDefinition | undefined
}): Promise<void>
}
8 changes: 4 additions & 4 deletions packages/next/src/server/dev/hot-reloader-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Duplex } from 'stream'
import type { Telemetry } from '../../telemetry/storage'
import type { IncomingMessage, ServerResponse } from 'http'
import type { UrlObject } from 'url'
import type { RouteDefinition } from '../future/route-definitions/route-definition'

import { webpack, StringXor } from 'next/dist/compiled/webpack/webpack'
import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
Expand Down Expand Up @@ -60,7 +61,6 @@ import ws from 'next/dist/compiled/ws'
import { existsSync, promises as fs } from 'fs'
import { UnwrapPromise } from '../../lib/coalesced-function'
import { getRegistry } from '../../lib/helpers/get-registry'
import { RouteMatch } from '../future/route-matches/route-match'
import { parseVersionInfo, VersionInfo } from './parse-version-info'
import { isAPIRoute } from '../../lib/is-api-route'
import { getRouteLoaderEntry } from '../../build/webpack/loaders/next-route-loader'
Expand Down Expand Up @@ -1466,14 +1466,14 @@ export default class HotReloader implements NextJsHotReloaderInterface {
page,
clientOnly,
appPaths,
match,
definition,
isApp,
}: {
page: string
clientOnly: boolean
appPaths?: ReadonlyArray<string> | null
isApp?: boolean
match?: RouteMatch
definition?: RouteDefinition
}): Promise<void> {
// Make sure we don't re-build or dispose prebuilt pages
if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
Expand All @@ -1490,7 +1490,7 @@ export default class HotReloader implements NextJsHotReloaderInterface {
page,
clientOnly,
appPaths,
match,
definition,
isApp,
})
}
Expand Down
48 changes: 24 additions & 24 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import type { UrlWithParsedQuery } from 'url'
import type { BaseNextRequest, BaseNextResponse } from '../base-http'
import type { FallbackMode, MiddlewareRoutingItem } from '../base-server'
import type { FunctionComponent } from 'react'
import type { RouteMatch } from '../future/route-matches/route-match'
import type { RouteDefinition } from '../future/route-definitions/route-definition'
import type { RouteMatcherManager } from '../future/route-matcher-managers/route-matcher-manager'
import type {
NextParsedUrlQuery,
NextUrlWithParsedQuery,
} from '../request-meta'
import type { DevBundlerService } from '../lib/dev-bundler-service'

import fs from 'fs'
import { Worker } from 'next/dist/compiled/jest-worker'
Expand Down Expand Up @@ -80,6 +81,11 @@ export interface Options extends ServerOptions {
* Tells of Next.js is running from the `next dev` command
*/
isNextDevCommand?: boolean

/**
* Interface to the development bundler.
*/
bundlerService: DevBundlerService
}

export default class DevServer extends Server {
Expand All @@ -92,15 +98,12 @@ export default class DevServer extends Server {
private actualInstrumentationHookFile?: string
private middleware?: MiddlewareRoutingItem
private originalFetch: typeof fetch
private readonly bundlerService: DevBundlerService
private staticPathsCache: LRUCache<
string,
UnwrapPromise<ReturnType<DevServer['getStaticPaths']>>
>

private invokeDevMethod({ method, args }: { method: string; args: any[] }) {
return (global as any)._nextDevHandlers[method](this.dir, ...args)
}

protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}
Expand Down Expand Up @@ -140,6 +143,7 @@ export default class DevServer extends Server {
Error.stackTraceLimit = 50
} catch {}
super({ ...options, dev: true })
this.bundlerService = options.bundlerService
this.originalFetch = global.fetch
this.renderOpts.dev = true
this.renderOpts.appDirDevErrorLogger = (err: any) =>
Expand Down Expand Up @@ -190,7 +194,7 @@ export default class DevServer extends Server {
const ensurer: RouteEnsurer = {
ensure: async (match) => {
await this.ensurePage({
match,
definition: match.definition,
page: match.definition.page,
clientOnly: false,
})
Expand Down Expand Up @@ -487,10 +491,7 @@ export default class DevServer extends Server {
err?: unknown,
type?: 'unhandledRejection' | 'uncaughtException' | 'warning' | 'app-dir'
): Promise<void> {
await this.invokeDevMethod({
method: 'logErrorWithOriginalStack',
args: [err, type],
})
await this.bundlerService.logErrorWithOriginalStack(err, type)
}

protected getPagesManifest(): PagesManifest | undefined {
Expand Down Expand Up @@ -534,6 +535,7 @@ export default class DevServer extends Server {
return this.ensurePage({
page: this.actualMiddlewareFile!,
clientOnly: false,
definition: undefined,
})
}

Expand All @@ -543,6 +545,7 @@ export default class DevServer extends Server {
(await this.ensurePage({
page: this.actualInstrumentationHookFile!,
clientOnly: false,
definition: undefined,
})
.then(() => true)
.catch(() => false))
Expand Down Expand Up @@ -570,7 +573,12 @@ export default class DevServer extends Server {
page: string
appPaths: string[] | null
}) {
return this.ensurePage({ page, appPaths, clientOnly: false })
return this.ensurePage({
page,
appPaths,
clientOnly: false,
definition: undefined,
})
}

generateRoutes(_dev?: boolean) {
Expand Down Expand Up @@ -723,12 +731,9 @@ export default class DevServer extends Server {
page: string
clientOnly: boolean
appPaths?: ReadonlyArray<string> | null
match?: RouteMatch
definition: RouteDefinition | undefined
}): Promise<void> {
await this.invokeDevMethod({
method: 'ensurePage',
args: [opts],
})
await this.bundlerService.ensurePage(opts)
}

protected async findPageComponents({
Expand Down Expand Up @@ -759,6 +764,7 @@ export default class DevServer extends Server {
page,
appPaths,
clientOnly: false,
definition: undefined,
})
}

Expand All @@ -785,17 +791,11 @@ export default class DevServer extends Server {
}

protected async getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null> {
await this.invokeDevMethod({
method: 'getFallbackErrorComponents',
args: [],
})
await this.bundlerService.getFallbackErrorComponents()
return await loadDefaultErrorComponents(this.distDir)
}

async getCompilationError(page: string): Promise<any> {
return await this.invokeDevMethod({
method: 'getCompilationError',
args: [page],
})
return await this.bundlerService.getCompilationError(page)
}
}
Loading

0 comments on commit a44b4f8

Please sign in to comment.