Add SEO metadata and security headers
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
export const CLOUD_TYPES = [
|
||||
{ id: 1, name: '积云', nameEn: 'Cumulus' },
|
||||
{ id: 2, name: '层云', nameEn: 'Stratus' },
|
||||
{ id: 3, name: '卷云', nameEn: 'Cirrus' },
|
||||
{ id: 4, name: '积雨云', nameEn: 'Cumulonimbus' },
|
||||
{ id: 5, name: '层积云', nameEn: 'Stratocumulus' },
|
||||
{ id: 6, name: '高积云', nameEn: 'Altocumulus' },
|
||||
{ id: 7, name: '高层云', nameEn: 'Altostratus' },
|
||||
{ id: 8, name: '雨层云', nameEn: 'Nimbostratus' },
|
||||
{ id: 9, name: '卷层云', nameEn: 'Cirrostratus' },
|
||||
{ id: 10, name: '卷积云', nameEn: 'Cirrocumulus' },
|
||||
] as const
|
||||
|
||||
export function getCloudTypeName(id: string | string[]) {
|
||||
const cloudTypeId = Number(Array.isArray(id) ? id[0] : id)
|
||||
return CLOUD_TYPES.find(item => item.id === cloudTypeId)?.name ?? '云型详情'
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
||||
|
||||
const defaultTitle = 'OpenCloud'
|
||||
const defaultDescription = 'OpenCloud - 活的天空地图,拍云、识别、收藏'
|
||||
|
||||
function resolveSiteUrl() {
|
||||
return (import.meta.env.VITE_SITE_URL || window.location.origin).replace(/\/$/, '')
|
||||
}
|
||||
|
||||
function upsertMeta(name: string, content: string) {
|
||||
let element = document.querySelector<HTMLMetaElement>(`meta[name="${name}"]`)
|
||||
if (!element) {
|
||||
element = document.createElement('meta')
|
||||
element.name = name
|
||||
document.head.appendChild(element)
|
||||
}
|
||||
element.content = content
|
||||
}
|
||||
|
||||
function upsertPropertyMeta(property: string, content: string) {
|
||||
let element = document.querySelector<HTMLMetaElement>(`meta[property="${property}"]`)
|
||||
if (!element) {
|
||||
element = document.createElement('meta')
|
||||
element.setAttribute('property', property)
|
||||
document.head.appendChild(element)
|
||||
}
|
||||
element.content = content
|
||||
}
|
||||
|
||||
function upsertCanonical(href: string) {
|
||||
let element = document.querySelector<HTMLLinkElement>('link[rel="canonical"]')
|
||||
if (!element) {
|
||||
element = document.createElement('link')
|
||||
element.rel = 'canonical'
|
||||
document.head.appendChild(element)
|
||||
}
|
||||
element.href = href
|
||||
}
|
||||
|
||||
export function applyRouteSeo(route: RouteLocationNormalizedLoaded) {
|
||||
const title = typeof route.meta.title === 'function' ? route.meta.title(route) : route.meta.title
|
||||
const description = typeof route.meta.description === 'function' ? route.meta.description(route) : route.meta.description
|
||||
const robots = route.meta.noindex ? 'noindex, nofollow' : 'index, follow'
|
||||
const canonicalPath = route.meta.canonicalPath || route.path
|
||||
const canonical = `${resolveSiteUrl()}${canonicalPath}`
|
||||
const pageTitle = title ? `${title} | OpenCloud` : defaultTitle
|
||||
const pageDescription = description || defaultDescription
|
||||
|
||||
document.title = pageTitle
|
||||
upsertMeta('description', pageDescription)
|
||||
upsertMeta('robots', robots)
|
||||
upsertCanonical(canonical)
|
||||
|
||||
upsertPropertyMeta('og:site_name', 'OpenCloud')
|
||||
upsertPropertyMeta('og:type', 'website')
|
||||
upsertPropertyMeta('og:title', pageTitle)
|
||||
upsertPropertyMeta('og:description', pageDescription)
|
||||
upsertPropertyMeta('og:url', canonical)
|
||||
|
||||
upsertMeta('twitter:card', 'summary')
|
||||
upsertMeta('twitter:title', pageTitle)
|
||||
upsertMeta('twitter:description', pageDescription)
|
||||
}
|
||||
+40
-4
@@ -1,4 +1,6 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { getCloudTypeName } from '@/lib/cloudTypes'
|
||||
import { applyRouteSeo } from '@/lib/seo'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const router = createRouter({
|
||||
@@ -8,85 +10,115 @@ const router = createRouter({
|
||||
path: '/',
|
||||
name: 'map',
|
||||
component: () => import('@/views/map/MapView.vue'),
|
||||
meta: {
|
||||
title: '实时云图地图',
|
||||
description: '在 OpenCloud 地图上浏览社区拍摄的云图,按位置查看天空观测记录。',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('@/views/auth/LoginView.vue'),
|
||||
meta: { title: '登录', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/register',
|
||||
name: 'register',
|
||||
component: () => import('@/views/auth/RegisterView.vue'),
|
||||
meta: { title: '注册', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/auth/confirm',
|
||||
name: 'auth-confirm',
|
||||
component: () => import('@/views/auth/AuthConfirmView.vue'),
|
||||
meta: { title: '确认邮箱', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/auth/reset-password',
|
||||
name: 'auth-reset-password',
|
||||
component: () => import('@/views/auth/ResetPasswordView.vue'),
|
||||
meta: { title: '重置密码', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/upload',
|
||||
name: 'upload',
|
||||
component: () => import('@/views/upload/UploadView.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
meta: { requiresAuth: true, title: '上传云图', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/encyclopedia',
|
||||
name: 'encyclopedia',
|
||||
component: () => import('@/views/encyclopedia/EncyclopediaView.vue'),
|
||||
meta: {
|
||||
title: '云朵图鉴',
|
||||
description: '浏览 10 种基础云属,了解云型特征,并通过拍摄云图点亮个人图鉴。',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/encyclopedia/:id',
|
||||
name: 'cloud-type',
|
||||
component: () => import('@/views/encyclopedia/CloudTypeView.vue'),
|
||||
meta: {
|
||||
title: route => getCloudTypeName(route.params.id),
|
||||
description: route => `查看 ${getCloudTypeName(route.params.id)} 的识别要点、公开云图和社区观测记录。`,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/gallery',
|
||||
name: 'gallery',
|
||||
component: () => import('@/views/gallery/GalleryView.vue'),
|
||||
meta: {
|
||||
title: '云图画廊',
|
||||
description: '按时间浏览 OpenCloud 社区公开分享的云图照片和观测记录。',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/community',
|
||||
name: 'community',
|
||||
component: () => import('@/views/community/CommunityView.vue'),
|
||||
meta: {
|
||||
title: '社区',
|
||||
description: '查看 OpenCloud 社区动态,发现更多天空观测和云图收藏。',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/profile',
|
||||
name: 'profile',
|
||||
component: () => import('@/views/profile/ProfileView.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
meta: { requiresAuth: true, title: '个人主页', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/profile/settings',
|
||||
name: 'profile-settings',
|
||||
component: () => import('@/views/profile/ProfileSettingsView.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
meta: { requiresAuth: true, title: '账号设置', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/profile/:id',
|
||||
name: 'user-profile',
|
||||
component: () => import('@/views/profile/ProfileView.vue'),
|
||||
meta: {
|
||||
title: '用户云图档案',
|
||||
description: '查看用户公开分享的云图记录、图鉴进度和拍摄时间线。',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/admin',
|
||||
name: 'admin',
|
||||
component: () => import('@/views/admin/AdminView.vue'),
|
||||
meta: { requiresAuth: true, requiresAdmin: true },
|
||||
meta: { requiresAuth: true, requiresAdmin: true, title: '管理后台', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/403',
|
||||
name: 'forbidden',
|
||||
component: () => import('@/views/system/ForbiddenView.vue'),
|
||||
meta: { title: '无权访问', noindex: true },
|
||||
},
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
name: 'not-found',
|
||||
component: () => import('@/views/system/NotFoundView.vue'),
|
||||
meta: { title: '页面不存在', noindex: true },
|
||||
},
|
||||
],
|
||||
scrollBehavior() {
|
||||
@@ -108,4 +140,8 @@ router.beforeEach((to, _from, next) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.afterEach(to => {
|
||||
applyRouteSeo(to)
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
||||
|
||||
declare module 'vue-router' {
|
||||
interface RouteMeta {
|
||||
title?: string | ((route: RouteLocationNormalizedLoaded) => string)
|
||||
description?: string | ((route: RouteLocationNormalizedLoaded) => string)
|
||||
canonicalPath?: string
|
||||
noindex?: boolean
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user