Skip to content

Commit 514ad63

Browse files
darcoderpaulgv
authored andcommitted
feat: Add support for X-Forwarded-Host (#92)
1 parent cbee47d commit 514ad63

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/helpers/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,27 @@ export const getLocaleFromRoute = (route = {}, routesNameSeparator = '', locales
8888
return null
8989
}
9090

91+
/**
92+
* Get x-forwarded-host
93+
* @return {String} x-forwarded-host
94+
*/
95+
export const getForwarded = () => (
96+
process.browser ? window.location.href.split('/')[2] : (req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host) // eslint-disable-line
97+
)
98+
9199
/**
92100
* Get hostname
93101
* @return {String} Hostname
94102
*/
95103
export const getHostname = () => (
96104
process.browser ? window.location.href.split('/')[2] : req.headers.host // eslint-disable-line
97105
)
98-
99106
/**
100107
* Get locale code that corresponds to current hostname
101108
* @return {String} Locade code found if any
102109
*/
103110
export const getLocaleDomain = () => {
104-
const hostname = getHostname()
111+
const hostname = app.i18n.forwardedHost ? getForwarded() : getHostname()
105112
if (hostname) {
106113
const localeDomain = app.i18n.locales.find(l => l[LOCALE_DOMAIN_KEY] === hostname) // eslint-disable-line
107114
if (localeDomain) {

src/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { makeRoutes } from './helpers/routes'
1818
import {
1919
getLocaleCodes,
2020
getLocaleFromRoute,
21+
getForwarded,
2122
getHostname,
2223
getLocaleDomain,
2324
syncVuex
@@ -45,6 +46,7 @@ export default function (userOptions) {
4546
LOCALE_FILE_KEY,
4647
getLocaleCodes,
4748
getLocaleFromRoute,
49+
getForwarded,
4850
getHostname,
4951
getLocaleDomain,
5052
syncVuex

src/plugins/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default async ({ app, route, store, req }) => {
1414
const getLocaleCodes = <%= options.getLocaleCodes %>
1515
const getLocaleFromRoute = <%= options.getLocaleFromRoute %>
1616
const getHostname = <%= options.getHostname %>
17+
const getForwarded = <%= options.getForwarded %>
1718
const getLocaleDomain = <%= options.getLocaleDomain %>
1819
const syncVuex = <%= options.syncVuex %>
1920

@@ -51,6 +52,7 @@ export default async ({ app, route, store, req }) => {
5152
app.i18n.locales = <%= JSON.stringify(options.locales) %>
5253
app.i18n.defaultLocale = '<%= options.defaultLocale %>'
5354
app.i18n.differentDomains = <%= options.differentDomains %>
55+
app.i18n.forwardedHost = <%= options.forwardedHost %>
5456
app.i18n.routesNameSeparator = '<%= options.routesNameSeparator %>'
5557
app.i18n.beforeLanguageSwitch = <%= options.beforeLanguageSwitch %>
5658
app.i18n.onLanguageSwitched = <%= options.onLanguageSwitched %>

0 commit comments

Comments
 (0)