Skip to content

Commit a938746

Browse files
Merge pull request #18206 from mozilla/FXA-10440
feat(next): Redirect next app to default locale for invalid locales Because: * The next app uses routing to handle locale indication, but has no rerouting fallback functionality for improper locales. This commit: * Adds functionality to the Next.js middleware to catch incoming requests to next app on routes other than: api|_next/static|_next/image|favicon.ico|__heartbeat__|__lbheartbeat__|__version__ Closes FXA-10440
2 parents a53e9a5 + 6e2f63a commit a938746

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

apps/payments/next/middleware.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
import { NextRequest, NextResponse } from 'next/server';
5+
import { supportedLanguages, DEFAULT_LOCALE } from '@fxa/shared/l10n';
56

67
export function middleware(request: NextRequest) {
8+
// Handle locale fallback
9+
const localeSegment = request.nextUrl.pathname.split('/')[1];
10+
if (localeSegment && !supportedLanguages.includes(localeSegment)) {
11+
const newPathname = `/${DEFAULT_LOCALE}${request.nextUrl.pathname.substring(
12+
localeSegment.length + 1
13+
)}`;
14+
return NextResponse.redirect(new URL(newPathname, request.url));
15+
}
16+
717
// Read env vars directly from process.env
818
// As of 05-15-2024 its not possible to use app/config in middleware
919
const accountsStaticCdn = process.env.CSP__ACCOUNTS_STATIC_CDN;
@@ -74,7 +84,8 @@ export const config = {
7484
* - favicon.ico (favicon file)
7585
*/
7686
{
77-
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
87+
source:
88+
'/((?!api|error|_next/static|_next/image|favicon.ico|__heartbeat__|__lbheartbeat__|__version__).*)',
7889
missing: [
7990
{ type: 'header', key: 'next-router-prefetch' },
8091
{ type: 'header', key: 'purpose', value: 'prefetch' },

0 commit comments

Comments
 (0)