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

refactor: updated waitUntil types #65724

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import { AppRouteRouteMatcherProvider } from './future/route-matcher-providers/a
import { PagesAPIRouteMatcherProvider } from './future/route-matcher-providers/pages-api-route-matcher-provider'
import { PagesRouteMatcherProvider } from './future/route-matcher-providers/pages-route-matcher-provider'
import { ServerManifestLoader } from './future/route-matcher-providers/helpers/manifest-loaders/server-manifest-loader'
import { getTracer, isBubbledError, SpanKind } from './lib/trace/tracer'
import { getTracer, SpanKind } from './lib/trace/tracer'
import { BaseServerSpan } from './lib/trace/constants'
import { I18NProvider } from './future/helpers/i18n-provider'
import { sendResponse } from './send-response'
Expand Down Expand Up @@ -150,6 +150,7 @@ import {
getBuiltinRequestContext,
type WaitUntil,
} from './after/builtin-request-context'
import { BubbledError, isBubbledError } from './lib/trace/bubble-error'

export type FindComponentsResult = {
components: LoadComponentsReturnType
Expand Down Expand Up @@ -1378,16 +1379,11 @@ export default abstract class Server<
)
if (finished) return

const err = new Error()
;(err as any).result = {
throw new BubbledError(true, {
response: new Response(null, {
headers: {
'x-middleware-next': '1',
},
headers: { 'x-middleware-next': '1' },
}),
}
;(err as any).bubble = true
throw err
})
}

// This wasn't a request via the matched path or the invoke path, so
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default class DevServer extends Server {
return result
}

result.waitUntil.catch((error) => {
result.waitUntil?.catch((error) => {
this.logErrorWithOriginalStack(error, 'unhandledRejection')
})
return result
Expand Down
15 changes: 15 additions & 0 deletions packages/next/src/server/lib/trace/bubble-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { FetchEventResult } from '../../web/types'

export class BubbledError extends Error {
constructor(
public readonly bubble?: boolean,
public readonly result?: FetchEventResult
) {
super()
}
}

export function isBubbledError(error: unknown): error is BubbledError {
if (typeof error !== 'object' || error === null) return false
return error instanceof BubbledError
}
16 changes: 1 addition & 15 deletions packages/next/src/server/lib/trace/tracer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FetchEventResult } from '../../web/types'
import type { TextMapSetter } from '@opentelemetry/api'
import { isBubbledError } from './bubble-error'
import type { SpanTypes } from './constants'
import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'

Expand Down Expand Up @@ -38,20 +38,6 @@ const isPromise = <T>(p: any): p is Promise<T> => {
return p !== null && typeof p === 'object' && typeof p.then === 'function'
}

export class BubbledError extends Error {
constructor(
public readonly bubble?: boolean,
public readonly result?: FetchEventResult
) {
super()
}
}

export function isBubbledError(error: unknown): error is BubbledError {
if (typeof error !== 'object' || error === null) return false
return error instanceof BubbledError
}

const closeSpanWithError = (span: Span, error?: Error) => {
if (isBubbledError(error) && error.bubble) {
span.setAttribute('next.bubble', true)
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import {
INSTRUMENTATION_HOOK_FILENAME,
RSC_PREFETCH_SUFFIX,
} from '../lib/constants'
import { BubbledError, getTracer } from './lib/trace/tracer'
import { getTracer } from './lib/trace/tracer'
import { NextNodeServerSpan } from './lib/trace/constants'
import { nodeFs } from './lib/node-fs-methods'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
Expand All @@ -105,6 +105,7 @@ import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import type { NextFontManifest } from '../build/webpack/plugins/next-font-manifest-plugin'
import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-rewrites'
import { stripNextRscUnionQuery } from '../lib/url'
import { BubbledError } from './lib/trace/bubble-error'

export * from './base-server'

Expand Down Expand Up @@ -1607,7 +1608,7 @@ export default class NextNodeServer extends BaseServer<
})

if (!this.renderOpts.dev) {
result.waitUntil.catch((error) => {
result.waitUntil?.catch((error) => {
console.error(`Uncaught: middleware waitUntil errored`, error)
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/web/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type NodejsRequestData = Omit<RequestData, 'body'> & {

export interface FetchEventResult {
response: Response
waitUntil: Promise<any>
waitUntil?: Promise<any>
fetchMetrics?: FetchMetrics
}

Expand Down
Loading