Skip to content

Commit

Permalink
feat(domains): support multiply domains (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mercs600 committed Mar 10, 2020
1 parent f604c0a commit dbe19d5
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
},
layouts: {},
i18n: {
locales: ['en', 'pl', 'de'],
locales: ['en'],
defaultLocale: 'en',
onLocaleChange: () => null,
beforeLocaleChange: () => null
Expand Down
34 changes: 34 additions & 0 deletions lib/templates/lib/domains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SET_DOMAIN, SET_LOCALES } from '~typo3/store/mutation-types'
import { getHostByURL } from '~typo3/lib/route'

/**
* Find domain based on host and available domain list
*
* @param {Object} context - Nuxt context
* @returns {Object} domain - Active domain object
*/
function getDomain({ app, req }) {
// get host depends on environment - SSR or browser
if (app.$typo3.options.domains && app.$typo3.options.domains.length) {
const host = getHostByURL(req ? req.headers.host : window.location.origin)
return app.$typo3.options.domains.find(domain => domain.name === host)
}
return false
}

/**
* Set active domain to store
*
* @param {Object} Context - Nuxt context
* @param {Object} domain - Active domain object
* @returns {void}
*/
function setDomain({ store, app }, domain) {
if (domain) {
app.$typo3.api.setOptions(domain.api)
store.commit(SET_LOCALES, domain.i18n.locales)
store.commit(SET_DOMAIN, domain)
}
}

export { getDomain, setDomain }
22 changes: 21 additions & 1 deletion lib/templates/lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ function isDynamicRoute(route) {
return route.matched[0].path.includes('*')
}

export { isDynamicRoute }
/**
* Get domain host from url
* e.g. https://en.myservice.com:4000/path returns en.myservice.com
*
* @param {String} url
*/
function getHostByURL(url) {
const match = url.match(/^(http:\/\/|https:\/\/)?(www[0-9]?.)?([^/:]*).*$/)
if (
match != null &&
match.length > 2 &&
typeof match[3] === 'string' &&
match[3].length > 0
) {
return match[3]
} else {
return null
}
}

export { isDynamicRoute, getHostByURL }
12 changes: 11 additions & 1 deletion lib/templates/plugins/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import API from '~typo3/lib/api'
import { getLocaleCodePath } from '~typo3/lib/i18n'
export default function(context, options) {
const { store, $axios } = context
const api = {
...new API(options, context.$axios)
...new API(
store.state.typo3 && store.state.typo3.domain
? store.state.typo3.domain
: options,
$axios
),
setOptions(options) {
Object.assign(this.$http.defaults, options)
Object.assign(this.options, options)
}
}

// add initialData fallback for 404 pages
Expand Down
2 changes: 2 additions & 0 deletions lib/templates/plugins/context.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import api from '~typo3/plugins/api'
import i18n from '~typo3/plugins/i18n'
import domains from '~typo3/plugins/domains'
export default function (context, inject) {
const moduleOptions = <%= serialize(options) %>
const _options = {
api: api(context, moduleOptions),
i18n: i18n(context, moduleOptions),
domains: domains(context, moduleOptions),
options: moduleOptions
}
inject('typo3', _options)
Expand Down
8 changes: 8 additions & 0 deletions lib/templates/plugins/domains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getDomain, setDomain } from '~typo3/lib/domains'
export default (context, options) => {
return {
list: options.domains ? options.domains : [],
getDomain: () => getDomain(context),
setDomain: domain => setDomain(context, domain)
}
}
13 changes: 10 additions & 3 deletions lib/templates/plugins/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import { getLocaleCodePath, getLocaleByPath, setLocale } from '~typo3/lib/i18n'
export default (context, options) => {
return {
...options.i18n,
locale: context.store.state.typo3
? context.store.state.typo3.locale
: options.i18n.defaultLocale,
get locale() {
return context.store.state.typo3
? context.store.state.typo3.locale
: options.i18n.defaultLocale
},
get locales() {
return context.store.state.typo3 && context.store.state.typo3.locales
? context.store.state.typo3.locales
: options.i18n.locales
},
getLocaleCodePath: () => getLocaleCodePath(context),
getLocaleByPath: () => getLocaleByPath(context),
setLocale: (localeCode, updateInitialData) =>
Expand Down
3 changes: 3 additions & 0 deletions lib/templates/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default {
* Called during SSR init
*/
async nuxtServerInit({ commit, dispatch, state }, { app }) {
if (app.$typo3.domains) {
app.$typo3.domains.setDomain(app.$typo3.domains.getDomain())
}
app.$typo3.i18n.setLocale(app.$typo3.i18n.getLocaleByPath(), false)
await dispatch('getInitialData')
},
Expand Down
4 changes: 3 additions & 1 deletion lib/templates/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default ({ store }) => {
layout: 'default',
locale: '',
initial: {},
page: {}
page: {},
domain: null,
locales: null
},
actions,
mutations
Expand Down
2 changes: 2 additions & 0 deletions lib/templates/store/mutation-types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const SET_INITIAL = 'SET_INITIAL_DATA'
export const SET_LOCALE = 'SET_LOCALE_ACTIVE'
export const SET_LOCALES = 'SET_AVAILABLE_LOCALES'
export const SET_PAGE = 'SET_PAGE_DATA'
export const SET_DOMAIN = 'SET_DOMAIN'
6 changes: 6 additions & 0 deletions lib/templates/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ export default {
},
[types.SET_PAGE](state, data) {
state.page = data
},
[types.SET_DOMAIN](state, domain) {
state.domain = domain
},
[types.SET_LOCALES](state, locales) {
state.locales = locales
}
}

0 comments on commit dbe19d5

Please sign in to comment.