fix(seo): load site URL from Vite environment
This commit is contained in:
+29
-21
@@ -1,4 +1,4 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig, loadEnv } from 'vite'
|
||||||
import { mkdirSync, writeFileSync } from 'node:fs'
|
import { mkdirSync, writeFileSync } from 'node:fs'
|
||||||
import { fileURLToPath, URL } from 'node:url'
|
import { fileURLToPath, URL } from 'node:url'
|
||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
@@ -10,17 +10,20 @@ const publicRoutes = [
|
|||||||
'/community',
|
'/community',
|
||||||
]
|
]
|
||||||
|
|
||||||
function getSiteUrl() {
|
const DEFAULT_SITE_URL = 'http://localhost:5173'
|
||||||
const explicitUrl = process.env.VITE_SITE_URL
|
|
||||||
const siteUrl = explicitUrl
|
function getSiteUrl(value?: string) {
|
||||||
return siteUrl
|
const siteUrl = new URL(value?.trim() || DEFAULT_SITE_URL)
|
||||||
|
if (!['http:', 'https:'].includes(siteUrl.protocol)) {
|
||||||
|
throw new Error('VITE_SITE_URL 必须使用 http 或 https 协议')
|
||||||
|
}
|
||||||
|
return siteUrl.href.replace(/\/$/, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function seoFilesPlugin() {
|
function seoFilesPlugin(siteUrl: string) {
|
||||||
return {
|
return {
|
||||||
name: 'opencloud-seo-files',
|
name: 'opencloud-seo-files',
|
||||||
closeBundle() {
|
closeBundle() {
|
||||||
const siteUrl = getSiteUrl()
|
|
||||||
mkdirSync('dist/.well-known', { recursive: true })
|
mkdirSync('dist/.well-known', { recursive: true })
|
||||||
|
|
||||||
const robots = [
|
const robots = [
|
||||||
@@ -55,19 +58,24 @@ function seoFilesPlugin() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig(({ mode }) => {
|
||||||
plugins: [vue(), tailwindcss(), seoFilesPlugin()],
|
const env = loadEnv(mode, process.cwd(), 'VITE_')
|
||||||
server: {
|
const siteUrl = getSiteUrl(env.VITE_SITE_URL)
|
||||||
port: 5173,
|
|
||||||
strictPort: true,
|
return {
|
||||||
},
|
plugins: [vue(), tailwindcss(), seoFilesPlugin(siteUrl)],
|
||||||
preview: {
|
server: {
|
||||||
port: 5173,
|
port: 5173,
|
||||||
strictPort: true,
|
strictPort: true,
|
||||||
},
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
||||||
},
|
},
|
||||||
},
|
preview: {
|
||||||
|
port: 5173,
|
||||||
|
strictPort: true,
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user