Skip to content

Commit a2d6478

Browse files
authored
fix: When using domains in routing config, handle unknown domains like localhost:3000 more gracefully in middleware (amannn#1389)
When a request from an unknown host like `localhost:3000` is made—as it's often the case during development—, don't try to pick a better domain when responding with redirects in the middleware. Instead, the host is now only changed for redirects if the requested host is a known one that is specified in `domains`. Additionally, the port is now no longer removed automatically to determine a domain. This allows e.g. to pick different ports locally to test different locales.
1 parent ebe93f5 commit a2d6478

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/middleware/middleware.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,15 @@ describe('domain-based routing', () => {
23192319
);
23202320
});
23212321

2322+
it('serves requests for unknown domains based on the global `defaultLocale`', () => {
2323+
middleware(createMockRequest('/', 'en', 'http://localhost:3000'));
2324+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
2325+
expect(MockedNextResponse.redirect).not.toHaveBeenCalled();
2326+
expect(MockedNextResponse.rewrite.mock.calls[0][0].toString()).toBe(
2327+
'http://localhost:3000/en'
2328+
);
2329+
});
2330+
23222331
it('serves requests for the default locale at sub paths', () => {
23232332
middleware(createMockRequest('/about', 'en', 'http://en.example.com'));
23242333
expect(MockedNextResponse.next).not.toHaveBeenCalled();
@@ -2367,6 +2376,16 @@ describe('domain-based routing', () => {
23672376
);
23682377
});
23692378

2379+
it('removes a superfluous locale prefix of a secondary locale that is the default locale of the domain', () => {
2380+
middleware(createMockRequest('/fr', 'fr', 'http://fr.example.com'));
2381+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
2382+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
2383+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
2384+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
2385+
'http://fr.example.com/'
2386+
);
2387+
});
2388+
23702389
it('returns alternate links', () => {
23712390
const response = middleware(createMockRequest('/'));
23722391
expect(response.headers.get('link')).toBe(
@@ -2434,6 +2453,16 @@ describe('domain-based routing', () => {
24342453
'http://localhost/fr/about'
24352454
);
24362455
});
2456+
2457+
it('keeps the host of an unknown domain for easier local development', () => {
2458+
middleware(createMockRequest('/en', 'en', 'http://localhost:3000'));
2459+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
2460+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
2461+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
2462+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
2463+
'http://localhost:3000/'
2464+
);
2465+
});
24372466
});
24382467

24392468
describe('locales-restricted domain', () => {

src/middleware/middleware.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function createMiddleware<
9191
function redirect(url: string, redirectDomain?: string) {
9292
const urlObj = new URL(normalizeTrailingSlash(url), request.url);
9393

94-
if (domainsConfig.length > 0 && !redirectDomain) {
94+
if (domainsConfig.length > 0 && !redirectDomain && domain) {
9595
const bestMatchingDomain = getBestMatchingDomain(
9696
domain,
9797
locale,

src/middleware/resolveLocale.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ function findDomainFromHost<AppLocales extends Locales>(
1616
requestHeaders: Headers,
1717
domains: DomainsConfig<AppLocales>
1818
) {
19-
let host = getHost(requestHeaders);
20-
21-
// Remove port (easier for local development)
22-
host = host?.replace(/:\d+$/, '');
19+
const host = getHost(requestHeaders);
2320

2421
if (host && domains) {
2522
return domains.find((cur) => cur.domain === host);

0 commit comments

Comments
 (0)