fix: skip intl middleware for paths already containing a locale prefix#183
Open
shukiv wants to merge 2 commits intobulwarkmail:mainfrom
Open
fix: skip intl middleware for paths already containing a locale prefix#183shukiv wants to merge 2 commits intobulwarkmail:mainfrom
shukiv wants to merge 2 commits intobulwarkmail:mainfrom
Conversation
added 2 commits
April 12, 2026 03:43
Allow the next-intl localePrefix mode to be set via environment variable at build time, defaulting to the existing 'never' behavior. This is useful when proxying Bulwark under a sub-path (where 'never' can trigger rewrite loops) or when users prefer URL-embedded locales (/en/settings vs /settings). Usage: NEXT_PUBLIC_LOCALE_PREFIX=always npm run build Accepted values: 'never' (default), 'always', 'as-needed'.
When localePrefix is 'always' (or 'as-needed' with a non-default locale), paths like /en/settings already have the locale in the URL. Running them through the next-intl middleware a second time can trigger rewrite loops, especially when combined with a proxy basePath where the middleware's detection of the 'current' path conflicts with the rewritten one. Skip the intl middleware in this case — the path is already in the canonical locale-prefixed form and no further rewriting is needed. This makes NEXT_PUBLIC_LOCALE_PREFIX=always reliable for sub-path deployments.
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
Skip the next-intl middleware in
proxy.tswhen the request path already starts with a locale prefix (e.g./en/settings). Re-processing already-prefixed paths can trigger rewrite loops, especially when combined with a proxybasePath.Motivation
When
localePrefixis'always'(or'as-needed'with a non-default locale), URLs take the form/en/settings. The intl middleware's job is to add or normalize the prefix — but if it's already there, the middleware should be a no-op.In practice, running the middleware on already-prefixed paths causes issues when:
basePath(feat: support configurable basePath via NEXT_PUBLIC_BASE_PATH #181) — the middleware's view of the "current" path can conflict with the rewritten one, producing 307 rewrite loopsX-Forwarded-*headers don't perfectly match what next-intl expectsThe existing code already skips the middleware for
/adminroutes for a similar reason (admin has its own layout). This PR extends that to any path that's already locale-prefixed.Changes
proxy.ts: addhasLocalePrefixcheck alongside the existingisAdminRoutecheck; skip intl middleware when either is trueTest plan
localePrefix: 'never') — verify no change in behaviorNEXT_PUBLIC_LOCALE_PREFIX=always(feat: configurable localePrefix via NEXT_PUBLIC_LOCALE_PREFIX #182) — verify pages load at/en/,/fr/, etc. without rewrite loopsNEXT_PUBLIC_LOCALE_PREFIX=always+NEXT_PUBLIC_BASE_PATH=/webmail(feat: support configurable basePath via NEXT_PUBLIC_BASE_PATH #181) — verify/webmail/en/settingsworksNotes
Depends on #182 (configurable localePrefix) for the full use case, but is compatible as a standalone change since
routing.localesis always available.