Skip to content

Commit

Permalink
Update domain locale redirect with basePath and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Dec 29, 2020
1 parent 6b122cd commit 3990850
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 59 deletions.
22 changes: 15 additions & 7 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ interface NextHistoryState {

type HistoryState = null | { __N: false } | ({ __N: true } & NextHistoryState)

let detectDomainLocale: typeof import('../i18n/detect-domain-locale').detectDomainLocale

if (process.env.__NEXT_I18N_SUPPORT) {
detectDomainLocale = require('../i18n/detect-domain-locale')
.detectDomainLocale
}

const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''

function buildCancellationError() {
Expand Down Expand Up @@ -670,10 +677,6 @@ export default class Router implements BaseRouter {
return new Promise(() => {})
}

const {
detectDomainLocale,
} = require('../i18n/detect-domain-locale') as typeof import('../i18n/detect-domain-locale')

const detectedDomain = detectDomainLocale(
this.domainLocales,
undefined,
Expand All @@ -683,11 +686,16 @@ export default class Router implements BaseRouter {
// if we are navigating to a domain locale ensure we redirect to the
// correct domain
if (detectedDomain && self.location.hostname !== detectedDomain.domain) {
const asNoBasePath = delBasePath(as)
window.location.href = `http${detectedDomain.http ? '' : 's'}://${
detectedDomain.domain
}${
this.locale === detectedDomain.defaultLocale ? '' : `/${this.locale}`
}${as === '/' ? '' : as}`
}${addBasePath(
`${
this.locale === detectedDomain.defaultLocale
? ''
: `/${this.locale}`
}${asNoBasePath === '/' ? '' : asNoBasePath}` || '/'
)}`
return new Promise(() => {})
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true)

// should be no bigger than 61.8 kb
expect(parseFloat(indexFirstLoad) - 62).toBeLessThanOrEqual(0)
expect(parseFloat(indexFirstLoad) - 62.2).toBeLessThanOrEqual(0)
expect(indexFirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
expect(err404Size.endsWith('kB')).toBe(true)

expect(parseFloat(err404FirstLoad) - 65.2).toBeLessThanOrEqual(0)
expect(parseFloat(err404FirstLoad) - 65.3).toBeLessThanOrEqual(0)
expect(err404FirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(sharedByAll) - 61.8).toBeLessThanOrEqual(0)
expect(parseFloat(sharedByAll) - 61.9).toBeLessThanOrEqual(0)
expect(sharedByAll.endsWith('kB')).toBe(true)

if (_appSize.endsWith('kB')) {
Expand Down
16 changes: 0 additions & 16 deletions test/integration/custom-routes-i18n/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@ module.exports = {
// localeDetection: false,
locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en'],
defaultLocale: 'en',
domains: [
{
// used for testing, this should not be needed in most cases
// as production domains should always use https
http: true,
domain: 'example.be',
defaultLocale: 'nl-BE',
locales: ['nl', 'nl-NL', 'nl-BE'],
},
{
http: true,
domain: 'example.fr',
defaultLocale: 'fr',
locales: ['fr-BE'],
},
],
},
async rewrites() {
return [
Expand Down
2 changes: 1 addition & 1 deletion test/integration/custom-routes-i18n/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const runTests = () => {
for (const [path, dest] of [
['/redirect-1', '/destination-1'],
['/en/redirect-1', '/destination-1'],
['/fr/redirect-1', 'http://example.fr/destination-1'],
['/fr/redirect-1', '/fr/destination-1'],
['/nl-NL/redirect-2', '/destination-2'],
['/fr/redirect-2', false],
]) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/i18n-support-base-path/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/i18n-support-base-path/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('i18n Support basePath', () => {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down
16 changes: 0 additions & 16 deletions test/integration/i18n-support-index-rewrite/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ module.exports = {
// localeDetection: false,
locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en'],
defaultLocale: 'en',
domains: [
{
// used for testing, this should not be needed in most cases
// as production domains should always use https
http: true,
domain: 'example.be',
defaultLocale: 'nl-BE',
locales: ['nl', 'nl-NL', 'nl-BE'],
},
{
http: true,
domain: 'example.fr',
defaultLocale: 'fr',
locales: ['fr-BE'],
},
],
},
async rewrites() {
return [
Expand Down
2 changes: 1 addition & 1 deletion test/integration/i18n-support/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('i18n Support', () => {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down
26 changes: 15 additions & 11 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export function runTests(ctx) {
)
})()`)

await check(() => browser.eval('window.location.hostname'), /google\.com/)
expect(await browser.eval('window.location.pathname')).toBe('/')
await check(() => browser.eval('window.location.hostname'), /example\.com/)
expect(await browser.eval('window.location.pathname')).toBe(
ctx.basePath || '/'
)

await browser.get(browser.initUrl)
await browser.waitForElementByCss('#index')
Expand All @@ -66,8 +68,10 @@ export function runTests(ctx) {
)
})()`)

await check(() => browser.eval('window.location.hostname'), /google\.com/)
expect(await browser.eval('window.location.pathname')).toBe('/go-BE/gssp')
await check(() => browser.eval('window.location.hostname'), /example\.com/)
expect(await browser.eval('window.location.pathname')).toBe(
`${ctx.basePath || ''}/go-BE/gssp`
)
})

it('should navigate through history with query correctly', async () => {
Expand Down Expand Up @@ -295,7 +299,7 @@ export function runTests(ctx) {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down Expand Up @@ -1219,7 +1223,7 @@ export function runTests(ctx) {
undefined,
{
headers: {
host: 'google.com',
host: 'example.com',
},
}
)
Expand Down Expand Up @@ -1262,7 +1266,7 @@ export function runTests(ctx) {
undefined,
{
headers: {
host: 'google.com',
host: 'example.com',
},
redirect: 'manual',
}
Expand Down Expand Up @@ -1321,8 +1325,8 @@ export function runTests(ctx) {
const checks = [
// test domain, locale prefix, redirect result
// ['example.be', 'nl-BE', 'http://example.be/'],
['google.com', 'do', 'http://example.do/'],
['example.do', 'go', 'https://google.com/'],
['example.com', 'do', 'http://example.do/'],
['example.do', 'go', 'https://example.com/'],
// ['example.fr', 'fr', 'http://example.fr/'],
]

Expand Down Expand Up @@ -1358,7 +1362,7 @@ export function runTests(ctx) {
locales: ['do-BE'],
},
{
domain: 'google.com',
domain: 'example.com',
defaultLocale: 'go',
locales: ['go-BE'],
},
Expand Down Expand Up @@ -1432,7 +1436,7 @@ export function runTests(ctx) {
it('should provide defaultLocale correctly for locale domain', async () => {
for (const { host, locale } of [
{ host: 'example.do', locale: 'do' },
{ host: 'google.com', locale: 'go' },
{ host: 'example.com', locale: 'go' },
]) {
const res = await fetchViaHTTP(
ctx.appPort,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/size-limit/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ describe('Production response size', () => {
const delta = responseSizesBytes / 1024

// Expected difference: < 0.5
expect(delta).toBeCloseTo(281.4, 0)
expect(delta).toBeCloseTo(281.96, 0)
})
})

0 comments on commit 3990850

Please sign in to comment.