-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically added scripts are always async #3757
Comments
@mcmah309
|
Thanks for the information.
Is this behavior documented somewhere? If not, why does it only have access to some and which ones?
Unfortunately this still results in a similar error: Uncaught ReferenceError: hljs is not defined
at <anonymous>:1:1
rust.min.js:28 Uncaught ReferenceError: hljs is not defined
at rust.min.js:28:2
at rust.min.js:28:35 But the raw html works fine if posted into regular Note, in my original code I use:
instead of |
The original solution with script tags seems like the same issue as #3756. The tag order is reversed, so the script that highlights the code is loaded before the library. The alternative solution with eval should work as long as the script has loaded before the eval runs. use dioxus::prelude::*;
fn main() {
dioxus::launch(App);
}
#[component]
fn App() -> Element {
rsx! {
BugExample {}
}
}
#[component]
pub fn BugExample() -> Element {
let code = r#"
use dioxus::prelude::*;
#[component]
pub fn Example() -> Element {}
"#;
let mut loaded = use_signal(|| 0);
use_effect(move || {
if loaded() > 1 {
document::eval("hljs.highlightAll();");
}
});
rsx! {
div {
pre {
code { class: "language-rust", {code} }
}
script { src: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/rust.min.js", onload: move |_| {
loaded += 1;
} }
script { src: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js", onload: move |_| {
loaded += 1;
} }
document::Link {
href: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css",
rel: "stylesheet",
}
}
}
} |
Yes the solution is the same, but the issue is not as far as I can tell.
The code actually takes this into account and reverses the order - see
Is there a dom event that triggers this? Or what do you mean by "mutations are done" As a far as I can tell, this issue should not be closed. The head script tags are "being applied correctly" (reverse order accounted for), but the mentioned error persists. If I apply the raw tags to a separate page (just copying and pasting the resulting html from the browser), It works. |
Scripts that are added dynamically act differently than scripts that are present in the initial HTML. Dioxus is running something like this: var script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js';
document.head.appendChild(script);
var script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/rust.min.js';
document.head.appendChild(script);
var script = document.createElement('script');
script.textContent = "hljs.highlightAll();"
document.head.appendChild(script); If you disable the head components and run that script in the console, you should see the same error. Your original code does work if you run it in fullstack because the head elements are present in the initial HTML. It looks like there are html attributes you can add to the script like |
Makes sense thanks!
That is interesting, since doing it this way is the same as |
I think this is the same fundamental design choice presented in #3756. The Looking at other frameworks: React's head script requires you always set |
I think requiring the same thing would make sense. Having consistent behavior for SSR too would also be a less of a headache if anyone ever ran into this. It also might make sense to add defining Also given
|
Problem
Scripts are not being executed correctly
Steps To Reproduce
e.g.
Results in
on web. And the formatting is not applied to the code block.
Running
hljs.highlightAll();
Directly in the console succeeds and the code block is formatted correctly.
Copying and pasting the html directly from the browser to a new html page and opening succeeds.
Thus considering this and looking at the error. This is a Dioxus specific error.
Expected behavior
The formatted code block is displayed
Screenshots
Environment:
Questionnaire
I'm interested in fixing this myself but don't know where to start.
The text was updated successfully, but these errors were encountered: