fix(next): support next.config trailingSlash without /api redirect loop#16432
Draft
JarrodMFlesch wants to merge 3 commits intomainfrom
Draft
fix(next): support next.config trailingSlash without /api redirect loop#16432JarrodMFlesch wants to merge 3 commits intomainfrom
JarrodMFlesch wants to merge 3 commits intomainfrom
Conversation
Setting `trailingSlash: true` in `next.config` was 308-redirecting every Payload API and admin request to its trailing-slash form, breaking admin saves and rendering middleware workarounds ineffective (Next applies the redirect before middleware runs). `withPayload` now mirrors `nextConfig.trailingSlash` into the `NEXT_TRAILING_SLASH` env var the same way `basePath` is threaded. `formatAdminURL` appends a trailing slash to the URLs it generates when that env var is set, so admin/API clients hit the canonical URL directly and Next never emits the redirect. Public-page behavior is unchanged.
Contributor
📦 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.
|
Adds a `test/trailing-slash` config that runs Next.js with `trailingSlash: true` to exercise the bug fix end-to-end. Verifies that admin form actions and API mutation requests include the trailing slash directly so Next.js never emits a 308 normalization redirect, which was the source of the original silent admin-save failure.
Two follow-ups to make `trailingSlash: true` actually round-trip end-to-end: - `handleEndpoints` now strips a single trailing slash from both the incoming pathname and the API base path before string-slicing, so `/api/users/login/` (the canonical form under trailingSlash:true) reaches the same endpoint match as `/api/users/login`. Without this the slug shift skipped the collection segment and produced 404s. - `Root/index.tsx` normalizes `currentRoute` and `createFirstUserRoute` before comparing them to the raw `adminRoute` config value, so the dashboard view is selected when navigating to `/admin/`. Also extends `test/trailing-slash` with the Next.js app boilerplate needed for it to boot under its own next.config (the test directory gets its own admin/api route handlers when it overrides next.config.mjs) and broadens the e2e to walk dashboard, list, edit, account and verify no /api response is a 3xx.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Setting
trailingSlash: trueinnext.configpreviously 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 inERR_TOO_MANY_REDIRECTSfor any attempt to skip/apiand/adminfrom middleware).withPayloadnow mirrorsnextConfig.trailingSlashinto aNEXT_TRAILING_SLASHenv var, the same waybasePathis threaded through (PR #14967).formatAdminURLappends 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 newwithPayloadoptions are introduced, and no user-side configuration is required.