Skip to content

Commit 15dad43

Browse files
committed
fix: mirrorBaseUrl
1 parent 662165a commit 15dad43

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

docs/.vitepress/plugins/mirror.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ export const transformHtml = (code: string) => {
158158
if (!code.includes('/assets/')) return;
159159
// 注意: 如果使用 htmlparser2+dom-serializer, 当 md 文件包含 `<<n` 将出现 Hydration mismatches 错误
160160
const doc = new Parser().parseFromString(code, 'text/html');
161-
const meta = doc.createElement('meta');
162-
meta.name = 'version';
163-
meta.content = selfPkg.version;
164-
doc.head.insertBefore(meta, doc.head.firstChild);
161+
const script = doc.createElement('script');
162+
script.textContent = `;(${rewriteAppendChild})(${JSON.stringify(mirrorBaseUrl)});`;
163+
doc.head.insertBefore(script, doc.head.firstChild);
165164
Object.entries({
166165
link: 'href',
167166
script: 'src',
@@ -185,3 +184,17 @@ export const transformHtml = (code: string) => {
185184

186185
return doc.documentElement.outerHTML;
187186
};
187+
188+
const rewriteAppendChild = (base: string) => {
189+
const rawAppendChild = Node.prototype.appendChild;
190+
Node.prototype.appendChild = function <T extends Node>(node: T): T {
191+
if (
192+
node instanceof HTMLLinkElement &&
193+
node.rel === 'prefetch' &&
194+
node.href.startsWith('/assets/')
195+
) {
196+
node.href = base + node.href;
197+
}
198+
return rawAppendChild(node);
199+
};
200+
};

0 commit comments

Comments
 (0)