Skip to content

Commit 11fa91b

Browse files
authored
Rename url to path for onRequestError request arg (#68672)
### What * Rename the `request.url` in onRequestError callback arg to `request.path` * Align `routeType: "middleware"` with others, only contain the resource path in the `request.path`, stripped out request origin. ### Why For non middleware case, the `req.url` in next-server is actual resource path, which only contains the pathname and search but without request origin. For middleware case, the origin is included in the `req.url`. This might be a implementation detail, but for users, having request origin here might not be critical and super helpful since they will know the deployment as a global context for error tracking. Hence we align the property to a variable that not holding request origin, given a better naming `request.path`. The idea is from `:path` request header, which represents the resource path, only containing pathname and search. e.g. `/a/b?c=2` x-ref: https://greenbytes.de/tech/webdav/draft-ietf-httpbis-http2-09.html#HttpRequest Closes NDX-54
1 parent 90ccf2e commit 11fa91b

File tree

7 files changed

+13
-20
lines changed

7 files changed

+13
-20
lines changed

docs/02-app/02-api-reference/02-file-conventions/instrumentation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ The function accepts three parameters: `error`, `request`, and `context`.
110110
export function onRequestError(
111111
error: { digest: string } & Error,
112112
request: {
113-
url: string // full URL
113+
path: string // resource path, e.g. /blog?name=foo
114114
method: string // request method. e.g. GET, POST, etc
115115
headers: { [key: string]: string }
116116
},

packages/next/src/build/templates/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ function errorHandledHandler(fn: AdapterOptions['handler']) {
2727
return await fn(...args)
2828
} catch (err) {
2929
const req = args[0]
30+
const url = new URL(req.url)
31+
const resource = url.pathname + url.search
3032
await edgeInstrumentationOnRequestError(
3133
err,
3234
{
33-
url: req.url,
35+
path: resource,
3436
method: req.method,
3537
headers: Object.fromEntries(req.headers.entries()),
3638
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ export default abstract class Server<
843843
await this.instrumentation.onRequestError?.(
844844
err,
845845
{
846-
url: req.url || '',
846+
path: req.url || '',
847847
method: req.method || 'GET',
848848
// Normalize middleware headers and other server request headers
849849
headers:

packages/next/src/server/instrumentation/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export type RequestErrorContext = {
1212
export type InstrumentationOnRequestError = (
1313
error: unknown,
1414
errorRequest: Readonly<{
15+
path: string
1516
method: string
16-
url: string
1717
headers: NodeJS.Dict<string | string[]>
1818
}>,
1919
errorContext: Readonly<RequestErrorContext>

test/e2e/on-request-error/basic/basic.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ describe('on-request-error - basic', () => {
1818
errorMessage,
1919
url,
2020
renderSource,
21-
isMiddleware = false,
2221
}: {
2322
errorMessage: string
2423
url: string
2524
renderSource: string | undefined
26-
isMiddleware?: boolean
2725
}) {
2826
// Assert the instrumentation is called
2927
await retry(async () => {
@@ -40,14 +38,8 @@ describe('on-request-error - basic', () => {
4038

4139
const { payload } = record
4240
const { request } = payload
43-
if (isMiddleware) {
44-
// For middleware, the URL is absolute url with host
45-
expect(request.url).toMatch(/^http:\/\//)
46-
expect(request.url).toMatch(url)
47-
} else {
48-
expect(request.url).toBe(url)
49-
}
5041

42+
expect(request.path).toBe(url)
5143
expect(record).toMatchObject({
5244
count: 1,
5345
payload: {
@@ -164,7 +156,6 @@ describe('on-request-error - basic', () => {
164156
await validateErrorRecord({
165157
errorMessage: 'middleware-error',
166158
url: '/middleware-error',
167-
isMiddleware: true,
168159
renderSource: undefined,
169160
})
170161
})

test/e2e/on-request-error/dynamic-routes/dynamic-routes.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('on-request-error - dynamic-routes', () => {
4545
payload: {
4646
message: 'server-dynamic-page-node-error',
4747
request: {
48-
url: '/app-page/dynamic/123?apple=dope',
48+
path: '/app-page/dynamic/123?apple=dope',
4949
},
5050
context: {
5151
routerKind: 'App Router',
@@ -65,7 +65,7 @@ describe('on-request-error - dynamic-routes', () => {
6565
payload: {
6666
message: 'server-dynamic-route-node-error',
6767
request: {
68-
url: '/app-route/dynamic/123?apple=dope',
68+
path: '/app-route/dynamic/123?apple=dope',
6969
},
7070
context: {
7171
routerKind: 'App Router',
@@ -86,7 +86,7 @@ describe('on-request-error - dynamic-routes', () => {
8686
payload: {
8787
message: 'server-suspense-page-node-error',
8888
request: {
89-
url: '/app-page/suspense',
89+
path: '/app-page/suspense',
9090
},
9191
context: {
9292
routerKind: 'App Router',
@@ -109,7 +109,7 @@ describe('on-request-error - dynamic-routes', () => {
109109
payload: {
110110
message: 'pages-page-node-error',
111111
request: {
112-
url: '/pages-page/dynamic/123?apple=dope',
112+
path: '/pages-page/dynamic/123?apple=dope',
113113
},
114114
context: {
115115
routerKind: 'Pages Router',
@@ -130,7 +130,7 @@ describe('on-request-error - dynamic-routes', () => {
130130
payload: {
131131
message: 'pages-api-node-error',
132132
request: {
133-
url: '/api/dynamic/123?apple=dope',
133+
path: '/api/dynamic/123?apple=dope',
134134
},
135135
context: {
136136
routerKind: 'Pages Router',

test/e2e/on-request-error/server-action-error/server-action-error.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('on-request-error - server-action-error', () => {
4040
payload: {
4141
message: errorMessage,
4242
request: {
43-
url,
43+
path: url,
4444
method: 'POST',
4545
headers: expect.objectContaining({
4646
accept: 'text/x-component',

0 commit comments

Comments
 (0)