Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing apiRoute in request to /api/payload-preferences/locale #10617

Closed
alexdeia opened this issue Jan 16, 2025 · 2 comments · Fixed by #10618
Closed

Missing apiRoute in request to /api/payload-preferences/locale #10617

alexdeia opened this issue Jan 16, 2025 · 2 comments · Fixed by #10618
Assignees

Comments

@alexdeia
Copy link

Describe the Bug

Image

If you use PayloadCMS 3 in subfolder, for example: https://domain.com/subfolder/admin, you get an error like

Request URL:
http://domain.com/api/payload-preferences/locale
Request Method:
GET
Status Code:
404 Not Found

but another requests work fine.

Request URL:
http://domain.com/subfolder/api/users/me
Request Method:
GET
Status Code:
200 OK

I found this code where apiRoute configs were missing.

const fetchPreferences = async <T extends Record<string, unknown> | string>(
  key: string,
): Promise<{ id: string; value: T }> =>
  await fetch(`/api/payload-preferences/${key}`, {
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json',
    },
    method: 'GET',
  })?.then((res) => res.json() as Promise<{ id: string; value: T }>)

Working example from another functions:

const request = await requests.get(`${serverURL}${api}/payload-preferences/${key}`, {
          async function refresh() {
            try {
              const request = await requests.post(
                `${serverURL}${apiRoute}/${userSlug}/refresh-token?refresh`,
                {
                  headers: {
                    'Accept-Language': i18n.language,
                  },
                },
              )
      const res = await fetch(`${apiRoute}/payload-preferences${stringifiedQuery}`, {
        credentials: 'include',
        headers: {
          'Content-Type': 'application/json',
        },
        method: 'DELETE',
      })

Link to the code that reproduces this issue

https://github.com/alexdeia/payload-api-route-bug

Reproduction Steps

check next.config.mjs:

const nextConfig = {
  // Your Next.js config here
  basePath: '/subfolder'
}

check payload.config.ts:

  localization: {
    locales: ['de', 'en'],
    defaultLocale: 'en',
    fallback: true
  },
  1. open the reproduction repo
  2. install dependencies
  3. set your empty database in .env
  4. pnpm dev
  5. open http://localhost:3000/subfolder/admin
  6. open browser dev-tools and look on the error:
index.js:4704        GET http://localhost:3000/api/payload-preferences/locale 404 (Not Found)

Just in case, I checked - these settings do not affect the payload-preferences URL

  routes: {
    // admin: '/subfolder/admin',
    api: '/subfolder/api',
  },

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

> cross-env NODE_OPTIONS=--no-deprecation payload "info"


Binaries:
  Node: 22.11.0
  npm: 10.9.0
  Yarn: 1.22.19
  pnpm: 9.15.4
Relevant Packages:
  payload: 3.17.1
  next: 15.1.0
  @payloadcms/db-postgres: 3.17.1
  @payloadcms/email-nodemailer: 3.17.1
  @payloadcms/graphql: 3.17.1
  @payloadcms/next/utilities: 3.17.1
  @payloadcms/payload-cloud: 3.17.1
  @payloadcms/richtext-lexical: 3.17.1
  @payloadcms/translations: 3.17.1
  @payloadcms/ui/shared: 3.17.1
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
@alexdeia alexdeia added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 16, 2025
@r1tsuu r1tsuu self-assigned this Jan 16, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jan 16, 2025
@alexdeia
Copy link
Author

@r1tsuu same problem with Relationship field type:

https://github.com/payloadcms/payload/blob/main/packages/ui/src/fields/Relationship/index.tsx#L214 and other places.

            const response = await fetch(`${serverURL}${api}/${relation}`, {
              body: qs.stringify(query),
              credentials: 'include',
              headers: {
                'Accept-Language': i18n.language,
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-HTTP-Method-Override': 'GET',
              },
              method: 'POST',
            })
Image

@r1tsuu
Copy link
Member

r1tsuu commented Jan 17, 2025

I think this request works for me:

Image

I set basePath to /payload.
Now I see it was fixed #10562 with but not released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants