Javascript inlined between script tags vs src DATA URI UTF-8 with percent-encoding

381 Views Asked by At

I build mobile first and I use tiny frameworks (under 10kB) which I inline in index.html to save on HTTP request.

I looked for days now and it seems like everyone else who inlines javascript does it like this:

<script>UGLIFIED JAVASCRIPT</script>

I do it like this:

<script src="data:application/javascript;utf8, UGLIFIED PERCENT-ENCODED JAVASCRIPT"></script>

You may say percent encoding will make a file much larger but it actually doesn't because the way gzip works- it replaces the repetition and it doesn't matter if the repeated phrase is <div> or %3Cdiv%3E.

My question is- are there any potential advantages of my approach?

PS. One of my ideas was browser caching file-like DATA-URI elements but I don't know if this makes sense since then I would have to also find the way of controlling how to prevent the load of parts of index.html. Unless I could use the cached elements elsewhere - that would have it's use cases too. Thoughts?

1

There are 1 best solutions below

1
richardtallent On

First, if your site isn't an SPA, inlining your shared scripts (regardless of method) means you're loading them on every page, negating the value of the browser cache.

Second, the trip across the wire may be similar for encoded vs. not script, but the more important metric is the time it takes for the Javascript to be parsed and compiled. URL decoding isn't free, but while I don't think it's going to matter much in the grand scheme of things, I see no reason why it would actually be faster to load than just script within the tag.