-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
External script doesn't always load when added to Nuxt page via "head()" #5052
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
Comments
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
I also faced this issue trying to load the Stripe API (v3) in a page, often it'll say Stripe library isn't loaded. |
Apologies for not replying in time to prevent the bot from closing this issue. I just updated my test repo to Nuxt 2.6.1 and I'm still experiencing the problems described above. |
I'm having similar issue when trying to load Youtube API on one particular page. I've also tried using npm vue-script2 to have more control over external scripts loading. on mounted() of the page
getting the below error It could technically be solved if I load the script across all pages. However, I don't want user to download unnecessarily. |
Does anyone have a solution to this problem? |
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
I have the same problem with Stripe and Google maps :p, anyone have a solution to this problem ? Nuxt version: 2.6.2 Example to reproduce:
|
@DidelotK All versions are affected, AFAIK. |
@markplewis and others: It all works as expected and has to do with how html/JS works, not Nuxt. In In Basically you have to understand the difference between script added programatically (through DOM) and script that is in HTML of the page already. Former will load asynchronously while latter will load synchronously. So to make this work consistently, rather than using |
Thanks @rchl. This is the simplest example that I could come up with and it seems to be working: export default {
mounted() {
if (!process.server && !window.jQuery) {
const script = document.createElement("script");
script.onload = this.onScriptLoaded;
script.type = "text/javascript";
script.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js";
document.head.appendChild(script);
} else {
this.onScriptLoaded();
}
},
methods: {
onScriptLoaded(event = null) {
if (event) {
console.log("Was added");
} else {
console.log("Already existed");
}
console.log(window.jQuery);
window.jQuery("h1").append(` <span>(CDN script has loaded)</span>`);
}
}
} |
Looks good to me. Something like BTW. |
I created this utility function to load external JS file as Nuxt page's head property doesn't work. In my case, It only adds <script> to if the page is requested directly, not as a route-view component. loadJsFile(url, id, onLoadedCallback, defer, async) {
if (process.browser) {
let test = document.getElementById(id);
if (test) return onLoadedCallback();
else {
const script = document.createElement("script")
script.src = url
script.id = id
script.onload = onLoadedCallback
script.type = "text/javascript"
script.defer = defer == true || defer == undefined ? true : false
script.async = async == true || defer == undefined ? true : false
document.head.appendChild(script)
}
}
} |
Thanks to vue-meta, this should be way easier now. I've wrote a bit about my preferred way in this article. |
This solve my issue: https://vue-meta.nuxtjs.org/api/#callback You only need to ensure to add
|
This works for me, but not when loading the page for the first time. It works only when navigating to the page. |
I know this thread is closed, but I may have found a new solution. I just wrote a script tag into template of the specific page body (anyway for external links like hubspot, it needs to come after the div tag of element). So if you have a specific page where the external embeded code should be seen only just add it into the template and nuxt will make sure to load it after the HTML element is rendered (with use of v-if for example). Works only in NUXT for me. |
replace |
EDIT: It's the opposite. |
but using |
If you reload the page then the script loads synchronously so it's already loaded. Now I'm not sure if that's a bug in VueMeta or expected behavior but I guess you can condition the logic to set Removing |
@rchl And set really appreciate your answer |
https://github.com/nuxt/nuxt.js/blob/dec8f99fc3ffbdcdf731b671b40ef3fd199e7b6b/packages/vue-renderer/src/renderers/spa.js https://vue-meta.nuxtjs.org/api/#callback In case I misunderstood something, please tell me where I got wrong. really appreciate that. |
Ops. Sorry, I got totally confused. Yes, Nuxt used |
i'm attempting to use VueMeta to inject an external library into a component and am experiencing an issue where, in some cases, while navigating between pages, the
it seems to only happen when navigating from another "SPA mode" page that has this component on it... but not in all cases. |
Hello @broox Did you ever locate a solution to this? I only ask because I'm experiencing this random behaviour too. At first I could only recreate this bug on Safari, but have now concluded it's browser-agnostic. For my environment it seems to only occur once in roughly 50 page loads. And when it does occur it's always when transitioning between two pages who both refer to the same 3rd-party JavaScript file. And it's the same outcome that you're experiencing (in that the Currently using Nuxt v2.15.8 |
I still have problems with this with Nuxt 3.4.3 My external scripts are written in |
Version
v2.4.3
Reproduction link
https://github.com/markplewis/nuxt-external-resource-loading-issue
Steps to reproduce
Clone my demo repo and then do the following:
Case 1
npm run dev
and open it up in your browser.Case 2
npm run dev
and open it up in your browser.<h1>
element.<h1>
.<h1>
.<h1>
element.<h1>
.What is expected ?
I'm trying to load an external script into a Nuxt page via the page's
head
:I expect the script to load and be available every time the page loads, regardless of whether it was served directly from the server or rendered on the client-side.
What is actually happening?
Unfortunately, the external script doesn't seem to load when the page is server-rendered - it only seems to work when the page is client-rendered. I discovered the following issues, but neither of them have helped me solve this problem:
In that first issue, manniL said the following:
What does "you can't ensure the readiness easily" mean? I'd prefer not to move this script into my layout because it will only be needed for one particular page, and I don't want to force users to download unnecessary resources.
I can avoid errors (see test case 1, above) by wrapping my calls to
window.jQuery
, like this:But this leaves me with the problem that I described in test case 2 (see above).
The text was updated successfully, but these errors were encountered: