-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
executable file
·213 lines (187 loc) · 4.25 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<template>
<div :class="['app-loading', { 'app-loaded': isLoaded }]">
<NuxtLayout>
<NuxtLoadingIndicator />
<NuxtPage />
</NuxtLayout>
</div>
</template>
<script setup lang="ts">
import { ref, watch, onMounted, computed, nextTick } from 'vue'
import { useRoute, useHead, useSeoMeta, useNuxtApp, useAsyncData } from '#app'
import gql from 'graphql-tag'
// Types
interface SeoData {
seo?: {
title?: string
metaDesc?: string
}
title?: string
}
interface Author {
node: {
name: string
}
}
interface PostData extends SeoData {
id: string
slug: string
content: string
date: string
modified: string
author: Author
}
interface PageData extends SeoData {
id: string
slug: string
}
// GraphQL queries
const GET_POST_SEO_DATA = gql`
query GetPostWithSEO($slug: ID!) {
post(id: $slug, idType: SLUG) {
id
title
slug
seo {
title
metaDesc
}
content
date
modified
author {
node {
name
}
}
}
}
`
const GET_PAGE_SEO_DATA = gql`
query GetPageWithSEO($uri: ID!) {
page(id: $uri, idType: URI) {
id
title
slug
seo {
title
metaDesc
}
}
}
`
// State and setup
const route = useRoute()
const nuxtApp = useNuxtApp()
const isLoaded = ref(false)
// Static titles mapping
const staticTitles: Record<string, string> = {
'disclaimer': 'Disclaimer',
'privacy-policy': 'Privacy Policy',
'cookie-policy': 'Cookie Policy'
}
// Computed properties
const canonicalUrl = computed(() => {
const baseUrl = 'https://wikiherbalist.com'
return route.path === '/' ? baseUrl : `${baseUrl}${route.path}`
})
const isHomepage = computed(() => route.path === '/')
// Async data fetching with useAsyncData
const { data: seoData } = useAsyncData(
'seoData',
async () => {
const routeName = route.name as string
if (!nuxtApp.$apolloClient) {
console.error('Apollo client not available')
return null
}
try {
if (routeName === 'index') {
const { data } = await nuxtApp.$apolloClient.query({
query: GET_PAGE_SEO_DATA,
variables: { uri: '/' }
})
return data.page
}
if (staticTitles[routeName]) {
return {
seo: {
title: staticTitles[routeName],
metaDesc: 'Enciclopedia online di erbe aromatiche e medicinali'
}
}
}
const isPost = !['about'].includes(routeName)
const slug = isPost
? (Array.isArray(route.params.uri) ? route.params.uri[0] : route.path.split('/').pop())
: route.path
const { data } = await nuxtApp.$apolloClient.query({
query: isPost ? GET_POST_SEO_DATA : GET_PAGE_SEO_DATA,
variables: { slug: isPost ? slug : slug || '/' }
})
return isPost ? data.post : data.page
} catch (error) {
console.error('GraphQL query error:', error)
return null
}
},
{
watch: [() => route.fullPath],
server: true,
lazy: false
}
)
// Update head metadata when seoData changes
watch(seoData, (newData) => {
if (!newData) return
const seo = newData.seo || {}
const title = isHomepage.value ? (seo.title || 'Wikiherbalist') : (seo.title || 'Pagina')
const metaDescription = seo.metaDesc || 'Enciclopedia online di erbe aromatiche e medicinali'
useHead({
titleTemplate: (titleChunk) => {
if (isHomepage.value) {
return titleChunk
}
return titleChunk ? `${titleChunk} | Wikiherbalist` : 'Wikiherbalist'
},
title,
link: [
{ rel: 'canonical', href: canonicalUrl.value },
],
})
useSeoMeta({
title,
ogTitle: title,
description: metaDescription,
ogDescription: metaDescription,
ogUrl: canonicalUrl.value,
ogImage: '/media/og-image.jpg',
twitterCard: 'summary_large_image',
})
}, { immediate: true })
// Lifecycle hooks
onMounted(() => {
nextTick(() => {
isLoaded.value = true
})
})
</script>
<style>
.app-loading {
opacity: 0;
transition: opacity 0.3s ease-in;
}
.app-loaded {
opacity: 1;
}
.debug-info {
position: fixed;
bottom: 10px;
left: 10px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 5px;
font-size: 12px;
z-index: 9999;
}
</style>