-
Notifications
You must be signed in to change notification settings - Fork 89
test: add locale assertions for middleware handling #2984
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
/** | ||
* @param {import('next/server').NextRequest} request | ||
*/ | ||
export async function middleware(request) { | ||
const url = request.nextUrl | ||
|
||
|
@@ -17,11 +20,9 @@ export async function middleware(request) { | |
} | ||
|
||
if (url.pathname.startsWith('/link/rewrite-me')) { | ||
const rewriteUrl = new URL( | ||
url.pathname.replace('/link/rewrite-me', '/link/rewrite-target'), | ||
url, | ||
) | ||
return NextResponse.rewrite(rewriteUrl, { | ||
// use NextUrl to rewrite the URL as it does preserve the locale | ||
url.pathname = url.pathname.replace('/link/rewrite-me', '/link/rewrite-target') | ||
return NextResponse.rewrite(url, { | ||
Comment on lines
-20
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: previous rewrite was just losing locale url due to using just This change keeps using |
||
headers: { | ||
'x-middleware-test': 'link-rewrite', | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
export default function Page() { | ||
export default function Page({ locale, locales }) { | ||
return ( | ||
<div data-page="NextResponse.next()#getServerSideProps"> | ||
<h1> | ||
<code>getServerSideProps</code> page | ||
</h1> | ||
<dt>Current locale:</dt> | ||
<dd data-testid="current-locale">{locale ?? 'N/A'}</dd> | ||
<dt>All locales:</dt> | ||
<dd data-testid="all-locales">{locales ? locales.join(',') : 'N/A'}</dd> | ||
</div> | ||
) | ||
} | ||
|
||
export function getServerSideProps() { | ||
/** @type {import('next').GetServerSideProps} */ | ||
export function getServerSideProps({ locale, locales }) { | ||
console.log('NextResponse.next()#getServerSideProps', { locale, locales }) | ||
return { | ||
props: {}, | ||
props: { | ||
locale, | ||
locales, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
export default function Page() { | ||
export default function Page({ locale, locales }) { | ||
return ( | ||
<div data-page="NextResponse.next()#getStaticProps"> | ||
<h1> | ||
<code>getStaticProps</code> page | ||
</h1> | ||
<dt>Current locale:</dt> | ||
<dd data-testid="current-locale">{locale ?? 'N/A'}</dd> | ||
<dt>All locales:</dt> | ||
<dd data-testid="all-locales">{locales ? locales.join(',') : 'N/A'}</dd> | ||
</div> | ||
) | ||
} | ||
|
||
export function getStaticProps() { | ||
/** @type {import('next').GetStaticProps} */ | ||
export function getStaticProps({ locale, locales }) { | ||
console.log('NextResponse.next()#getStaticProps', { locale, locales }) | ||
return { | ||
props: {}, | ||
props: { | ||
locale, | ||
locales, | ||
}, | ||
revalidate: 5, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
export default function Page() { | ||
export default function Page({ locale, locales }) { | ||
return ( | ||
<div data-page="NextResponse.rewrite()#getServerSideProps"> | ||
<h1> | ||
<code>getServerSideProps</code> page | ||
</h1> | ||
<dt>Current locale:</dt> | ||
<dd data-testid="current-locale">{locale ?? 'N/A'}</dd> | ||
<dt>All locales:</dt> | ||
<dd data-testid="all-locales">{locales ? locales.join(',') : 'N/A'}</dd> | ||
</div> | ||
) | ||
} | ||
|
||
export function getServerSideProps() { | ||
/** @type {import('next').GetServerSideProps} */ | ||
export function getServerSideProps({ locale, locales }) { | ||
console.log('NextResponse.rewrite()#getServerSideProps', { locale, locales }) | ||
return { | ||
props: {}, | ||
props: { | ||
locale, | ||
locales, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
export default function Page() { | ||
export default function Page({ locale, locales }) { | ||
return ( | ||
<div data-page="NextResponse.rewrite()#getStaticProps"> | ||
<h1> | ||
<code>getStaticProps</code> page | ||
</h1> | ||
<dt>Current locale:</dt> | ||
<dd data-testid="current-locale">{locale ?? 'N/A'}</dd> | ||
<dt>All locales:</dt> | ||
<dd data-testid="all-locales">{locales ? locales.join(',') : 'N/A'}</dd> | ||
</div> | ||
) | ||
} | ||
|
||
export function getStaticProps() { | ||
/** @type {import('next').GetStaticProps} */ | ||
export function getStaticProps({ locale, locales }) { | ||
console.log('NextResponse.rewrite()#getStaticProps', { locale, locales }) | ||
return { | ||
props: {}, | ||
props: { | ||
locale, | ||
locales, | ||
}, | ||
revalidate: 5, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only
getServerSideProps
andgetStaticProps
even have access to locale and "fully static page" cases just don't have access to it so asserting locale is optional check enabled to cases when locale is even available to check