rsnext/test/e2e/app-dir/i18n-hybrid/i18n-hybrid.test.js
Wyatt Johnson b8d785d286
Locale fixes for App Directory (#47429)
Fixes issue with app directory that caused problems with `i18n` was
enabled. Previously, when you accessed a URL like `/blog/first-issue`,
you would expect it to hit the app route at `/app/blog/[slug]/page.tsx`,
but instead it internally attempted to access
`/app/{defaultLocale}/blog/[slug]/page.tsx` (where `{defaultLocale}` is
set in `next.config.js` via `i18n.defaultLocale`). This is because while
the path did not assume the locale, the domain/default was enough to
suggest it, causing the mismatch.

This adds a new internal parameter for tracking this _default_
assignment so it can be handled by the matcher correctly.

Fixes #46814
Fixes #46841
fix NEXT-777 ([link](https://linear.app/vercel/issue/NEXT-777))
fix NEXT-834 ([link](https://linear.app/vercel/issue/NEXT-834))

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-03-24 13:06:27 +01:00

77 lines
1.7 KiB
JavaScript

// @ts-check
// @ts-ignore
import { createNextDescribe } from 'e2e-utils'
import cheerio from 'cheerio'
const { i18n } = require('./next.config')
const urls = [
// Include the app page without a locale.
{
pathname: '/blog/first-post',
expected: {
pathname: '/blog/first-post',
page: '/app/blog/[slug]/page.js',
},
},
// Include the app pages with locales (should not resolve).
...i18n.locales.map((locale) => ({
pathname: `/${locale}/blog/first-post`,
expected: null,
})),
// Include the pages page without a locale (should default to the default
// locale).
{
pathname: '/about',
expected: {
pathname: `/${i18n.defaultLocale}/about`,
page: '/pages/about.js',
},
},
// Include the locale prefixed urls for the pages page (should resolve).
...i18n.locales.map((locale) => ({
pathname: `/${locale}/about`,
expected: {
pathname: `/${locale}/about`,
page: '/pages/about.js',
},
})),
]
createNextDescribe(
'i18n-hybrid',
{
files: __dirname,
},
({ next }) => {
it.each(urls.filter((url) => !url.expected))(
'does not resolve $pathname',
async (url) => {
const res = await next.fetch(url.pathname, {
redirect: 'manual',
})
expect(res.status).toBe(404)
}
)
it.each(urls.filter((url) => url.expected))(
'does resolve $pathname',
async (url) => {
const res = await next.fetch(url.pathname, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
const debug = JSON.parse($('#debug').text())
expect(debug).toEqual(url.expected)
}
)
}
)