From 50c0a14cc695f5fb7161048b4492837b734817d0 Mon Sep 17 00:00:00 2001 From: keven Date: Sun, 19 Oct 2025 00:26:12 +0800 Subject: [PATCH] feat(front): implement dynamic API base URL retrieval and enhance SEO metadata handling --- front/composables/useSeo.ts | 27 +++++++++++++++++---------- front/lib/getApiBaseUrl.ts | 5 +++++ front/nuxt.config.ts | 3 ++- 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 front/lib/getApiBaseUrl.ts diff --git a/front/composables/useSeo.ts b/front/composables/useSeo.ts index e700da2..fba1790 100644 --- a/front/composables/useSeo.ts +++ b/front/composables/useSeo.ts @@ -1,23 +1,30 @@ +import getApiBaseUrl from '~/lib/getApiBaseUrl' + type UseSeoProps = { head?: Record seo?: Record } const useSeo = async (props: UseSeoProps = {}) => { const { head, seo } = props || {} - const seoMeta = ref() + const seoMeta = ref<{ + site_title: string + site_desc: string + site_url: string + site_icon: string + site_bg_url: string + }>() if (import.meta.server) { - const { SITE_TITLE, SITE_DESC, SITE_URL } = process.env || {} - seoMeta.value = { - site_title: SITE_TITLE, - site_desc: SITE_DESC, - site_url: SITE_URL, - } + await fetch(`${getApiBaseUrl()}/config`) + .then((res) => res.json()) + .then(({ data }) => { + seoMeta.value = data + }) const { title } = head || {} useHead({ link: [ - { rel: 'icon', href: '/logo.png', sizes: 'any' }, + { rel: 'icon', href: seoMeta.value?.site_icon || '/logo.png', sizes: 'any' }, // { rel: 'icon', href: '/favicon.svg', sizes: 'any', type: 'image/svg+xml' }, - { rel: 'apple-touch-icon', sizes: '180x180', href: '/logo.png' }, + { rel: 'apple-touch-icon', sizes: '180x180', href: seoMeta.value?.site_icon || '/logo.png' }, ], meta: [ // used on some mobile browsers @@ -33,7 +40,7 @@ const useSeo = async (props: UseSeoProps = {}) => { ogTitle: seoMeta?.value?.site_title, ogDescription: seoMeta?.value?.site_desc, ogImage: { - url: `${seoMeta?.value?.site_url}/logo.png`, + url: `${seoMeta?.value?.site_url}${seoMeta?.value?.site_icon || '/logo.png'}`, width: 1024, height: 1024, alt: 'logo', diff --git a/front/lib/getApiBaseUrl.ts b/front/lib/getApiBaseUrl.ts new file mode 100644 index 0000000..d653a6d --- /dev/null +++ b/front/lib/getApiBaseUrl.ts @@ -0,0 +1,5 @@ +const getApiBaseUrl = () => { + return import.meta.env.API_BASE_URL?.replace(/\/$/, '') || 'http://127.0.0.1:5001' +} + +export default getApiBaseUrl diff --git a/front/nuxt.config.ts b/front/nuxt.config.ts index ca15b2c..05d2fc5 100644 --- a/front/nuxt.config.ts +++ b/front/nuxt.config.ts @@ -1,4 +1,5 @@ import tailwindcss from '@tailwindcss/vite' +import getApiBaseUrl from './lib/getApiBaseUrl' // https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ compatibilityDate: '2024-04-03', @@ -35,7 +36,7 @@ export default defineNuxtConfig({ nitro: { routeRules: { '/api/**': { - proxy: process.env.API_BASE_URL || 'http://127.0.0.1:1323/**', + proxy: `${getApiBaseUrl()}/**`, }, }, },