Skip to content

Commit

Permalink
test: add new middleware i18n fixture for skipping normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
orinokai committed Jun 14, 2024
1 parent 2326319 commit 21df1aa
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 1 deletion.
91 changes: 91 additions & 0 deletions tests/fixtures/middleware-i18n-skip-normalize/middleware.js
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 tests/fixtures/middleware-i18n-skip-normalize/next.config.js
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 tests/fixtures/middleware-i18n-skip-normalize/package.json
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"
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/middleware-i18n-skip-normalize/pages/_app.js
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 tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.send('ok')
}
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 tests/fixtures/middleware-i18n-skip-normalize/pages/index.js
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>
)
}
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>
)
}
6 changes: 5 additions & 1 deletion tests/fixtures/middleware-i18n/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export async function middleware(request) {
}

if (url.pathname.includes('/json')) {
return NextResponse.json({ url: request.nextUrl.href, locale: request.nextUrl.locale })
return NextResponse.json({
requestUrlPathname: new URL(request.url).pathname,
nextUrlPathname: request.nextUrl.pathname,
nextUrlLocale: request.nextUrl.locale,
})
}
}

0 comments on commit 21df1aa

Please sign in to comment.