File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed
Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff 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+ } ;
You can’t perform that action at this time.
0 commit comments