From 5d94d966fec97d7b4dfa841259f87fa5dbcb831a Mon Sep 17 00:00:00 2001 From: TatriX Date: Wed, 15 Jan 2020 13:34:45 +0100 Subject: [PATCH] feat: Improve before_mount docs and do runtime debug check See commit code for explanation. --- src/app.rs | 3 +++ src/app/builder/before_mount.rs | 10 ++++++++-- src/virtual_dom/node.rs | 7 +++++++ src/virtual_dom/node/el.rs | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index b3dcc3b19..57ec3a2ec 100644 --- a/src/app.rs +++ b/src/app.rs @@ -246,6 +246,9 @@ impl + 'static, GMs: 'static> App { // not entirely correct). // TODO: 1) Please refer to [issue #277](https://github.com/seed-rs/seed/issues/277) let mut dom_nodes: El = (&self.cfg.mount_point).into(); + if cfg!(debug_assertions) { + dom_nodes.warn_about_script_tags(); + } dom_nodes.strip_ws_nodes_from_self_and_children(); // Replace the root dom with a placeholder tag and move the children from the root element diff --git a/src/app/builder/before_mount.rs b/src/app/builder/before_mount.rs index ed17a42b4..65301cbe8 100644 --- a/src/app/builder/before_mount.rs +++ b/src/app/builder/before_mount.rs @@ -83,8 +83,14 @@ impl BeforeMount { /// mount_point("another_id") /// /// // argument is `HTMLElement` - /// // NOTE: Be careful with mounting into body, - /// // it can cause hard-to-debug bugs when there are other scripts in the body. + /// + /// // NOTE: Be careful with mounting into body! + /// // If you render directly into document.body, you risk collisions + /// // with scripts that do something with it (e.g. Google Font Loader or + /// // third party browser extensions) which produce very weird and hard + /// // to debug errors in production. + /// // (from https://github.com/facebook/create-react-app/issues/1568) + /// /// mount_point(seed::body()) /// /// // argument is `Element` diff --git a/src/virtual_dom/node.rs b/src/virtual_dom/node.rs index cdc6708ec..a0e65531d 100644 --- a/src/virtual_dom/node.rs +++ b/src/virtual_dom/node.rs @@ -168,6 +168,13 @@ impl Node { Node::Empty => (), } } + + #[cfg(debug_assertions)] + pub fn warn_about_script_tags(&self) { + if let Node::Element(e) = self { + e.warn_about_script_tags(); + } + } } impl MessageMapper for Node { diff --git a/src/virtual_dom/node/el.rs b/src/virtual_dom/node/el.rs index 263abf2fa..2291d184b 100644 --- a/src/virtual_dom/node/el.rs +++ b/src/virtual_dom/node/el.rs @@ -211,6 +211,24 @@ impl El { .collect() } + #[cfg(debug_assertions)] + /// Warn user about potential bugs when having scripts and `Takeover` mount type. + pub fn warn_about_script_tags(&self) { + let script_found = match &self.tag { + Tag::Script => true, + Tag::Custom(tag) if tag == "script" => true, + _ => false, + }; + if script_found { + error!("Script tag found inside mount point! \ + Please check https://docs.rs/seed/latest/seed/app/builder/struct.Builder.html#examples"); + } + + for child in &self.children { + child.warn_about_script_tags(); + } + } + /// Remove websys nodes. pub fn strip_ws_nodes_from_self_and_children(&mut self) { self.node_ws.take();