Skip to content

Commit a433443

Browse files
Merge branch 'canary' into refactor/revalidate-type
2 parents 59db0f8 + 9288ef4 commit a433443

File tree

28 files changed

+106
-143
lines changed

28 files changed

+106
-143
lines changed

examples/radix-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"dependencies": {
99
"@radix-ui/react-dropdown-menu": "1.0.0",
10-
"@radix-ui/react-icons": "1.1.1",
10+
"@radix-ui/react-icons": "1.3.0",
1111
"next": "latest",
1212
"react": "latest",
1313
"react-dom": "latest"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename this file to `.env.local` to use environment variables locally with `next dev`
2+
# https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
3+
MY_HOST="example.com"

packages/next/src/build/handle-externals.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export async function resolveExternal(
4444
context: string,
4545
request: string,
4646
isEsmRequested: boolean,
47+
hasAppDir: boolean,
4748
getResolve: (
4849
options: any
4950
) => (
@@ -65,7 +66,11 @@ export async function resolveExternal(
6566

6667
let preferEsmOptions =
6768
esmExternals && isEsmRequested ? [true, false] : [false]
68-
69+
// Disable esm resolving for app/ and pages/ so for esm package using under pages/
70+
// won't load react through esm loader
71+
if (hasAppDir) {
72+
preferEsmOptions = [false]
73+
}
6974
for (const preferEsm of preferEsmOptions) {
7075
const resolve = getResolve(
7176
preferEsm ? esmResolveOptions : nodeResolveOptions
@@ -130,10 +135,12 @@ export function makeExternalHandler({
130135
config,
131136
optOutBundlingPackageRegex,
132137
dir,
138+
hasAppDir,
133139
}: {
134140
config: NextConfigComplete
135141
optOutBundlingPackageRegex: RegExp
136142
dir: string
143+
hasAppDir: boolean
137144
}) {
138145
let resolvedExternalPackageDirs: Map<string, string>
139146
const looseEsmExternals = config.experimental?.esmExternals === 'loose'
@@ -286,6 +293,7 @@ export function makeExternalHandler({
286293
context,
287294
request,
288295
isEsmRequested,
296+
hasAppDir,
289297
getResolve,
290298
isLocal ? resolveNextExternal : undefined
291299
)
@@ -345,6 +353,7 @@ export function makeExternalHandler({
345353
config.experimental.esmExternals,
346354
context,
347355
pkg + '/package.json',
356+
hasAppDir,
348357
isEsmRequested,
349358
getResolve,
350359
isLocal ? resolveNextExternal : undefined

packages/next/src/build/webpack-config-rules/resolve.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

packages/next/src/build/webpack-config.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ import { needsExperimentalReact } from '../lib/needs-experimental-react'
7474
import { getDefineEnvPlugin } from './webpack/plugins/define-env-plugin'
7575
import type { SWCLoaderOptions } from './webpack/loaders/next-swc-loader'
7676
import { isResourceInPackages, makeExternalHandler } from './handle-externals'
77-
import {
78-
getMainField,
79-
edgeConditionNames,
80-
} from './webpack-config-rules/resolve'
8177

8278
type ExcludesFalse = <T>(x: T | false) => x is T
8379
type ClientEntries = {
@@ -108,6 +104,21 @@ const babelIncludeRegexes: RegExp[] = [
108104
const asyncStoragesRegex =
109105
/next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/
110106

107+
// exports.<conditionName>
108+
const edgeConditionNames = [
109+
'edge-light',
110+
'worker',
111+
// inherits the default conditions
112+
'...',
113+
]
114+
115+
// packageJson.<mainField>
116+
const mainFieldsPerCompiler: Record<CompilerNameValues, string[]> = {
117+
[COMPILER_NAMES.server]: ['main', 'module'],
118+
[COMPILER_NAMES.client]: ['browser', 'module', 'main'],
119+
[COMPILER_NAMES.edgeServer]: edgeConditionNames,
120+
}
121+
111122
// Support for NODE_PATH
112123
const nodePathList = (process.env.NODE_PATH || '')
113124
.split(process.platform === 'win32' ? ';' : ':')
@@ -920,8 +931,7 @@ export default async function getBaseWebpackConfig(
920931
},
921932
}
922933
: undefined),
923-
// default main fields use pages dir ones, and customize app router ones in loaders.
924-
mainFields: getMainField('pages', compilerType),
934+
mainFields: mainFieldsPerCompiler[compilerType],
925935
...(isEdgeServer && {
926936
conditionNames: edgeConditionNames,
927937
}),
@@ -1029,6 +1039,7 @@ export default async function getBaseWebpackConfig(
10291039
config,
10301040
optOutBundlingPackageRegex,
10311041
dir,
1042+
hasAppDir,
10321043
})
10331044

10341045
const shouldIncludeExternalDirs =
@@ -1599,7 +1610,6 @@ export default async function getBaseWebpackConfig(
15991610
],
16001611
},
16011612
resolve: {
1602-
mainFields: getMainField('app', compilerType),
16031613
conditionNames: reactServerCondition,
16041614
// If missing the alias override here, the default alias will be used which aliases
16051615
// react to the direct file path, not the package name. In that case the condition
@@ -1744,9 +1754,6 @@ export default async function getBaseWebpackConfig(
17441754
],
17451755
exclude: [codeCondition.exclude],
17461756
use: swcLoaderForClientLayer,
1747-
resolve: {
1748-
mainFields: getMainField('app', compilerType),
1749-
},
17501757
},
17511758
]
17521759
: []),

packages/next/src/build/webpack/plugins/next-trace-entrypoints-plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
743743
context,
744744
request,
745745
isEsmRequested,
746+
!!this.appDirEnabled,
746747
(options) => (_: string, resRequest: string) => {
747748
return getResolve(options)(parent, resRequest, job)
748749
},

packages/next/src/lib/polyfill-promise-with-resolvers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ if (
99
!('withResolvers' in Promise) ||
1010
typeof Promise.withResolvers !== 'function'
1111
) {
12-
Promise.withResolvers = <T>() => {
12+
Promise.withResolvers = <T>(): {
13+
readonly promise: Promise<T>
14+
readonly resolve: (value: T | PromiseLike<T>) => void
15+
readonly reject: (reason: any) => void
16+
} => {
1317
let resolvers: {
1418
resolve: (value: T | PromiseLike<T>) => void
1519
reject: (reason: any) => void

packages/next/src/server/dev/next-dev-server.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import type {
1515
NextUrlWithParsedQuery,
1616
} from '../request-meta'
1717
import type { DevBundlerService } from '../lib/dev-bundler-service'
18+
import type { IncrementalCache } from '../lib/incremental-cache'
19+
import type { UnwrapPromise } from '../../lib/coalesced-function'
20+
import type { NodeNextResponse, NodeNextRequest } from '../base-http/node'
21+
import type { RouteEnsurer } from '../future/route-matcher-managers/dev-route-matcher-manager'
22+
import type { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
1823

1924
import fs from 'fs'
2025
import { Worker } from 'next/dist/compiled/jest-worker'
@@ -39,26 +44,21 @@ import { setGlobal } from '../../trace'
3944
import { findPageFile } from '../lib/find-page-file'
4045
import { getNodeOptionsWithoutInspect } from '../lib/utils'
4146
import { withCoalescedInvoke } from '../../lib/coalesced-function'
42-
import type { UnwrapPromise } from '../../lib/coalesced-function'
4347
import { loadDefaultErrorComponents } from '../load-default-error-components'
4448
import { DecodeError, MiddlewareNotFoundError } from '../../shared/lib/utils'
4549
import * as Log from '../../build/output/log'
4650
import isError, { getProperError } from '../../lib/is-error'
47-
import type { NodeNextResponse, NodeNextRequest } from '../base-http/node'
4851
import { isMiddlewareFile } from '../../build/utils'
4952
import { formatServerError } from '../../lib/format-server-error'
5053
import { DevRouteMatcherManager } from '../future/route-matcher-managers/dev-route-matcher-manager'
51-
import type { RouteEnsurer } from '../future/route-matcher-managers/dev-route-matcher-manager'
5254
import { DevPagesRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-pages-route-matcher-provider'
5355
import { DevPagesAPIRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-pages-api-route-matcher-provider'
5456
import { DevAppPageRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-app-page-route-matcher-provider'
5557
import { DevAppRouteRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-app-route-route-matcher-provider'
56-
import type { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
5758
import { NodeManifestLoader } from '../future/route-matcher-providers/helpers/manifest-loaders/node-manifest-loader'
5859
import { BatchedFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/batched-file-reader'
5960
import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/default-file-reader'
6061
import { NextBuildContext } from '../../build/build-context'
61-
import type { IncrementalCache } from '../lib/incremental-cache'
6262
import LRUCache from 'next/dist/compiled/lru-cache'
6363
import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
6464

@@ -85,8 +85,11 @@ export interface Options extends ServerOptions {
8585
}
8686

8787
export default class DevServer extends Server {
88-
private devReady: Promise<void>
89-
private setDevReady?: Function
88+
/**
89+
* The promise that resolves when the server is ready. When this is unset
90+
* the server is ready.
91+
*/
92+
private ready? = Promise.withResolvers<void>()
9093
protected sortedRoutes?: string[]
9194
private pagesDir?: string
9295
private appDir?: string
@@ -145,9 +148,6 @@ export default class DevServer extends Server {
145148
this.renderOpts.appDirDevErrorLogger = (err: any) =>
146149
this.logErrorWithOriginalStack(err, 'app-dir')
147150
;(this.renderOpts as any).ErrorDebug = ReactDevOverlay
148-
this.devReady = new Promise((resolve) => {
149-
this.setDevReady = resolve
150-
})
151151
this.staticPathsCache = new LRUCache({
152152
// 5MB
153153
max: 5 * 1024 * 1024,
@@ -268,7 +268,9 @@ export default class DevServer extends Server {
268268
await super.prepareImpl()
269269
await this.runInstrumentationHookIfAvailable()
270270
await this.matchers.reload()
271-
this.setDevReady!()
271+
272+
this.ready?.resolve()
273+
this.ready = undefined
272274

273275
// This is required by the tracing subsystem.
274276
setGlobal('appDir', this.appDir)
@@ -428,7 +430,7 @@ export default class DevServer extends Server {
428430
res: BaseNextResponse,
429431
parsedUrl?: NextUrlWithParsedQuery
430432
): Promise<void> {
431-
await this.devReady
433+
await this.ready?.promise
432434
return await super.handleRequest(req, res, parsedUrl)
433435
}
434436

@@ -437,7 +439,7 @@ export default class DevServer extends Server {
437439
res: NodeNextResponse,
438440
parsedUrl: UrlWithParsedQuery
439441
): Promise<void> {
440-
await this.devReady
442+
await this.ready?.promise
441443

442444
const { basePath } = this.nextConfig
443445
let originalPathname: string | null = null
@@ -748,7 +750,8 @@ export default class DevServer extends Server {
748750
appPaths?: ReadonlyArray<string> | null
749751
shouldEnsure: boolean
750752
}): Promise<FindComponentsResult | null> {
751-
await this.devReady
753+
await this.ready?.promise
754+
752755
const compilationErr = await this.getCompilationError(page)
753756
if (compilationErr) {
754757
// Wrap build errors so that they don't get logged again

packages/next/src/server/future/route-matcher-managers/default-route-matcher-manager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
1+
// This takes advantage of `Promise.withResolvers` which is polyfilled in
2+
// this imported module.
3+
import '../../../lib/polyfill-promise-with-resolvers'
24

35
import { isDynamicRoute } from '../../../shared/lib/router/utils'
46
import type { RouteKind } from '../route-kind'
@@ -44,10 +46,8 @@ export class DefaultRouteMatcherManager implements RouteMatcherManager {
4446

4547
private previousMatchers: ReadonlyArray<RouteMatcher> = []
4648
public async reload() {
47-
let callbacks: { resolve: Function; reject: Function }
48-
this.waitTillReadyPromise = new Promise((resolve, reject) => {
49-
callbacks = { resolve, reject }
50-
})
49+
const { promise, resolve, reject } = Promise.withResolvers<void>()
50+
this.waitTillReadyPromise = promise
5151

5252
// Grab the compilation ID for this run, we'll verify it at the end to
5353
// ensure that if any routes were added before reloading is finished that
@@ -182,11 +182,11 @@ export class DefaultRouteMatcherManager implements RouteMatcherManager {
182182
)
183183
}
184184
} catch (err) {
185-
callbacks!.reject(err)
185+
reject(err)
186186
} finally {
187187
// The compilation ID matched, so mark the complication as finished.
188188
this.lastCompilationID = compilationID
189-
callbacks!.resolve()
189+
resolve()
190190
}
191191
}
192192

packages/next/src/server/image-optimizer.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,3 @@ export async function getImageSize(
794794
const { width, height } = imageSizeOf(buffer)
795795
return { width, height }
796796
}
797-
798-
export class Deferred<T> {
799-
promise: Promise<T>
800-
resolve!: (value: T) => void
801-
reject!: (error?: Error) => void
802-
803-
constructor() {
804-
this.promise = new Promise((resolve, reject) => {
805-
this.resolve = resolve
806-
this.reject = reject
807-
})
808-
}
809-
}

packages/next/src/server/response-cache/web.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,11 @@ export default class WebResponseCache {
4141
return pendingResponse
4242
}
4343

44-
let resolver: (cacheEntry: ResponseCacheEntry | null) => void = () => {}
45-
let rejecter: (error: Error) => void = () => {}
46-
const promise: Promise<ResponseCacheEntry | null> = new Promise(
47-
(resolve, reject) => {
48-
resolver = resolve
49-
rejecter = reject
50-
}
51-
)
44+
const {
45+
promise,
46+
resolve: resolver,
47+
reject: rejecter,
48+
} = Promise.withResolvers<ResponseCacheEntry | null>()
5249
if (pendingResponseKey) {
5350
this.pendingResponses.set(pendingResponseKey, promise)
5451
}

0 commit comments

Comments
 (0)