Repository navigation
fix: basePath not working properly with admin routes - #14967
Conversation
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
There was a problem hiding this comment.
Pull request overview
This PR fixes issues with basePath handling in admin and API routes by introducing two utility functions (formatAdminURL and formatApiURL) that consistently construct URLs throughout the codebase. The key change is removing automatic basePath prepending from the default API route configuration and instead handling it explicitly in the URL formatting functions.
Key Changes:
- Updated
formatAdminURLandformatApiURLhelper functions to properly handlebasePathfrom Next.js config - Replaced manual URL construction throughout the codebase with calls to these helper functions
- Removed
basePathfrom default API route configuration (config.routes.api)
Reviewed changes
Copilot reviewed 154 out of 155 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
packages/payload/src/utilities/formatApiURL.ts |
Completely rewrote function to handle basePath and construct relative/absolute API URLs |
packages/payload/src/utilities/formatAdminURL.ts |
Enhanced to read basePath from environment and handle relative URL generation |
packages/payload/src/config/defaults.ts |
Removed basePath from default API route configuration |
test/versions/shared.ts |
Added BASE_PATH constant for test configuration |
test/base-path/* |
New test suite for basePath functionality |
| Multiple UI/test files | Replaced manual URL construction with formatApiURL/formatAdminURL calls |
Comments suppressed due to low confidence (2)
packages/plugin-seo/src/fields/MetaDescription/MetaDescriptionComponent.tsx:1
- There is an unnecessary blank line (line 109) in the dependency array. Remove the blank line to maintain consistent formatting.
'use client'
packages/payload/src/utilities/handleEndpoints.ts:273
- The caught error is named
_errbut is immediately used aserrin the next line. Remove the underscore prefix since the variable is being used.
} catch (_err) {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
|
🚀 This is included in version v3.69.0 |
This PR adds a base-path test suite, updates the formatAdminURL helper
function.
The helper function allows you to create routes inside the admin panel
that take `basePath` into account when set.
There are a lot of touched files here, most have the same treatment.
## API URL construction changes
Previously we were appending the basePath in the default api
configuration. That is no longer the case and is more predictable to use
the helper function that will generate a correct path/url.
## Admin URL construction changes
The default admin route differed from the default api route, the default
admin route did not prepend the basePath. The reason it did not/should
not, is because `usePathname` from NextJS excludes basePath.
So if we were to prepend it to `routes.admin` and then for example use
`pathname.startsWith(config.routes.admin)` it would always return false
when using a basePath, since `/route` will never start with
`/basePath/route`.
Also, when you do something like `router.push(/adminRoute)` NextJS will
prepend the basePath and push you to `/basePath/adminRoute`. If we
prepended it in the config it would push you to
`/basePath/basePath/adminRoute`.
## Helper function usage
The helper is especially useful for plugin work, this way your plugin
will generate correct urls for users using basePath.
Before we were doing:
```ts
// worked because we prepended basePath to apiRoute
const apiURL = `${serverURL}${apiRoute}/collections/${slug}/${id}`
// never worked since basePath was never prepended to adminRoute
const adminURL = `${serverURL}${adminRoute}/collections/${slug}/${id}`
```
Now we can do:
```ts
import { formatAdminURL } from 'payload/shared'
// Admin url example
const adminURL = formatAdminURL({
adminRoute: config.routes.admin,
path: `/collections/${slug}/${id}`,
})
// API url example
const apiURL = formatAdminURL({
apiRoute: config.routes.api,
path: `/collections/${slug}/${id}`,
// serverURL, when supplied returns a full url
})
```
If you pass `serverURL` the result will be a full URL. If excluded the
result will be a relative path.
---------
Co-authored-by: Copilot <[email protected]>
This PR adds a base-path test suite, updates the formatAdminURL helper
function.
The helper function allows you to create routes inside the admin panel
that take `basePath` into account when set.
There are a lot of touched files here, most have the same treatment.
## API URL construction changes
Previously we were appending the basePath in the default api
configuration. That is no longer the case and is more predictable to use
the helper function that will generate a correct path/url.
## Admin URL construction changes
The default admin route differed from the default api route, the default
admin route did not prepend the basePath. The reason it did not/should
not, is because `usePathname` from NextJS excludes basePath.
So if we were to prepend it to `routes.admin` and then for example use
`pathname.startsWith(config.routes.admin)` it would always return false
when using a basePath, since `/route` will never start with
`/basePath/route`.
Also, when you do something like `router.push(/adminRoute)` NextJS will
prepend the basePath and push you to `/basePath/adminRoute`. If we
prepended it in the config it would push you to
`/basePath/basePath/adminRoute`.
## Helper function usage
The helper is especially useful for plugin work, this way your plugin
will generate correct urls for users using basePath.
Before we were doing:
```ts
// worked because we prepended basePath to apiRoute
const apiURL = `${serverURL}${apiRoute}/collections/${slug}/${id}`
// never worked since basePath was never prepended to adminRoute
const adminURL = `${serverURL}${adminRoute}/collections/${slug}/${id}`
```
Now we can do:
```ts
import { formatAdminURL } from 'payload/shared'
// Admin url example
const adminURL = formatAdminURL({
adminRoute: config.routes.admin,
path: `/collections/${slug}/${id}`,
})
// API url example
const apiURL = formatAdminURL({
apiRoute: config.routes.api,
path: `/collections/${slug}/${id}`,
// serverURL, when supplied returns a full url
})
```
If you pass `serverURL` the result will be a full URL. If excluded the
result will be a relative path.
---------
Co-authored-by: Copilot <[email protected]>
…op (#16432) ## Summary Setting `trailingSlash: true` in `next.config` previously caused every Payload API and admin request to be 308-redirected to its trailing-slash form. The most visible symptom was admin saves silently failing on first attempt, and middleware-based workarounds were impossible because Next applies the trailing-slash redirect before middleware runs (resulting in `ERR_TOO_MANY_REDIRECTS` for any attempt to skip `/api` and `/admin` from middleware). `withPayload` now mirrors `nextConfig.trailingSlash` into a `NEXT_TRAILING_SLASH` env var, the same way `basePath` is threaded through (PR #14967). `formatAdminURL` appends a trailing slash to the URLs it generates when that env var is set, so the admin and API clients hit the canonical URL directly and Next never emits the redirect. The user's public-page trailing-slash behavior is unchanged, no new `withPayload` options are introduced, and no user-side configuration is required.
This PR adds a base-path test suite, updates the formatAdminURL helper function.
The helper function allows you to create routes inside the admin panel that take
basePathinto account when set.There are a lot of touched files here, most have the same treatment.
API URL construction changes
Previously we were appending the basePath in the default api configuration. That is no longer the case and is more predictable to use the helper function that will generate a correct path/url.
Admin URL construction changes
The default admin route differed from the default api route, the default admin route did not prepend the basePath. The reason it did not/should not, is because
usePathnamefrom NextJS excludes basePath.So if we were to prepend it to
routes.adminand then for example usepathname.startsWith(config.routes.admin)it would always return false when using a basePath, since/routewill never start with/basePath/route.Also, when you do something like
router.push(/adminRoute)NextJS will prepend the basePath and push you to/basePath/adminRoute. If we prepended it in the config it would push you to/basePath/basePath/adminRoute.Helper function usage
The helper is especially useful for plugin work, this way your plugin will generate correct urls for users using basePath.
Before we were doing:
Now we can do:
If you pass
serverURLthe result will be a full URL. If excluded the result will be a relative path.