Skip to content

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions tests/e2e/edge-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ test.describe('json data', () => {
describeLabel: 'NextResponse.next() -> getServerSideProps page',
selector: 'NextResponse.next()#getServerSideProps',
jsonPathMatcher: '/link/next-getserversideprops.json',
assertLocale: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only getServerSideProps and getStaticProps 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

},
{
describeLabel: 'NextResponse.next() -> getStaticProps page',
selector: 'NextResponse.next()#getStaticProps',
jsonPathMatcher: '/link/next-getstaticprops.json',
assertLocale: true,
},
{
describeLabel: 'NextResponse.next() -> fully static page',
Expand All @@ -78,11 +80,13 @@ test.describe('json data', () => {
describeLabel: 'NextResponse.rewrite() -> getServerSideProps page',
selector: 'NextResponse.rewrite()#getServerSideProps',
jsonPathMatcher: '/link/rewrite-me-getserversideprops.json',
assertLocale: true,
},
{
describeLabel: 'NextResponse.rewrite() -> getStaticProps page',
selector: 'NextResponse.rewrite()#getStaticProps',
jsonPathMatcher: '/link/rewrite-me-getstaticprops.json',
assertLocale: true,
},
]

Expand Down Expand Up @@ -144,10 +148,22 @@ test.describe('json data', () => {
test.describe('with 18n', () => {
for (const testConfig of testConfigs) {
test.describe(testConfig.describeLabel, () => {
for (const { localeLabel, pageWithLinksPathname } of [
{ localeLabel: 'implicit default locale', pageWithLinksPathname: '/link' },
{ localeLabel: 'explicit default locale', pageWithLinksPathname: '/en/link' },
{ localeLabel: 'explicit non-default locale', pageWithLinksPathname: '/fr/link' },
for (const { localeLabel, pageWithLinksPathname, expectedLocale } of [
{
localeLabel: 'implicit default locale',
pageWithLinksPathname: '/link',
expectedLocale: 'en',
},
{
localeLabel: 'explicit default locale',
pageWithLinksPathname: '/en/link',
expectedLocale: 'en',
},
{
localeLabel: 'explicit non-default locale',
pageWithLinksPathname: '/fr/link',
expectedLocale: 'fr',
},
]) {
test.describe(localeLabel, () => {
test('json data fetch', async ({ middlewareI18n, page }) => {
Expand Down Expand Up @@ -189,6 +205,10 @@ test.describe('json data', () => {

// we expect client navigation to work without browser reload
expect(browserNavigationWorked).toBe(true)

if (testConfig.assertLocale) {
await expect(page.getByTestId('current-locale')).toHaveText(expectedLocale)
}
})
})
}
Expand Down
11 changes: 6 additions & 5 deletions tests/fixtures/middleware-i18n/middleware.js
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

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: previous rewrite was just losing locale url due to using just NextURL.pathname to construct new URL effectively losing locale context in the process.

This change keeps using NextURL and just mutate pathname portion of it which preserve locale context and resulting in seemingly working as expected rewrites

headers: {
'x-middleware-test': 'link-rewrite',
},
Expand Down
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,
}
}
Loading