SIGN IN SIGN UP
payloadcms / payload UNCLAIMED

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.

41461 0 0 TypeScript
fix: basePath not working properly with admin routes (#14967) 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 <175728472+Copilot@users.noreply.github.com>
2025-12-19 11:13:21 -05:00
export const BASE_PATH = '/cms'