mirror of
https://github.com/chenasraf/leptos.git
synced 2026-05-18 01:49:06 +00:00
Working on disposing metadata tags
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use cfg_if::cfg_if;
|
||||
use leptos::{Scope, component, IntoView};
|
||||
use leptos::{Scope, component, IntoView, on_cleanup};
|
||||
use std::{rc::Rc, cell::{RefCell, Cell}, collections::HashMap};
|
||||
|
||||
use crate::{use_head, TextProp};
|
||||
@@ -164,12 +164,19 @@ pub fn Meta(
|
||||
}
|
||||
|
||||
// add to head
|
||||
document()
|
||||
.query_selector("head")
|
||||
.unwrap_throw()
|
||||
.unwrap_throw()
|
||||
.append_child(&el)
|
||||
.unwrap_throw();
|
||||
let head = document()
|
||||
.query_selector("head")
|
||||
.unwrap_throw()
|
||||
.unwrap_throw();
|
||||
head.append_child(&el)
|
||||
.unwrap_throw();
|
||||
|
||||
on_cleanup(cx, {
|
||||
let el = el.clone();
|
||||
move || {
|
||||
head.remove_child(&el);
|
||||
}
|
||||
});
|
||||
|
||||
// add to meta tags
|
||||
meta_tags.els.borrow_mut().insert(id, (None, Some(el.unchecked_into())));
|
||||
|
||||
@@ -61,7 +61,6 @@ pub fn Stylesheet(
|
||||
|
||||
let meta = use_head(cx);
|
||||
|
||||
// TODO I guess this will create a duplicated <link> when hydrating
|
||||
let existing_el = {
|
||||
let els = meta.stylesheets.els.borrow();
|
||||
let key = (id.clone(), href.clone());
|
||||
@@ -82,15 +81,27 @@ pub fn Stylesheet(
|
||||
el.set_attribute("id", id_val).unwrap_throw();
|
||||
}
|
||||
el.set_attribute("href", &href).unwrap_throw();
|
||||
document()
|
||||
.query_selector("head")
|
||||
.unwrap_throw()
|
||||
.unwrap_throw()
|
||||
let head = document().head().unwrap_throw();
|
||||
head
|
||||
.append_child(el.unchecked_ref())
|
||||
.unwrap_throw();
|
||||
|
||||
el
|
||||
});
|
||||
|
||||
on_cleanup(cx, {
|
||||
let el = el.clone();
|
||||
let els = meta.stylesheets.els.clone();
|
||||
let href = href.clone();
|
||||
let id = id.clone();
|
||||
move || {
|
||||
leptos::log!("removing stylesheet");
|
||||
let head = document().head().unwrap_throw();
|
||||
head.remove_child(&el);
|
||||
els.borrow_mut().remove(&(id, href));
|
||||
}
|
||||
});
|
||||
|
||||
meta.stylesheets
|
||||
.els
|
||||
.borrow_mut()
|
||||
|
||||
@@ -96,6 +96,11 @@ pub fn Title(
|
||||
) -> impl IntoView {
|
||||
let meta = use_head(cx);
|
||||
|
||||
let prev = meta.title.clone();
|
||||
on_cleanup(cx, move || {
|
||||
leptos::log!("cleaning up <Title/>");
|
||||
});
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(feature = "csr", feature = "hydrate"))] {
|
||||
if let Some(formatter) = formatter {
|
||||
@@ -106,24 +111,40 @@ pub fn Title(
|
||||
}
|
||||
|
||||
let el = {
|
||||
let el_ref = meta.title.el.borrow_mut();
|
||||
let mut el_ref = meta.title.el.borrow_mut();
|
||||
let el = if let Some(el) = &*el_ref {
|
||||
let prev_text = el.inner_text();
|
||||
on_cleanup(cx, {
|
||||
let el = el.clone();
|
||||
move || {
|
||||
el.set_text(&prev_text);
|
||||
}
|
||||
});
|
||||
|
||||
el.clone()
|
||||
} else {
|
||||
match document().query_selector("title") {
|
||||
Ok(Some(title)) => title.unchecked_into(),
|
||||
_ => {
|
||||
let el = document().create_element("title").unwrap_throw();
|
||||
document()
|
||||
.query_selector("head")
|
||||
.unwrap_throw()
|
||||
.unwrap_throw()
|
||||
.append_child(el.unchecked_ref())
|
||||
let head = document().head().unwrap_throw();
|
||||
head.append_child(el.unchecked_ref())
|
||||
.unwrap_throw();
|
||||
|
||||
on_cleanup(cx, {
|
||||
let el = el.clone();
|
||||
move || {
|
||||
head.remove_child(&el);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
el.unchecked_into()
|
||||
}
|
||||
}
|
||||
};
|
||||
*el_ref = Some(el.clone().unchecked_into());
|
||||
|
||||
el
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user