Skip to content

Commit

Permalink
fix: multidomain config
Browse files Browse the repository at this point in the history
  • Loading branch information
mercs600 committed Nov 30, 2023
1 parent 4db575d commit 5c509d1
Show file tree
Hide file tree
Showing 7 changed files with 1,053 additions and 1,032 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@nuxtjs/eslint-config-typescript": "^12.1.0",
"eslint": "^8.29.0",
"husky": "^8.0.0",
"nuxt": "^3.8.0",
"nuxt": "^3.8.2",
"playwright": "^1.28.1",
"standard-version": "^9.5.0",
"vitest": "latest"
Expand Down
24 changes: 18 additions & 6 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import nuxtTypo3 from '..'
export default defineNuxtConfig({
modules: [nuxtTypo3],
typo3: {
api: {
baseUrl: 'http://localhost:3000/api'
},
i18n: {
locales: ['en', 'de', 'pl']
}
sites: [
{
hostname: 'localhost',
api: {
baseUrl: 'http://localhost:3000/api'
},
i18n: {
default: 'en',
locales: ['en', 'de', 'pl']
}
},
{
hostname: 'local.t3pwa.com',
api: {
baseUrl: 'http://localhost:3000/api'
}
}
]
},
components: {
dirs: [
Expand Down
25 changes: 16 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export default defineNuxtModule<ModuleOptions>({
}
},
hooks: {
't3:initialData': () => {},
't3:page': () => {},
't3:i18n': () => {},
't3:initialData': () => { },
't3:page': () => { },
't3:i18n': () => { },
'components:dirs' (dirs) {
dirs.push({
path: fileURLToPath(new URL('./runtime/components', import.meta.url)),
Expand All @@ -80,7 +80,9 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.build.transpile.push(runtimeDir)

if (options.sites) {
options.sites = mergeSiteOptions(options)
options = {
sites: mergeSiteOptions(options)
}
}

nuxt.options.runtimeConfig.public.typo3 = defu(
Expand Down Expand Up @@ -113,9 +115,13 @@ export default defineNuxtModule<ModuleOptions>({

const mergeSiteOptions = (options: ModuleOptions) => {
return options.sites?.map((site) => {
const _options = { ...options }
delete _options.sites
return defu(site, _options)
const defaulOptions = { ...options }
delete defaulOptions.sites
const siteOptions = defu(site, defaulOptions)
if (site.i18n?.locales) {
siteOptions.i18n.locales = [...new Set([...defaulOptions.i18n!.locales, ...site.i18n?.locales])]
}
return siteOptions
})
}

Expand All @@ -127,16 +133,17 @@ declare module '#app' {
}
}

interface RuntimeNuxtHooks extends ModuleHooks {}
interface RuntimeNuxtHooks extends ModuleHooks { }
}

declare module '@nuxt/schema' {
interface NuxtHooks extends ModuleHooks {}
interface NuxtHooks extends ModuleHooks { }

interface ConfigSchema {
runtimeConfig: {
public?: {
typo3?: ModuleOptions
typo3current: any
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/runtime/composables/useT3Options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { computed } from 'vue'
import type { ComputedRef } from 'vue'
import { useRuntimeConfig, useRequestHeaders } from '#app'
import type { Ref } from 'vue'
import { useRuntimeConfig, useRequestHeaders, useState } from '#app'
import type { T3Site, T3Options } from '../../types'
import { getRawHost } from '../lib/url'

export const useT3OptionsState = () => {
const options = useState<T3Site | null>('T3:Options')
return options
}

export const useT3Options = (): {
/**
* Get all module options
Expand All @@ -16,11 +20,10 @@ export const useT3Options = (): {
/**
* Get current site options computed value
*/
currentSiteOptions: ComputedRef<T3Site>
currentSiteOptions: Ref<T3Site>
} => {
const options = useRuntimeConfig().public.typo3 as T3Options

const currentSiteOptions = computed(() => getSiteOptions())
const currentSiteOptions = useT3OptionsState() as Ref<T3Site>

const getSiteOptions = (domain?: string): T3Site => {
const { sites } = options
Expand All @@ -47,5 +50,9 @@ export const useT3Options = (): {
return site
}

if (!currentSiteOptions.value) {
currentSiteOptions.value = getSiteOptions()
}

return { options, getSiteOptions, currentSiteOptions }
}
41 changes: 30 additions & 11 deletions test/fixtures/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { defineNuxtConfig } from 'nuxt/config'
import MyModule from '../..'
import MyModule from '../../'

export default defineNuxtConfig({
modules: [MyModule],
typo3: {
api: {
baseUrl: 'https://my-api-demo.com'
},
i18n: {
default: 'pl',
locales: ['pl', 'en', 'de']
},
features: {
initInitialData: false
}
sites:
[
{
hostname: '127.0.0.1',
api: {
baseUrl: 'https://my-api-demo.com'
},
i18n: {
default: 'pl',
locales: ['pl', 'en', 'de']
},
features: {
initInitialData: false
}
},
{
hostname: 'localhost',
api: {
baseUrl: 'https://my-api-demo.com'
},
i18n: {
default: 'pl',
locales: ['pl', 'en', 'de']
},
features: {
initInitialData: false
}
}
]
}
})
4 changes: 2 additions & 2 deletions test/fixtures/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

<script lang="ts" setup>
import { useT3Options } from '../../../src/runtime/composables/useT3Options'
const { options } = useT3Options()
const { api, i18n } = options
const { currentSiteOptions } = useT3Options()
const { api, i18n } = currentSiteOptions.value
</script>
Loading

0 comments on commit 5c509d1

Please sign in to comment.