From b376d66e58a115a2a18917b8d855c234c4174345 Mon Sep 17 00:00:00 2001 From: Kasper Seweryn Date: Wed, 14 Jun 2023 21:34:09 +0200 Subject: [PATCH] feat(instance): standardize instanceUrl value Part-of: --- changes/changelog.d/2113.bugfix | 1 + front/src/components/SetInstanceModal.vue | 2 +- front/src/store/instance.ts | 56 +++++++++++------------ 3 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 changes/changelog.d/2113.bugfix diff --git a/changes/changelog.d/2113.bugfix b/changes/changelog.d/2113.bugfix new file mode 100644 index 000000000..c6334df3b --- /dev/null +++ b/changes/changelog.d/2113.bugfix @@ -0,0 +1 @@ +Standardize instanceUrl value in instance store (#2113) diff --git a/front/src/components/SetInstanceModal.vue b/front/src/components/SetInstanceModal.vue index 199724e39..43b1662e9 100644 --- a/front/src/components/SetInstanceModal.vue +++ b/front/src/components/SetInstanceModal.vue @@ -33,7 +33,7 @@ const suggestedInstances = computed(() => { ...store.state.instance.knownInstances, serverUrl.endsWith('/') ? serverUrl : serverUrl + '/', store.getters['instance/defaultInstance'] - ]).slice(1) + ]) }) watch(() => store.state.instance.instanceUrl, () => store.dispatch('instance/fetchSettings')) diff --git a/front/src/store/instance.ts b/front/src/store/instance.ts index f2f1a55a7..f0d20bb6b 100644 --- a/front/src/store/instance.ts +++ b/front/src/store/instance.ts @@ -126,16 +126,21 @@ const logger = useLogger() // 1. use the url provided in settings.json, if any // 2. use the url specified when building via VUE_APP_INSTANCE_URL // 3. use the current url -const instanceUrl = import.meta.env.VUE_APP_INSTANCE_URL as string ?? location.origin +let DEFAULT_INSTANCE_URL = `${location.origin}/` +try { + DEFAULT_INSTANCE_URL = new URL(import.meta.env.VUE_APP_INSTANCE_URL as string).href +} catch (e) { + logger.warn('Invalid VUE_APP_INSTANCE_URL, falling back to current url', e) +} const store: Module = { namespaced: true, state: { frontSettings: { - defaultServerUrl: instanceUrl, + defaultServerUrl: DEFAULT_INSTANCE_URL, additionalStylesheets: [] }, - instanceUrl, + instanceUrl: DEFAULT_INSTANCE_URL, knownInstances: [], nodeinfo: null, settings: { @@ -190,40 +195,31 @@ const store: Module = { state.nodeinfo = value }, instanceUrl: (state, value) => { - if (value && !value.endsWith('/')) { - value = value + '/' - } + try { + const { href } = new URL(value) + state.instanceUrl = href + axios.defaults.baseURL = `${href}api/v1/` - state.instanceUrl = value - - // append the URL to the list (and remove existing one if needed) - if (value) { - const index = state.knownInstances.indexOf(value) - if (index > -1) { - state.knownInstances.splice(index, 1) - } - state.knownInstances.splice(0, 0, value) - } - - if (!value) { + // append the URL to the list (and remove existing one if needed) + const index = state.knownInstances.indexOf(href) + if (index > -1) state.knownInstances.splice(index, 1) + state.knownInstances.unshift(href) + } catch (e) { + logger.error('Invalid instance URL', e) axios.defaults.baseURL = undefined - return } - const suffix = 'api/v1/' - axios.defaults.baseURL = state.instanceUrl + suffix } }, getters: { - absoluteUrl: (state) => (relativeUrl: string) => { + absoluteUrl: (_state, getters) => (relativeUrl: string) => { if (relativeUrl.startsWith('http')) return relativeUrl - if (state.instanceUrl?.endsWith('/') && relativeUrl.startsWith('/')) { - relativeUrl = relativeUrl.slice(1) - } - - return (state.instanceUrl ?? instanceUrl) + relativeUrl + return relativeUrl.startsWith('/') + ? `${getters.url.href}${relativeUrl.slice(1)}` + : `${getters.url.href}${relativeUrl}` }, - domain: (state) => new URL(state.instanceUrl ?? instanceUrl).hostname, - defaultInstance: () => instanceUrl + url: (state) => new URL(state.instanceUrl ?? DEFAULT_INSTANCE_URL), + domain: (_state, getters) => getters.url.hostname, + defaultInstance: () => DEFAULT_INSTANCE_URL }, actions: { setUrl ({ commit }, url) { @@ -269,7 +265,7 @@ const store: Module = { for (const [key, value] of Object.entries(response.data as FrontendSettings)) { if (key === 'defaultServerUrl' && !value) { - state.frontSettings.defaultServerUrl = instanceUrl + state.frontSettings.defaultServerUrl = DEFAULT_INSTANCE_URL continue }