question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for subdomain routing

See original GitHub issue

As i18next-browser-languageDetector supports subdomain detection out of the box, it’d be nice to also support it here.

We would have two mutually exclusive options regarding routing:

  1. localeSubpaths (already exists)
  2. localeSubdomains (does not exist)

Or perhaps we’d have a single localeRouting option that would take an enum of strings, eg subpaths or subdomains.

We’d need to add logic to lng-path-corrector and lng-path-detector to handle subdomains instead of subpaths.

We’d also need to update the Link component.

I don’t yet know how feasible this proposal is, as it’s not possible to have a SPA across subdomains anyways. Development and testing will also be a pain.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
lucasfelicianocommented, Aug 13, 2019

Hey @HeadFox, here is my implementation of my detector

languageByDomainDetector.js

const getLanguageFromHostname = require('./getLanguageFromHostname')

module.exports = {
  name: 'domain',

  lookup(req, res, options) {
    let language = 'en'

    if (typeof window !== 'undefined' ) {
      language = getLanguageFromHostname(window.location.hostname)
    } else {
      language = getLanguageFromHostname(req.get('host'))
    }

    return language
  },

  cacheUserLanguage(req, res, language, options = {}) {
    //todo
  },
}

The getLanguageFromHostname is a helper that I have a map of my domains which is mapped to a certain language

And when creating the next-i18next instance you pass like this:

const NextI18NextInstance = new NextI18Next({
  defaultLanguage: 'en',
  fallbackLng: 'en',
  otherLanguages: ['nl', 'de', 'fr', 'hu', 'es'],
  load: 'languageOnly',
  customDetectors: [languageByDomainDetector], // <- Add the domain detector as a custom detector
  detection: {
    order: ['domain'], // <- Say to i18n to use the domain detector
  },
})
1reaction
isaachinmancommented, Jan 14, 2019

When changing languages in the localeSubpaths approach, for example, we can maintain full SPA navigation. That is to say, a router event happens within NextJs and a new chunk/page and namespaces will be loaded, but are no new document requests or hard navigation.

That is not possible over subdomains - you are forced to basically serve two completely different webpages. You might as well have static React apps, or just forcefully set the locale and don’t allow changing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing traffic for subdomains - Amazon Route 53
One way to route traffic for a subdomain is to create a hosted zone for the subdomain, and then create records for the...
Read more >
Add support for subdomain routing #16 - i18next/next- ...
We'd need to add logic to lng-path-corrector and lng-path-detector to handle subdomains instead of subpaths. We'd also need to update the Link component....
Read more >
Building a Rails App With Multiple Subdomains
We'll use Rails' powerful routing constructs to support multiple subdomains in our application. We'll also set up subdomains locally and ...
Read more >
Creating a subdomain that uses Amazon Route 53 as ...
Using the method provided by the DNS service of the parent domain, add NS records for the subdomain to the zone file for...
Read more >
How to Create a Subdomain for my Domain
1. Go to your Domain List and click Manage next to the domain: · 2. Select the Advanced DNS tab: · 3. Find...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found