From 21df1aa76c933326037e3debebbdbe657991d47a Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 14 Jun 2024 13:37:40 +0100 Subject: [PATCH] test: add new middleware i18n fixture for skipping normalization --- .../middleware.js | 91 +++++++++++++++++++ .../next.config.js | 24 +++++ .../package.json | 18 ++++ .../pages/_app.js | 6 ++ .../pages/api/ok.js | 3 + .../pages/dynamic/[slug].js | 15 +++ .../pages/index.js | 35 +++++++ .../pages/new-home.js | 7 ++ tests/fixtures/middleware-i18n/middleware.js | 6 +- 9 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/middleware.js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/next.config.js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/package.json create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/pages/_app.js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/pages/dynamic/[slug].js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/pages/index.js create mode 100644 tests/fixtures/middleware-i18n-skip-normalize/pages/new-home.js diff --git a/tests/fixtures/middleware-i18n-skip-normalize/middleware.js b/tests/fixtures/middleware-i18n-skip-normalize/middleware.js new file mode 100644 index 0000000000..24517d72de --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/middleware.js @@ -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, + }) + } +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/next.config.js b/tests/fixtures/middleware-i18n-skip-normalize/next.config.js new file mode 100644 index 0000000000..eadf9cf8fb --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/next.config.js @@ -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, + }, + ] + }, +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/package.json b/tests/fixtures/middleware-i18n-skip-normalize/package.json new file mode 100644 index 0000000000..5708c88b50 --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/package.json @@ -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" + } +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/pages/_app.js b/tests/fixtures/middleware-i18n-skip-normalize/pages/_app.js new file mode 100644 index 0000000000..4f7709a5bc --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/pages/_app.js @@ -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 +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.js b/tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.js new file mode 100644 index 0000000000..fb91e8b611 --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/pages/api/ok.js @@ -0,0 +1,3 @@ +export default function handler(req, res) { + res.send('ok') +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/pages/dynamic/[slug].js b/tests/fixtures/middleware-i18n-skip-normalize/pages/dynamic/[slug].js new file mode 100644 index 0000000000..61131835fa --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/pages/dynamic/[slug].js @@ -0,0 +1,15 @@ +export default function Account({ slug }) { + return ( +

+ Welcome to a /dynamic/[slug]: {slug} +

+ ) +} + +export function getServerSideProps({ params }) { + return { + props: { + slug: params.slug, + }, + } +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/pages/index.js b/tests/fixtures/middleware-i18n-skip-normalize/pages/index.js new file mode 100644 index 0000000000..362aa85570 --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/pages/index.js @@ -0,0 +1,35 @@ +import Link from 'next/link' + +export default function Home() { + return ( +
+

Home Page

+ + Redirect me to a new version of a page + +
+ + Redirect me to an external site + +
+ Redirect me with URL params intact +
+ Redirect me to Google (with no body response) +
+ Redirect me to Google (with no stream response) +
+ Redirect me alot (chained requests) +
+ Redirect me alot (infinite loop) +
+ + Redirect me to api with locale + +
+ + Redirect me to a redirecting page of new version of page + +
+
+ ) +} diff --git a/tests/fixtures/middleware-i18n-skip-normalize/pages/new-home.js b/tests/fixtures/middleware-i18n-skip-normalize/pages/new-home.js new file mode 100644 index 0000000000..313011766e --- /dev/null +++ b/tests/fixtures/middleware-i18n-skip-normalize/pages/new-home.js @@ -0,0 +1,7 @@ +export default function Account() { + return ( +

+ Welcome to a new page +

+ ) +} diff --git a/tests/fixtures/middleware-i18n/middleware.js b/tests/fixtures/middleware-i18n/middleware.js index ef6fa57296..24517d72de 100644 --- a/tests/fixtures/middleware-i18n/middleware.js +++ b/tests/fixtures/middleware-i18n/middleware.js @@ -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, + }) } }