Skip to content

Commit

Permalink
perf: don't fetch hitokoto util hovering avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Renovamen committed Mar 29, 2022
1 parent f9187c2 commit 487c439
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default defineUserConfig<GungnirThemeOptions>({
repo: "Renovamen/vuepress-theme-gungnir",
docsDir: "docs",

hitokoto: "https://v1.hitokoto.cn?c=d&c=i", // enable hitokoto (一言) or not?
hitokoto: "https://v1.hitokoto.cn?c=i", // enable hitokoto (一言) or not?

// personal information
personalInfo: {
Expand Down
15 changes: 9 additions & 6 deletions packages/theme/src/client/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
class="hero-avatar hide-on-mobile"
:src="$withBase(personalInfo.avatar)"
alt="hero"
@mouseover="fetchHitokoto"
/>

<div v-if="hitokotoAPI" class="hero-bubble">
<div class="hero-bubble__body">
<p id="hitokoto">正在加载一言...</p>
<p>{{ hitokotoText }}</p>
</div>
<div class="hero-bubble__tile" />
</div>
Expand Down Expand Up @@ -73,24 +74,26 @@ const scrollToPost = () => {
// -------- Hitokoto --------
const hitokotoAPI = themeLocale.value.hitokoto;
const hitokotoText = ref("正在加载一言...");
let hasFetchedHitokoto = false;
const fetchHitokoto = () => {
if (!hitokotoAPI || hasFetchedHitokoto) return;
hasFetchedHitokoto = true;
let api = hitokotoAPI;
api = typeof api === "string" ? api : "https://v1.hitokoto.cn";
fetch(api)
.then((response) => response.json())
.then((data) => {
const hitokoto = document.querySelector("#hitokoto") as HTMLElement;
hitokoto.innerHTML = data.hitokoto;
})
.then((data) => (hitokotoText.value = data.hitokoto))
.catch((error) => {
console.log(`Get ${api} error: `, error);
});
};
onMounted(() => {
if (hitokotoAPI) fetchHitokoto();
if (bgImages && bgImages.length > 0)
bgImageID.value = Math.floor(Math.random() * bgImages.length);
});
Expand Down

0 comments on commit 487c439

Please sign in to comment.