Skip to content

Commit 67c4a20

Browse files
authored
fix(next): properly instantiates req.url on localhost (#11455)
The `req.url` property at the page level was not reflective of the actual URL on localhost. This was because we were passing an incompatible `url` override into `createLocalReq` (lacking protocol). This would silently fail to construct the URL object, ultimately losing the top-level domain on `req.url` as well as the port on `req.origin` (see #11454). Closes #11448.
1 parent 38131ed commit 67c4a20

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

packages/next/src/utilities/initPage/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { InitPageResult, VisibleEntities } from 'payload'
22

3-
import { headers as getHeaders } from 'next/headers.js'
43
import { notFound } from 'next/navigation.js'
5-
import { getPayload, isEntityHidden, parseCookies } from 'payload'
4+
import { isEntityHidden } from 'payload'
65
import * as qs from 'qs-esm'
76

87
import type { Args } from './types.js'
@@ -40,8 +39,8 @@ export const initPage = async ({
4039
ignoreQueryPrefix: true,
4140
}),
4241
},
42+
urlSuffix: `${route}${searchParams ? queryString : ''}`,
4343
},
44-
urlSuffix: `${route}${searchParams ? queryString : ''}`,
4544
})
4645

4746
const {

packages/next/src/utilities/initReq.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ export const initReq = async function ({
5353
importMap,
5454
key,
5555
overrides,
56-
urlSuffix,
5756
}: {
5857
configPromise: Promise<SanitizedConfig> | SanitizedConfig
5958
importMap: ImportMap
6059
key: string
6160
overrides?: Parameters<typeof createLocalReq>[0]
62-
urlSuffix?: string
6361
}): Promise<Result> {
6462
const headers = await getHeaders()
6563
const cookies = parseCookies(headers)
@@ -96,14 +94,14 @@ export const initReq = async function ({
9694
const { i18n, languageCode, payload, responseHeaders, user } = partialResult
9795

9896
const { req: reqOverrides, ...optionsOverrides } = overrides || {}
97+
9998
const req = await createLocalReq(
10099
{
101100
req: {
102101
headers,
103102
host: headers.get('host'),
104103
i18n: i18n as I18n,
105104
responseHeaders,
106-
url: `${payload.config.serverURL}${urlSuffix || ''}`,
107105
user,
108106
...(reqOverrides || {}),
109107
},
@@ -115,6 +113,7 @@ export const initReq = async function ({
115113
const locale = await getRequestLocale({
116114
req,
117115
})
116+
118117
req.locale = locale?.code
119118

120119
const permissions = await getAccessResults({

packages/payload/src/utilities/createLocalReq.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getRequestContext(
2323
}
2424
}
2525

26-
const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
26+
const attachFakeURLProperties = (req: Partial<PayloadRequest>, urlSuffix?: string) => {
2727
/**
2828
* *NOTE*
2929
* If no URL is provided, the local API was called outside
@@ -38,13 +38,20 @@ const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
3838
return urlObject
3939
}
4040

41-
const fallbackURL = `http://${req.host || 'localhost'}`
41+
const fallbackURL = `http://${req.host || 'localhost'}${urlSuffix || ''}`
4242

43-
const urlToUse = req?.url || req.payload.config?.serverURL || fallbackURL
43+
const urlToUse =
44+
req?.url || req.payload.config?.serverURL
45+
? `${req.payload.config.serverURL}${urlSuffix || ''}`
46+
: fallbackURL
4447

4548
try {
4649
urlObject = new URL(urlToUse)
4750
} catch (_err) {
51+
req.payload.logger.error(
52+
`Failed to create URL object from URL: ${urlToUse}, falling back to ${fallbackURL}`,
53+
)
54+
4855
urlObject = new URL(fallbackURL)
4956
}
5057

@@ -85,13 +92,14 @@ type CreateLocalReq = (
8592
fallbackLocale?: false | TypedLocale
8693
locale?: string
8794
req?: Partial<PayloadRequest>
95+
urlSuffix?: string
8896
user?: User
8997
},
9098
payload: Payload,
9199
) => Promise<PayloadRequest>
92100

93101
export const createLocalReq: CreateLocalReq = async (
94-
{ context, fallbackLocale, locale: localeArg, req = {} as PayloadRequest, user },
102+
{ context, fallbackLocale, locale: localeArg, req = {} as PayloadRequest, urlSuffix, user },
95103
payload,
96104
) => {
97105
const localization = payload.config?.localization
@@ -131,7 +139,7 @@ export const createLocalReq: CreateLocalReq = async (
131139
req.routeParams = req?.routeParams || {}
132140
req.query = req?.query || {}
133141

134-
attachFakeURLProperties(req)
142+
attachFakeURLProperties(req, urlSuffix)
135143

136144
return req as PayloadRequest
137145
}

0 commit comments

Comments
 (0)