external scripts does not load on the client side

NodeBB Development
  • Hello. I have a plugin with this simple client side script

    $(document).ready(function () {
    	if(!window['firebase']) {
    		const script = document.createElement('script');
    		script.src = "https://www.gstatic.com/firebasejs/7.0.0/firebase-app.js";
    		script.type = 'text/javascript';
    		script.async = true;
    		script.onload = () => {
                         console.log(window['firebase']); //undefined
    		}
    		
    		document.body.appendChild(script);
    	}
    });
    

    The script is loaded correctly but is not accessible in the window object (as it should be).
    I also get the following error

    benchpress.js:226 TypeError: o is not a function
    at i (benchpress.js:70)
    at benchpress.js:195

    Why benchpress error? If i delete the script above all works fine.

    It happen also if i try to load an external script in other way. maybe using jquery

  • No idea what's going on there but I recommend trying it after building in dev mode so you can see the original code responsible for the error:

    ./nodebb build --dev
    
  • This post is deleted!
  • @PitaJ said in external scripts does not load on the client side:

    ./nodebb build --dev

    no error in dev mode, but again the script is undefined. Is there to activate some authorization?
    the script is loaded from the network, it is also present in the html tags index. However it is not accessible.

    p.s: i running the app in localhost.

    if i try this code

    	$(document.body).append(`
    	    <script defer src="https://www.gstatic.com/firebasejs/7.0.0/firebase-app.js"></script>
    	`)
    

    i get this errors

    nodebb.min.js?v=b09q1lvc4ag:14627 Uncaught TypeError: Cannot read property 'create' of undefined
    at nodebb.min.js?v=b09q1lvc4ag:14627
    at Object.execCb (nodebb.min.js?v=b09q1lvc4ag:13769)
    at da.check (nodebb.min.js?v=b09q1lvc4ag:13758)
    at da.<anonymous> (nodebb.min.js?v=b09q1lvc4ag:13762)
    at nodebb.min.js?v=b09q1lvc4ag:13747
    at nodebb.min.js?v=b09q1lvc4ag:13763
    at y (nodebb.min.js?v=b09q1lvc4ag:13746)
    at da.emit (nodebb.min.js?v=b09q1lvc4ag:13763)
    at da.check (nodebb.min.js?v=b09q1lvc4ag:13759)
    at da.enable (nodebb.min.js?v=b09q1lvc4ag:13763)

    footer.js?v=b09q1lvc4ag:13 Uncaught TypeError: translator.prepareDOM is not a function
    at footer.js?v=b09q1lvc4ag:13
    at Object.execCb (nodebb.min.js?v=b09q1lvc4ag:13769)
    at da.check (nodebb.min.js?v=b09q1lvc4ag:13758)
    at da.<anonymous> (nodebb.min.js?v=b09q1lvc4ag:13762)
    at nodebb.min.js?v=b09q1lvc4ag:13747
    at nodebb.min.js?v=b09q1lvc4ag:13763
    at y (nodebb.min.js?v=b09q1lvc4ag:13746)
    at da.emit (nodebb.min.js?v=b09q1lvc4ag:13763)
    at da.check (nodebb.min.js?v=b09q1lvc4ag:13759)
    at da.<anonymous> (nodebb.min.js?v=b09q1lvc4ag:13762)

    seems like the insertion of the script broke something.

    If i insert the same script on the head i receive this error

    nodebb.min.js?v=mhcbi3377ao:16519 Uncaught TypeError: pagination.init is not a function
    at nodebb.min.js?v=mhcbi3377ao:16519
    at Object.execCb (nodebb.min.js?v=mhcbi3377ao:13769)
    at da.check (nodebb.min.js?v=mhcbi3377ao:13758)
    at da.<anonymous> (nodebb.min.js?v=mhcbi3377ao:13762)
    at nodebb.min.js?v=mhcbi3377ao:13747
    at nodebb.min.js?v=mhcbi3377ao:13763
    at y (nodebb.min.js?v=mhcbi3377ao:13746)
    at da.emit (nodebb.min.js?v=mhcbi3377ao:13763)
    at da.check (nodebb.min.js?v=mhcbi3377ao:13759)
    at da.enable (nodebb.min.js?v=mhcbi3377ao:13763)

    script loaded but not executed.....

    if i try to load a script on a template file, loaded with benchpress, i dont have problems...

  • problem solved. i have to import external script with

    require([script], function(name) {
    
    })
    

    It seems that importing scripts in a classic way creates conflicts in nodebb


Suggested Topics