You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, Next.js removes trailing slashes with the trailingSlash option. However, this doesn't play well with the custom redirects I have in my middleware.
I have the following middleware.ts (simplified for brevity):
The issue here is that redirect chains are bad for SEO. For this reason, I want to disable the default Next.js functionality and implement it myself in the middleware in a way that works with my other redirects. How can I do this?
I tried changing the trailingSlash setting to true, but that just makes non-trailing slash paths redirect to their trailing slash counterparts, which is worse. And setting it to undefined just leaves the default behavior.
Additional information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 23.6.0: Thu Sep 12 23:34:49 PDT 2024; root:xnu-10063.141.1.701.1~1/RELEASE_X86_64
Available memory (MB): 32768
Available CPU cores: 16
Binaries:
Node: 20.17.0
npm: 10.8.2
Yarn: N/A
pnpm: 9.9.0
Relevant Packages:
next: 15.0.1
eslint-config-next: 15.0.1
react: 19.0.0-rc-cae764ce-20241025
react-dom: 19.0.0-rc-cae764ce-20241025
typescript: 5.6.3
Next.js Config:
output: standalone
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
By default, Next.js removes trailing slashes with the
trailingSlash
option. However, this doesn't play well with the custom redirects I have in my middleware.I have the following
middleware.ts
(simplified for brevity):…and when a user navigates to
/foo/bar/
, they are redirected twice:/foo/bar/
→/foo/bar
(308
internal Next.js redirect)/foo/bar
→/foo
(307
custom middleware redirect)The issue here is that redirect chains are bad for SEO. For this reason, I want to disable the default Next.js functionality and implement it myself in the middleware in a way that works with my other redirects. How can I do this?
I tried changing the
trailingSlash
setting totrue
, but that just makes non-trailing slash paths redirect to their trailing slash counterparts, which is worse. And setting it toundefined
just leaves the default behavior.Additional information
Beta Was this translation helpful? Give feedback.
All reactions