forked from danielroe/page-speed.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
255 lines (239 loc) · 8.04 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<script lang="ts" setup>
import '@unocss/reset/tailwind-compat.css'
import { joinURL } from 'ufo'
import { vConfetti } from '@neoconfetti/vue'
const route = useRoute()
const domain = computed(() => sanitizeDomain(route.path))
const hasValidDomain = computed(() => !!domain.value && isValidDomain(domain.value))
const canonicalURL = computed(() => domain.value ? joinURL(`https://page-speed.dev`, domain.value) : 'https://page-speed.dev')
const editing = ref(!domain.value)
const [{ data: crux, status: cruxStatus, refresh: cruxRefresh }, { data: lighthouse, status: lighthouseStatus, refresh: lighthouseRefresh }] = await Promise.all([
useAsyncData(() => $fetch(`/api/crux/${domain.value}`), {
immediate: hasValidDomain.value,
}),
useAsyncData(() => $fetch(`/api/run/${domain.value}`), {
immediate: hasValidDomain.value,
server: false,
}),
])
watch(domain, () => {
if (hasValidDomain.value) {
lighthouseRefresh()
cruxRefresh()
}
else {
lighthouse.value = null
crux.value = null
}
})
if (import.meta.server) {
const validatedDomain = crux.value?.domain || lighthouse.value?.domain
if (validatedDomain && validatedDomain !== domain.value)
await navigateTo(`/${validatedDomain}`)
}
if (import.meta.client) {
watch([crux, lighthouse], ([crux, lighthouse]) => {
const validatedDomain = crux?.domain || lighthouse?.domain
if (validatedDomain && validatedDomain !== domain.value)
navigateTo(`/${validatedDomain}`)
})
}
const keys = ['performance', 'accessibility', 'bestPractices', 'seo'] as const
const showConfetti = computed(() => {
// CWV fail, but ignore if there is now CrUX data
if (cruxStatus.value !== 'pending' && crux.value && !crux.value.cwv)
return false
return lighthouse.value && keys.every(key => lighthouse.value![key] === 100)
})
useFavicon(() => lighthouseStatus.value !== 'pending' && hasValidDomain.value && (lighthouse.value ? lighthouse.value.performance : false))
useHead({
title: () => hasValidDomain.value ? domain.value : 'page-speed.dev',
})
useServerHead({
htmlAttrs: {
lang: 'en',
class: 'bg-[#212121]',
},
bodyAttrs: {
// @ts-expect-error allow escaping focus from tooltips on mobile
tabindex: '0',
},
link: [
{
rel: 'canonical',
href: canonicalURL.value,
},
{
rel: 'apple-touch-icon',
sizes: '180x180',
href: '/apple-touch-icon.png',
},
{
rel: 'manifest',
href: '/site.webmanifest',
},
{
rel: 'mask-icon',
href: '/safari-pinned-tab.svg',
color: '#23c55e',
},
],
meta: [
{
name: 'msapplication-TileColor',
content: '#23c55e',
},
{
name: 'theme-color',
content: '#ffffff',
},
],
})
useServerSeoMeta({
ogTitle: hasValidDomain.value ? domain.value : 'page-speed.dev',
ogUrl: canonicalURL.value,
twitterTitle: hasValidDomain.value ? domain.value : 'page-speed.dev',
twitterCard: 'summary_large_image',
twitterSite: '@danielcroe',
})
if (!hasValidDomain.value) {
defineOgImageComponent('Home')
useServerSeoMeta({
description: 'See and share Core Web Vitals and PageSpeed Insights results simply and easily.',
ogDescription: 'See and share Core Web Vitals and PageSpeed Insights results simply and easily.',
})
}
else {
if (!crux.value)
await lighthouseRefresh()
if (crux.value || lighthouse.value) {
const description = crux.value
? `Core Web Vitals ${crux.value?.cwv ? 'pass' : 'fail'}: `
+ `LCP: ${crux.value?.lcp.caption} | `
+ `CLS: ${crux.value?.cls.caption} | `
+ `INP: ${crux.value?.inp.caption}`
: `Performance: ${lighthouse.value!.performance} | `
+ `Accessibility: ${lighthouse.value!.accessibility} | `
+ `Best Practices: ${lighthouse.value!.bestPractices} | `
+ `SEO: ${lighthouse.value!.seo}`
useServerSeoMeta({
description,
ogDescription: description,
})
defineOgImageComponent('Lighthouse', {
domain: domain.value,
})
}
}
</script>
<template>
<div
class="md:pl-[5vw] font-sans text-white min-h-screen min-h-dvh min-w-screen flex flex-col items-start justify-start"
>
<div
v-if="showConfetti"
v-confetti
/>
<div class="flex flex-col justify-start mt-4 sm:mt-8 md:my-12 p-4 gap-8 md:gap-12 flex-grow max-w-full">
<div class="flex flex-row gap-4 text-white text-3xl md:text-5xl">
<span class="text-green-400">»</span>
<TheDomainForm
v-model:editing="editing"
:domain="domain"
/>
</div>
<template v-if="!editing && domain">
<template v-if="cruxStatus === 'pending' || lighthouseStatus === 'pending' || crux || lighthouse">
<div class="flex flex-row flex-wrap gap-4 lg:flex-row justify-around w-full">
<CoreWebVitals
v-if="cruxStatus === 'pending' || crux"
:pass="crux?.cwv"
:lcp="crux?.lcp"
:cls="crux?.cls"
:inp="crux?.inp"
:loading="cruxStatus === 'pending'"
size="normal"
show-p75
/>
<template v-else>
<ProgressRing
size="normal"
:value="lighthouseStatus !== 'pending' && lighthouse?.performance"
caption="performance"
/>
<ProgressRing
size="normal"
:value="lighthouseStatus !== 'pending' && lighthouse?.accessibility"
caption="accessibility"
/>
<ProgressRing
size="normal"
:value="lighthouseStatus !== 'pending' && lighthouse?.bestPractices"
caption="best practices"
/>
<ProgressRing
size="normal"
:value="lighthouseStatus !== 'pending' && lighthouse?.seo"
caption="SEO"
/>
</template>
</div>
<LighthouseTable
v-if="cruxStatus === 'pending' || crux"
:loading="lighthouseStatus === 'pending' || !lighthouse"
v-bind="lighthouse || {}"
/>
</template>
<div v-else-if="domain">
No results could be fetched. Is it a valid domain?
</div>
<TheShareLink
v-if="crux || lighthouse"
:domain="domain"
:type="crux ? 'crux' : 'pagespeed-insights'"
:timestamp="crux && cruxStatus !== 'pending' ? crux.timestamp : lighthouse && lighthouseStatus !== 'pending' ? lighthouse.timestamp : undefined"
/>
<details
v-if="crux || lighthouse"
class="max-w-[500px] text-gray-400"
>
<summary class="cursor-pointer">
about these results
</summary>
<p class="mt-4">
This tool uses the <a
class="underline hover:text-green-400 focus:text-green-400 active:text-green-400"
href="https://developers.google.com/web/tools/chrome-user-experience-report"
>Chrome User Experience
Report</a> and
<a
class="underline hover:text-green-400 focus:text-green-400 active:text-green-400"
href="https://developers.google.com/web/tools/lighthouse"
>Lighthouse</a> APIs to fetch and display Core
Web
Vitals and PageSpeed Insights results.
</p>
<p class="mt-4">
Results are cached for a minimum of one day plus the first following request.
</p>
<p class="mt-4">
The numbers you see are for <b>mobile devices</b>.
</p>
</details>
</template>
</div>
<footer class="mt-auto p-3 text-gray-400">
<a
class="underline hover:text-green-400 focus:text-green-400 active:text-green-400"
href="https://github.com/danielroe/page-speed.dev"
>source</a>
·
made with ❤️ by <a
class="underline hover:text-green-400 focus:text-green-400 active:text-green-400"
href="https://roe.dev"
>
daniel roe
</a>
</footer>
</div>
</template>