Skip to content

Commit 4450977

Browse files
committed
fix: comment not update when route changes
1 parent 97a8468 commit 4450977

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/assets/ts/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ export const isMobile = (): boolean => is_mobile({ tablet: true });
3939
* @param src - The source URL of the script to load
4040
* @returns A promise that resolves when the script is loaded
4141
*/
42-
export const loadJS = (src: string): Promise<unknown> =>
42+
export const loadJS = (src: string): Promise<HTMLScriptElement> =>
4343
new Promise((resolve, reject) => {
4444
let script = document.createElement("script");
4545
script.type = "text/javascript";
4646
script.src = src;
4747
document.body.appendChild(script);
4848

49-
script.onload = resolve;
49+
script.onload = () => resolve(script);
5050
script.onerror = reject;
5151
});
5252

src/components/Comment.vue

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ import { onMounted } from "vue";
33
import { loadJS } from "@/assets/ts/utils";
44
55
import "@/assets/css/twikoo.css";
6+
import router from "@/router";
7+
8+
let script: HTMLScriptElement | null = null;
69
710
const URL = "https://cdn.jsdelivr.net/npm/twikoo@1.6.39/dist/twikoo.nocss.js";
811
const ENV_ID = "https://twikoo-blog.xecades.xyz/";
912
10-
onMounted(async () => {
11-
await loadJS(URL);
13+
const loadTwikoo = async () => {
14+
if (script) script.remove();
15+
script = await loadJS(URL);
1216
1317
// @ts-expect-error
1418
const twikoo = window.twikoo;
1519
await twikoo.init({ envId: ENV_ID, el: "#twikoo" });
16-
});
20+
};
21+
22+
router.afterEach(loadTwikoo);
23+
onMounted(loadTwikoo);
1724
</script>
1825

1926
<template>
@@ -39,7 +46,7 @@ onMounted(async () => {
3946
4047
.tk-extras
4148
display: none;
42-
49+
4350
.tk-input textarea
4451
padding: 0.7rem 1rem;
4552
min-height: 170px !important;
@@ -60,7 +67,7 @@ onMounted(async () => {
6067
6168
.tk-actions
6269
font-size: 0.8rem;
63-
70+
6471
a
6572
color: #777;
6673

src/components/Content.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ onUpdated(registerAnchor);
8080
<Metadata
8181
:breadcrumb="meta.breadcrumb"
8282
:type="meta.type"
83-
:key="meta.attr.title"
83+
:key="meta.pathname"
8484
/>
8585

8686
<main class="markdown">

0 commit comments

Comments
 (0)