File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { type Accessor , createEffect } from 'solid-js' ;
1+ import { type Accessor , createEffect , onCleanup } from 'solid-js' ;
22
33export function makeMeta ( name : string , content : Accessor < string > ) {
4- let metaElement = document . querySelector < HTMLMetaElement > ( `meta[name="${ name } "]` ) ;
5- if ( ! metaElement ) {
6- metaElement = document . createElement ( 'meta' ) ;
7- metaElement . name = name ;
8- document . head . appendChild ( metaElement ) ;
9- }
4+ let metaElement : HTMLMetaElement | undefined = document . createElement ( 'meta' ) ;
5+ metaElement . name = name ;
6+ document . head . appendChild ( metaElement ) ;
7+ onCleanup ( ( ) => {
8+ if ( ! metaElement ) return ;
9+ document . head . removeChild ( metaElement ) ;
10+ metaElement = undefined ;
11+ } ) ;
1012 // Meta 组件不稳定,所以需要手动更新
1113 createEffect ( ( ) => {
14+ if ( ! metaElement ) return ;
1215 metaElement . content = content ( ) ;
1316 } ) ;
1417}
You can’t perform that action at this time.
0 commit comments