Hugo minify giving different output for the same JS function

Hi,
I got following function in JS (before hugo minify it)

localStorage.clear();
    const generatecidId = () => Math.floor(Math.random() * 0x7FFFFFFF) + "." + Math.floor(Date.now() / 1000);
    const cidId = () => {
        if (!localStorage.cid) {
            localStorage.cid = generatecidId();
        }
        return localStorage.cid;
    };
console.log(cidId())

On one Hugo website, hosted on Netlify the output value is for example 1917383613.1649013816 (expected, correct)

But, using exactly the same formula on a different website, hosted in the same place, the output is 0.2p8sujqw5va (not what I expected, incorrect)

I checked minified codes, and they are the same, so nothing is changed.
I can even use minified codes on both sides and got the same behaviour.

I am expecting 10-digit number followed with dot and further 10-digit number. The whole shall generate a string (or number).

Tried to force generatecidId(), return localStorage.cid; or even the whole function to be a number by Number() or adding + but still got exactly the same behaviour which is a bit strange and don’t know how to bite it.

(Pst… that is not a function.)

As it is Javascript I would not assume that a different backend would change how the function behaves. If you are sure that the code is the same (is it really? As in same line, same position, same minification?) there should be nothing, that would break things here or change how it works if you visit in the same webbrowser.

One thing I could think of is that you have additional optimisation enabled in Netlify which leads to more minification and moves the JS into one big file on a CDN somewhere. in that case maybe the order of other code and this snippet might collide.

First thing to “closure” it up is: put it in a real function that executes itself at the end, so that it’s autark of anything else.

It’s not a problem on Hugo’s side or server side imho, this is in the browser. My assumption is, that the minification after all is not the same local and over there.

Also: The way this all looks it might have to do with typecasting, because the netlify version is not digits at all. Maybe 0x7FFFFFFF is misunderstood.

Then this is not an issue with Hugo minify (or Hugo in general).

For anyone who lands here from search: Issues with Hugo minify can typically be solved by configuring minify.

Yes, it is not a full function. This is part of the function described here.

I am not sure that is not Hugo issue.

Same browsers, same hosting etc, minification off on netlify end is off.

Thanks, @RHEV for suggesting minify option, however, I got on both sites’ default settings, so don’t want to correlate output with specific settings in config.

I will need to do some more testing today by using this in other of my sites and see it that only relate to one, or all.

Hmm… Implemented the code on 5 other websites and on the got same (incorrect) behaviour were on my “playground” site it’s working ok. Then it’s something that I got set similarly on the and not on the “playground” site I will need to investigate.

Yes, this looks odd, but tried different approach ((Math.floor(Math.random() * 1000000000) + 1)) with simillar results.

Timezones? Maybe the Date.now() is returning different results. I wouldn’t think so as it’s just an integer.

The two parts are probably 0.2 and p8sujqw5va. Maybe even “Math” is not running as intended.

Try debugging BEFORE and AFTER you move into localstorage. Maybe there is a bug.

Still can’t envision any form of Hugo involvement.

Thank, will have a try.

1 Like

I also suspect timezone issue, but if you drop the type attribute in your script tag, I think the minifier will ignore it, you could also try running without the ´–minify` flag to see if they still behave the same.

Thank you all.

Tried without --minify and still face the issue, hence yes, that’s not Hugo issue. We can close that, and will keep looking further into JS itself.

ps. its not timezone or date issue as this Math.floor(Math.random() * 0x7FFFFFFF) + "." + Math.floor(Math.random() * 0x7FFFFFFF) will also generate this kind of format 0.2p8sujqw5va instead expected 1917383613.1649013816

JFTR:
console.log(Math.floor(Math.random() * 0x7FFFFFFF) + "." + Math.floor(Math.random() * 0x7FFFFFFF)); gives “1081569771.1875837710” in Firefox. I guess you’re having a very different problem than calculating this pseudo UUID.

It might be related to the fact that your usage of localStorage is not quite in line with what MDN describes:

Found the issue.
A bit crazy. The other script overwrites my localStorage.cid value which causes an issue. This will make me think and thanks to that I improved the code that I will update now.

Despite that this was not hugo related, thats fro your help.

namespaces. :wink:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.