Home / Uncategorized / Speeding Up Async Snippets – Web Performance and Site Speed Consultant

Speeding Up Async Snippets – Web Performance and Site Speed Consultant

Written by on CSS Wizardry.

Table of Contents
  1. What Is an Async Snippet?
  2. Legacy async Support
  3. What’s Wrong With the Polyfill?
    1. The Preload Scanner
  4. The New Syntax
  5. When We Can’t Avoid Async Snippets
    1. Dynamic Script Locations
    2. Injecting Into a Page You Don’t Control
  6. Takeaways

If you’ve been a web developer for any reasonable amount of time, you’ve more
likely than not come across an async snippet before. At its simplest, it looks
a little like this:

"https://third-party.io/bundle.min.js">

How is this any different to just loading the file normally? What have we done that makes this asynchronous? Where is the magic?!

Well, the answer is nothing. We didn’t do a thing. It’s the spec which dictates that any scripts injected dynamically should be treated as asynchronous. Simply by inserting the script with a script, we’ve automatically opted into a standard browser behaviour. That’s really the extent of the whole technique.

But that begs the question… can’t we just use the async attribute?

As a bit of additional trivia, this means that adding script.async="async" is redundant—don’t bother with that. Interestingly, adding script.defer=defer does work, but again, you don’t need an async snippet to achieve that result—just use a regular "https://third-party.io/bundle.min.js" async>

These are functionally identical.

If you’re feeling nervous about completely replacing your async snippet, or the async snippet contains config variables, then you can replace this:

"https://third-party.io/bundle.min.js?user=USR-135-6911-7" async>

This works because, even though the

Leave a Reply

Your email address will not be published. Required fields are marked *