[SOLVED] Player loading all scripts twice after concatenate

Good morning guys,

I had some problems with the project that only appeared after the build (only happen at concatenated scripts build), so I found that all my scripts were loaded twice. Does anyone out there know what’s going on?

I can’t reproduce the issue here with a exported build or a build hosted on PlayCanvas. :thinking:

Is the project public that we could take a look at? Or can you share the published build?

Here is a secondary build that is presenting this issue:

Hi @derickwelman,

Are you by any chance attaching the same script file twice? Or defining a script type twice (same name)?

Nop. The strange thing is that every single script was duplicated. In addition, when i launch by project or by build with separated scripts, this not happen.

Some scripts are global and is not even stored at hierarchy. They load twice too.

Looks like it is related to the code to query the URL params. Once I remove ?teleport=113, there’s no double load of scripts.

At a guess, it looks like it is trying to do logic too early before the PlayCanvas app is ready. A couple of things that you could try.

Listening for the start event on pc.app or try rearranging the script order in settings so that this param parsing is at the end.

1 Like

Looking into this more, something in your scripts is causing the second load of the game scripts file

See initiator column:

Have you taken the build down? I was still looking at the issue. :sweat_smile:

Oh sorry, i needed to launch some hotfixes and forgot that I couldn’t delete the build

Util = {},
Util.loadAssetScript = function(t, e) {
    var r = pc.app.assets.find(t);
    if (!r)
        throw new Error('Could not find script asset with name "' + t + '"');
    var n = r.getFileUrl()
      , i = document.getElementsByTagName("head")[0]
      , s = document.createElement("script");
    s.type = "text/javascript",
    s.src = n,
    r.ready(function() {
        i.appendChild(s),
        e()
    }),
    pc.app.assets.load(r)
}

Is this code yours or ours? :sweat_smile:

Short version, this code looks for the asset script of draco_decoder.js, gets the url for the asset and adds it to the document in a script DOM.

The problem is that the scripts are all concatenated and the asset’s url is pointing to __game-scripts.js as that’s where the resource is. This leads to two __game-scripts.js being added and everything in global scope being ran twice.

Not sure why it’s been done this way as if this draco_decoder script is in the project, it’s already loaded and doesn’t need to be added manually like this.

Hummm… Interesting. This was a workaround for select which type of script would be loaded (normal javascript or ESM). But i not eve use the ESM one anymore.

Thank you so much for the help, now it should be easy to fix