Skip to content

Commit

Permalink
Add LocalePrefix never option
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-arkenaar committed Jul 7, 2023
1 parent ff327ea commit 30f885a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type LocalePrefix = 'as-needed' | 'always';
type LocalePrefix = 'as-needed' | 'always' | 'never';

type RoutingBaseConfig = {
/** A list of all locales that are supported. */
Expand Down
16 changes: 10 additions & 6 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ export default function createMiddleware(config: MiddlewareConfig) {
}

if (
hasMatchedDefaultLocale &&
configWithDefaults.localePrefix === 'as-needed'
configWithDefaults.localePrefix === 'never' ||
(hasMatchedDefaultLocale &&
configWithDefaults.localePrefix === 'as-needed')
) {
response = rewrite(pathWithSearch);
} else {
Expand All @@ -140,7 +141,9 @@ export default function createMiddleware(config: MiddlewareConfig) {
if (hasLocalePrefix) {
const basePath = pathWithSearch.replace(`/${pathLocale}`, '') || '/';

if (pathLocale === locale) {
if (configWithDefaults.localePrefix === 'never') {
response = redirect(basePath);
} else if (pathLocale === locale) {
if (
hasMatchedDefaultLocale &&
configWithDefaults.localePrefix === 'as-needed'
Expand Down Expand Up @@ -168,9 +171,10 @@ export default function createMiddleware(config: MiddlewareConfig) {
}
} else {
if (
hasMatchedDefaultLocale &&
(configWithDefaults.localePrefix === 'as-needed' ||
configWithDefaults.domains)
configWithDefaults.localePrefix === 'never' ||
(hasMatchedDefaultLocale &&
(configWithDefaults.localePrefix === 'as-needed' ||
configWithDefaults.domains))
) {
response = rewrite(`/${locale}${pathWithSearch}`);
} else {
Expand Down
74 changes: 72 additions & 2 deletions packages/next-intl/test/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,68 @@ describe('prefix-based routing', () => {
expect(MockedNextResponse.next).toHaveBeenCalled();
});
});

describe('localePrefix: never', () => {
const middleware = createMockMiddleware({
defaultLocale: 'en',
locales: ['en', 'de'],
localePrefix: 'never'
});

it('rewrites requests for the default locale', () => {
middleware(createMockRequest('/'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/en'
);
});

it('rewrites requests for other locales', () => {
middleware(createMockRequest('/', 'de'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/de'
);
});

it('rewrites requests for the default locale at a nested path', () => {
middleware(createMockRequest('/list'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/en/list'
);
});

it('rewrites requests for other locales at a nested path', () => {
middleware(createMockRequest('/list', 'de'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/de/list'
);
});

it('redirects requests with default locale in the path', () => {
middleware(createMockRequest('/en'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/'
);
});

it('redirects requests with other locales in the path', () => {
middleware(createMockRequest('/de', 'de'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
'http://localhost:3000/'
);
});
});
});

describe('domain-based routing', () => {
Expand All @@ -310,7 +372,11 @@ describe('domain-based routing', () => {
locales: ['en', 'fr'],
domains: [
{defaultLocale: 'en', domain: 'en.example.com', locales: ['en']},
{defaultLocale: 'en', domain: 'ca.example.com', locales: ['en', 'fr']},
{
defaultLocale: 'en',
domain: 'ca.example.com',
locales: ['en', 'fr']
},
{defaultLocale: 'fr', domain: 'fr.example.com', locales: ['fr']}
]
});
Expand Down Expand Up @@ -514,7 +580,11 @@ describe('domain-based routing', () => {
localePrefix: 'always',
domains: [
{defaultLocale: 'en', domain: 'example.com', locales: ['en']},
{defaultLocale: 'en', domain: 'ca.example.com', locales: ['en', 'fr']}
{
defaultLocale: 'en',
domain: 'ca.example.com',
locales: ['en', 'fr']
}
]
});

Expand Down

0 comments on commit 30f885a

Please sign in to comment.