Skip to content

File URLs get a trailing slash when Next.js trailingSlash is enabled (404 on every upload) #19292

Open

Description

@guilhermemarketing

Describe the Bug

Since 3.90 (#18207), formatAdminURL appends a trailing slash to every URL it builds when Next.js runs with trailingSlash: true (NEXT_TRAILING_SLASH=true). generateFilePathOrURL builds upload URLs with it, so every file URL now ends with a slash:

url:          /api/media/file/image.png/
thumbnailURL: /api/media/file/image-300x300.png/

A file URL points at an object, not a page route. With the slash, storage adapters and CDNs resolve a different (missing) key, so every image rendered from doc.url returns 404. In our production site (Next.js on Cloudflare Workers, @payloadcms/storage-r2) every image broke right after upgrading from 3.89.0 to 3.92.1 and we had to roll back.

Admin and API routes are fine with the slash; only file URLs are affected. The same code is on main, where a ?original=true query would also end up as /image.png/?original=true.

Link to the code that reproduces this issue

https://github.com/guilhermemarketing/payload/blob/fix/file-url-trailing-slash/packages/payload/src/uploads/generateFilePathOrURL.spec.ts

Reproduction Steps

  1. Set trailingSlash: true in next.config.js (wrapped with withPayload).
  2. Upload an image to any upload collection.
  3. Read the document: url and every sizes.*.url end with /.
  4. Request that URL: 404 with a storage adapter (R2 in our case); with local storage the URL differs from the stored file path as well.

Unit-level reproduction:

process.env.NEXT_TRAILING_SLASH = 'true'
generateFilePathOrURL({ collectionSlug: 'media', config, filename: 'image.png', relative: true, urlOrPath: undefined })
// => '/api/media/file/image.png/'   (expected '/api/media/file/image.png')

Which area(s) are affected?

area: core, area: uploads

Environment Info

payload: 3.92.1 (also 3.90.x, 3.91.x; 3.89.0 is not affected)
@payloadcms/next: 3.92.1
@payloadcms/storage-r2: 3.92.1
next: 16.3.3
Node.js: 24.22.1
Database: @payloadcms/db-d1-sqlite 3.92.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPayload OSSarea: corearea: uploads

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions