-
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.
test: add new middleware i18n fixture for skipping normalization
- Loading branch information
Showing
9 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
tests/fixtures/middleware-i18n-skip-normalize/middleware.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,91 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export async function middleware(request) { | ||
const url = request.nextUrl | ||
|
||
// this is needed for tests to get the BUILD_ID | ||
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) { | ||
return NextResponse.next() | ||
} | ||
|
||
if (url.pathname === '/old-home') { | ||
if (url.searchParams.get('override') === 'external') { | ||
return Response.redirect('https://example.vercel.sh') | ||
} else { | ||
url.pathname = '/new-home' | ||
return Response.redirect(url) | ||
} | ||
} | ||
|
||
if (url.searchParams.get('foo') === 'bar') { | ||
url.pathname = '/new-home' | ||
url.searchParams.delete('foo') | ||
return Response.redirect(url) | ||
} | ||
|
||
// Chained redirects | ||
if (url.pathname === '/redirect-me-alot') { | ||
url.pathname = '/redirect-me-alot-2' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-2') { | ||
url.pathname = '/redirect-me-alot-3' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-3') { | ||
url.pathname = '/redirect-me-alot-4' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-4') { | ||
url.pathname = '/redirect-me-alot-5' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-5') { | ||
url.pathname = '/redirect-me-alot-6' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-6') { | ||
url.pathname = '/redirect-me-alot-7' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-me-alot-7') { | ||
url.pathname = '/new-home' | ||
return Response.redirect(url) | ||
} | ||
|
||
// Infinite loop | ||
if (url.pathname === '/infinite-loop') { | ||
url.pathname = '/infinite-loop-1' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/infinite-loop-1') { | ||
url.pathname = '/infinite-loop' | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/to') { | ||
url.pathname = url.searchParams.get('pathname') | ||
url.searchParams.delete('pathname') | ||
return Response.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/with-fragment') { | ||
console.log(String(new URL('/new-home#fragment', url))) | ||
return Response.redirect(new URL('/new-home#fragment', url)) | ||
} | ||
|
||
if (url.pathname.includes('/json')) { | ||
return NextResponse.json({ | ||
requestUrlPathname: new URL(request.url).pathname, | ||
nextUrlPathname: request.nextUrl.pathname, | ||
nextUrlLocale: request.nextUrl.locale, | ||
}) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/fixtures/middleware-i18n-skip-normalize/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,24 @@ | ||
module.exports = { | ||
output: 'standalone', | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
i18n: { | ||
locales: ['en', 'fr', 'nl', 'es'], | ||
defaultLocale: 'en', | ||
}, | ||
skipMiddlewareUrlNormalize: true, | ||
experimental: { | ||
clientRouterFilter: true, | ||
clientRouterFilterRedirects: true, | ||
}, | ||
redirects() { | ||
return [ | ||
{ | ||
source: '/to-new', | ||
destination: '/dynamic/new', | ||
permanent: false, | ||
}, | ||
] | ||
}, | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/fixtures/middleware-i18n-skip-normalize/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,18 @@ | ||
{ | ||
"name": "middleware-pages", | ||
"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/react": "18.2.47" | ||
} | ||
} |
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,6 @@ | ||
export default function App({ Component, pageProps }) { | ||
if (!pageProps || typeof pageProps !== 'object') { | ||
throw new Error(`Invariant: received invalid pageProps in _app, received ${pageProps}`) | ||
} | ||
return <Component {...pageProps} /> | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.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,3 @@ | ||
export default function handler(req, res) { | ||
res.send('ok') | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/middleware-i18n-skip-normalize/pages/dynamic/[slug].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,15 @@ | ||
export default function Account({ slug }) { | ||
return ( | ||
<p id="dynamic" className="title"> | ||
Welcome to a /dynamic/[slug]: {slug} | ||
</p> | ||
) | ||
} | ||
|
||
export function getServerSideProps({ params }) { | ||
return { | ||
props: { | ||
slug: params.slug, | ||
}, | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/fixtures/middleware-i18n-skip-normalize/pages/index.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,35 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Home() { | ||
return ( | ||
<div> | ||
<p className="title">Home Page</p> | ||
<Link href="/old-home" id="old-home"> | ||
Redirect me to a new version of a page | ||
</Link> | ||
<div /> | ||
<Link href="/old-home?override=external" id="old-home-external"> | ||
Redirect me to an external site | ||
</Link> | ||
<div /> | ||
<Link href="/blank-page?foo=bar">Redirect me with URL params intact</Link> | ||
<div /> | ||
<Link href="/redirect-to-google">Redirect me to Google (with no body response)</Link> | ||
<div /> | ||
<Link href="/redirect-to-google">Redirect me to Google (with no stream response)</Link> | ||
<div /> | ||
<Link href="/redirect-me-alot">Redirect me alot (chained requests)</Link> | ||
<div /> | ||
<Link href="/infinite-loop">Redirect me alot (infinite loop)</Link> | ||
<div /> | ||
<Link href="/to?pathname=/api/ok" locale="nl" id="link-to-api-with-locale"> | ||
Redirect me to api with locale | ||
</Link> | ||
<div /> | ||
<Link href="/to?pathname=/old-home" id="link-to-to-old-home"> | ||
Redirect me to a redirecting page of new version of page | ||
</Link> | ||
<div /> | ||
</div> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/middleware-i18n-skip-normalize/pages/new-home.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,7 @@ | ||
export default function Account() { | ||
return ( | ||
<p id="new-home-title" className="title"> | ||
Welcome to a new page | ||
</p> | ||
) | ||
} |
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