Skip to content

Commit 9c7b4e9

Browse files
committed
0602更新
1. 博客列表页面能够有更好的SSR优化 2. 调整了一下OG图片的渲染
1 parent 0471488 commit 9c7b4e9

File tree

8 files changed

+102
-75
lines changed

8 files changed

+102
-75
lines changed

components/subpage/blog/RandomSentence.vue

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,56 @@
1212
</div>
1313
</template>
1414

15-
<script lang="ts" setup>
16-
let sentence = ref()
17-
let displayText = ref('')
18-
let fullText = ref('')
19-
let isTyping = ref(false) // 添加打字状态控制
15+
<script setup>
16+
const sentence = ref()
17+
const displayText = ref("")
18+
const fullText = ref("")
19+
const isTyping = ref(false)
2020
21-
// 改进的打字效果函数
21+
// 打字效果函数
2222
const typeText = async () => {
2323
isTyping.value = true
24-
displayText.value = ''
25-
26-
// 为每个字符设置随机的打字间隔,模拟真实打字效果
24+
displayText.value = ""
25+
2726
for (let i = 0; i < fullText.value.length; i++) {
28-
if (!isTyping.value) break // 允许中断打字
27+
if (!isTyping.value) break
2928
displayText.value += fullText.value[i]
30-
// 根据标点符号调整停顿时间
3129
const delay = fullText.value[i].match(/[,。!?、]/) ? 300 : 50
32-
await new Promise(resolve => setTimeout(resolve, delay))
30+
await new Promise((resolve) => setTimeout(resolve, delay))
3331
}
34-
32+
3533
isTyping.value = false
3634
}
3735
38-
const getSentence = async () => {
39-
const res = await fetch("https://open.saintic.com/api/sentence/", {
40-
method: "GET",
41-
})
42-
const data = await res.json()
43-
sentence.value = data.data
44-
fullText.value = data.data.sentence
45-
await typeText()
46-
}
36+
// 获取随机句子
37+
const { data } = await useAsyncData(
38+
`sentence-${Math.floor(Date.now() / 60000)}`,
39+
async () => {
40+
const res = await $fetch("https://open.saintic.com/api/sentence/")
41+
return {
42+
sentence: res?.data?.sentence || "",
43+
author: res?.data?.author || "",
44+
name: res?.data?.name || "",
45+
}
46+
},
47+
{
48+
default: () => null,
49+
server: false
50+
}
51+
)
4752
48-
onMounted(() => {
49-
getSentence()
53+
// 监听数据变化并执行打字效果
54+
watch(data, async (newData) => {
55+
if (newData?.sentence) {
56+
sentence.value = newData
57+
fullText.value = newData.sentence
58+
await typeText()
59+
}
60+
}, { immediate: true })
61+
62+
// 组件卸载时停止打字
63+
onBeforeUnmount(() => {
64+
isTyping.value = false
5065
})
5166
</script>
5267
@@ -59,6 +74,7 @@ onMounted(() => {
5974
height: 45vh;
6075
background: rgb(var(--mdui-color-surface-container-highest));
6176
transform: translateY(calc(-1 * var(--inline-padding)));
77+
6278
.inner {
6379
width: 70%;
6480
min-width: 370px;
@@ -68,28 +84,32 @@ onMounted(() => {
6884
flex-direction: column;
6985
align-items: center;
7086
box-sizing: border-box;
87+
7188
.sentence {
7289
font-size: 1.5rem;
73-
font-weight: 500;
90+
line-height: 1.6;
91+
margin-bottom: 1rem;
92+
min-height: 2em;
93+
cursor: pointer;
7494
text-align: center;
75-
margin-bottom: 10px;
76-
transform: translateX(7px);
95+
font-weight: bold;
7796
}
97+
7898
.source {
79-
width: 100%;
8099
font-size: 1rem;
81-
font-weight: 400;
100+
opacity: 0.7;
82101
text-align: center;
83-
margin-top: 10px;
102+
}
103+
104+
.cursor {
105+
animation: blink 1s infinite;
106+
color: var(--mdui-color-primary);
84107
}
85108
}
86109
}
87-
.cursor {
88-
animation: blink 0.8s infinite;
89-
}
90110
91111
@keyframes blink {
92-
0%, 100% { opacity: 1; }
93-
50% { opacity: 0; }
112+
0%, 50% { opacity: 1; }
113+
51%, 100% { opacity: 0; }
94114
}
95115
</style>

content.config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineCollection, defineContentConfig, z } from "@nuxt/content"
22
import { asSeoCollection } from "@nuxtjs/seo/content"
33

4+
45
export default defineContentConfig({
56
collections: {
67
blog: defineCollection(
@@ -12,12 +13,6 @@ export default defineContentConfig({
1213
}),
1314
}),
1415
),
15-
content: defineCollection(
16-
asSeoCollection({
17-
type: 'page',
18-
source: '**/*.md',
19-
}),
20-
),
2116
indexActions: defineCollection({
2217
type: "data",
2318
source: "data/index/actions.yml",

layouts/Default.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424

2525
<script setup lang="ts">
2626
const [drawerStatus, toggleDrawer] = useToggle(false)
27+
28+
useHead({
29+
htmlAttrs: {
30+
lang: "zh-CN",
31+
},
32+
link: [
33+
{
34+
rel: "icon",
35+
type: "image/png",
36+
href: "/favicon.png",
37+
},
38+
],
39+
})
40+
41+
useSeoMeta({
42+
ogImage: "/__og-image__/static/og.png",
43+
})
2744
</script>
2845

2946
<style lang="less" scoped>

nuxt.config.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,16 @@ export default defineNuxtConfig({
5858
prerender: {
5959
failOnError: true,
6060
crawlLinks: true,
61-
routes:[
62-
"/",
63-
"/blog/",
64-
"/search",
65-
"/rss",
66-
]
61+
routes: ["/", "/blog/", "/search", "/rss"],
6762
},
6863
},
6964
app: {
7065
baseURL: "/",
7166
// cdnURL: "https://cdn.shaly.sdutacm.cn/csbig/",
7267
buildAssetsDir: "nuxt_assets",
7368
head: {
69+
htmlAttrs: { lang: "zh-CN" },
70+
link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
7471
noscript: [{ innerHTML: "JavaScript is required" }],
7572
},
7673
},
@@ -89,9 +86,10 @@ export default defineNuxtConfig({
8986
typeCheck: true,
9087
},
9188
site: {
92-
url: process.env.NODE_ENV === 'production'
93-
? 'https://csbigcaptain.github.io'
94-
: 'http://localhost:3000',
89+
url:
90+
process.env.NODE_ENV === "production"
91+
? "https://csbigcaptain.github.io"
92+
: "http://localhost:3000",
9593
name: "CSBigCaptain Blog",
9694
},
9795
ssr: true,
@@ -104,7 +102,6 @@ export default defineNuxtConfig({
104102
ogImage: {
105103
// zeroRuntime: true,
106104
googleFontMirror: true,
107-
fonts: ["Noto+Sans+SC"],
108105
},
109106
robots: {
110107
enabled: false,

pages/Search.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
import { useFuse } from "@vueuse/integrations/useFuse";
7373
import "@mdui/icons/chevron-right";
7474
75-
useHead({
75+
useSeoMeta({
7676
title: "搜索 - CSBigCaptain Blog",
7777
});
7878

pages/blog/[...slug].vue

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
<script setup lang="ts">
2-
const route = useRoute();
2+
const route = useRoute()
33
44
const { data: post } = await useAsyncData(`blog-${route.path}`, () => {
5-
return queryCollection("blog").path(route.path).first();
6-
});
5+
return queryCollection("blog").path(route.path).first()
6+
})
77
8-
useHead({
9-
title: post.value?.title || "CSBigCaptain Blog",
10-
meta: [
11-
{ name: "description", content: post.value?.description || "" },
12-
{
13-
name: "og:title",
14-
content: post.value?.title + "-- CSBigCaptain Blog",
15-
},
16-
{ name: "og:type", content: "article" },
17-
],
18-
});
8+
useSeoMeta({
9+
title: (post.value?.title || "Blog") + " - CSBigCaptain Blog",
10+
ogTitle: (post.value?.title || "Blog") + " - CSBigCaptain Blog",
11+
description: "The introduction to CSBigCaptain Blog.",
12+
ogDescription: "The introduction to CSBigCaptain Blog.",
13+
ogUrl: "https://csbigcaptain.github.io" + route.path,
14+
})
1915
</script>
2016

2117
<template>

pages/blog/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ useSeoMeta({
55
description: "CSBigCaptain Blog list.",
66
ogDescription: "CSBigCaptain Blog List.",
77
ogUrl: 'https://csbigcaptain.github.io/blog',
8-
ogImage: "/__og-image__/static/og.png",
98
});
109
defineOgImageComponent('Nuxt',{
1110
title: "Blog List",

pages/index.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ useSeoMeta({
55
description: "The introduction to CSBigCaptain Blog.",
66
ogDescription: "The introduction to CSBigCaptain Blog.",
77
ogUrl: 'https://csbigcaptain.github.io/',
8-
ogImage: "/__og-image__/static/og.png",
98
});
109
defineOgImageComponent('Nuxt',{
11-
title: "首页",
12-
headline: "CSBigCaptain Blog",
10+
title: "CSBigCaptain Blog",
11+
headline: "WELCOME TO",
12+
description: "A open-source Git-CMS blog system driven by Nuxt 3.",
1313
})
1414
1515
const {data: main} = await useAsyncData('indexMain', () => {
@@ -40,15 +40,18 @@ const data = {
4040
<span class="text">{{ data.main.text }}</span>
4141
</h1>
4242
<div class="actions">
43-
<mdui-button
43+
<NuxtLink
4444
v-for="item in data.actions"
4545
:key="item.text"
4646
:href="item.link"
47-
:variant="item.variant"
48-
:icon="item.icon"
49-
:end-icon="item.endIcon"
50-
>{{ item.text }}</mdui-button
5147
>
48+
<mdui-button
49+
:variant="item.variant"
50+
:icon="item.icon"
51+
:end-icon="item.endIcon"
52+
>{{ item.text }}
53+
</mdui-button>
54+
</NuxtLink>
5255
</div>
5356
</div>
5457
</div>

0 commit comments

Comments
 (0)