Skip to content

Commit

Permalink
revert: revert waitUntil change
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed May 14, 2024
1 parent 3fdea0e commit 269f1d2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
14 changes: 9 additions & 5 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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, SpanKind } from './lib/trace/tracer'
import { getTracer, isBubbledError, 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 @@ -137,7 +137,6 @@ import {
XInvokePathRequestAdapter,
} from './request-adapter/x-invoke-path-request-adapter'
import { BaseRequestAdapter } from './request-adapter/base-request-adapter'
import { BubbledError, isBubbledError } from './lib/trace/bubble-error'
import { StandaloneRequestAdapter } from './request-adapter/standalone-request-adapter'

export type FindComponentsResult = {
Expand Down Expand Up @@ -946,11 +945,16 @@ export default abstract class Server<
)
if (finished) return

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

res.statusCode = 200
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: 0 additions & 15 deletions packages/next/src/server/lib/trace/bubble-error.ts

This file was deleted.

16 changes: 15 additions & 1 deletion packages/next/src/server/lib/trace/tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBubbledError } from './bubble-error'
import type { FetchEventResult } from '../../web/types'
import type { SpanTypes } from './constants'
import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'

Expand Down Expand Up @@ -37,6 +37,20 @@ 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: 2 additions & 3 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 { getTracer } from './lib/trace/tracer'
import { BubbledError, 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,7 +105,6 @@ 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 +1606,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 @@ -39,7 +39,7 @@ export type NodejsRequestData = Omit<RequestData, 'body'> & {

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

Expand Down

0 comments on commit 269f1d2

Please sign in to comment.