-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: narrow down middleware i18n locale matcher to concrete locales (#…
…2768) * test: add test cases to i18n middleware with exclusions * fix: narrow down middleware i18n locale matcher to concrete locales
- Loading branch information
Showing
11 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
36 changes: 36 additions & 0 deletions
36
tests/fixtures/middleware-i18n-excluded-paths/middleware.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
|
||
export async function middleware(request: NextRequest) { | ||
const url = request.nextUrl | ||
|
||
// if path ends with /json we create response in middleware, otherwise we pass it through | ||
// to next server to get page or api response from it | ||
const response = url.pathname.includes('/json') | ||
? NextResponse.json({ | ||
requestUrlPathname: new URL(request.url).pathname, | ||
nextUrlPathname: request.nextUrl.pathname, | ||
nextUrlLocale: request.nextUrl.locale, | ||
}) | ||
: NextResponse.next() | ||
|
||
response.headers.set('x-test-used-middleware', 'true') | ||
|
||
return response | ||
} | ||
|
||
// matcher copied from example in https://nextjs.org/docs/pages/building-your-application/routing/middleware#matcher | ||
// with `excluded` segment added to exclusion | ||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - api (API routes) | ||
* - excluded (for testing localized routes and not just API routes) | ||
* - _next/static (static files) | ||
* - _next/image (image optimization files) | ||
* - favicon.ico, sitemap.xml, robots.txt (metadata files) | ||
*/ | ||
'/((?!api|excluded|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)', | ||
], | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/middleware-i18n-excluded-paths/next.config.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
output: 'standalone', | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
i18n: { | ||
locales: ['en', 'fr'], | ||
defaultLocale: 'en', | ||
}, | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/fixtures/middleware-i18n-excluded-paths/package.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "middleware-i18n-excluded-paths", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"postinstall": "next build", | ||
"dev": "next dev", | ||
"build": "next build" | ||
}, | ||
"dependencies": { | ||
"next": "latest", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.12", | ||
"@types/react": "18.2.47", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/fixtures/middleware-i18n-excluded-paths/pages/[[...catchall]].tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { GetStaticPaths, GetStaticProps } from 'next' | ||
|
||
export default function CatchAll({ params, locale }) { | ||
return <pre>{JSON.stringify({ params, locale }, null, 2)}</pre> | ||
} | ||
|
||
export const getStaticPaths: GetStaticPaths = () => { | ||
return { | ||
paths: [], | ||
fallback: 'blocking', | ||
} | ||
} | ||
|
||
export const getStaticProps: GetStaticProps = ({ params, locale }) => { | ||
return { | ||
props: { | ||
params, | ||
locale, | ||
}, | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/fixtures/middleware-i18n-excluded-paths/pages/api/[[...catchall]].ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
type ResponseData = { | ||
params: { | ||
catchall?: string[] | ||
} | ||
} | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) { | ||
res.status(200).json({ params: req.query }) | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/fixtures/middleware-i18n-excluded-paths/tsconfig.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains 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
This file contains 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