Menu

Why "defer" breaks WordPress sites (and how to test it before your visitors notice)
📰

Why "defer" breaks WordPress sites (and how to test it before your visitors notice)

DEV Community: php·rahim·5 days ago
#5D2YSwRM
#devto#dev#defer#jquery#scripts
Reading 0:00
15s threshold

Adding defer to your scripts is one of the cheapest speed wins on a WordPress site. It is also the setting most likely to break a menu, a slider or a checkout field. This post explains why, with a small example, and shows a safe way to roll it out.

Disclosure: I'm part of the team behind NodeThemes Cache AI, a free WordPress cache plugin that tests changes like this one. The technique below works with or without it.

What defer actually changes

A classic <script src> in the <head> blocks parsing: the browser downloads and runs it before it builds the rest of the page. With defer , the browser keeps parsing and runs the script later. According to MDN , deferred scripts:

run after the document has been parsed, before DOMContentLoaded

run in the order they appear

only apply to external scripts ( defer on an inline script does nothing)

Attribute Blocks parsing?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.