Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle edge case for localePrefix: 'as-needed', domains & pathnames #1594

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions packages/next-intl/src/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2704,6 +2704,27 @@ describe('domain-based routing', () => {
} satisfies Pathnames<ReadonlyArray<'en' | 'fr'>>
});

it('redirects an unprefixed pathname from the fr locale on the en domain', () => {
middlewareWithPathnames(
createMockRequest('/a-propos', 'fr', 'http://en.example.com')
);
expect(MockedNextResponse.redirect).toHaveBeenCalled();
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
'http://en.example.com/about'
);
});
Copy link
Owner Author

Choose a reason for hiding this comment

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

@markomitranic I've tried to recreate your case here.

See also the config above:

const middlewareWithPathnames = createMiddleware({
defaultLocale: 'en',
locales: ['en', 'fr'],
localePrefix: 'as-needed',
domains: [
{defaultLocale: 'en', domain: 'en.example.com', locales: ['en']},
{
defaultLocale: 'en',
domain: 'ca.example.com',
locales: ['en', 'fr']
},
{defaultLocale: 'fr', domain: 'fr.example.com', locales: ['fr']}
],
pathnames: {
'/': '/',
'/about': {
en: '/about',
fr: '/a-propos'
},
'/users': {
en: '/users',
fr: '/utilisateurs'
},
'/users/[userId]': {
en: '/users/[userId]',
fr: '/utilisateurs/[userId]'
},
'/news/[articleSlug]-[articleId]': {
en: '/news/[articleSlug]-[articleId]',
fr: '/nouvelles/[articleSlug]-[articleId]'
},
'/products/[...slug]': {
en: '/products/[...slug]',
fr: '/produits/[...slug]'
},
'/categories/[[...slug]]': {
en: '/categories/[[...slug]]',
fr: '/categories/[[...slug]]'
},
'/internal': '/external',
'/internal/foo/bar': {
en: '/external-en/foo/bar',
fr: '/external-fr/foo/bar'
},
'/internal/[id]': {
en: '/external-en/[id]',
fr: '/external-fr/[id]'
},
'/internal/[...slug]': {
en: '/external-en/[...slug]',
fr: '/external-fr/[...slug]'
}
} satisfies Pathnames<ReadonlyArray<'en' | 'fr'>>
});

This seems to be working for me. A redirect to /about is currently added here for compatibility, e.g. when the user switches the domain without changing pathnames.

Can you help me find a failing test?


it('redirects a prefixed pathname from the fr locale on the en domain', () => {
middlewareWithPathnames(
createMockRequest('/fr/a-propos', 'fr', 'http://en.example.com')
);
expect(MockedNextResponse.redirect).toHaveBeenCalled();
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
// (will get another redirect to /a-propos on this domain)
'http://fr.example.com/fr/a-propos'
);
});

it('serves requests for the default locale at the root', () => {
middlewareWithPathnames(
createMockRequest('/', 'en', 'http://en.example.com')
Expand Down
Loading