Chapter 7. Tutorial: Optimize Your Site Step by Step

Step 3: Optimize CSS and JavaScript Files

This step makes your CSS and JavaScript files smaller and moves them out of the way of your page. Most later features build on it, so it is worth getting it right.

JCH Optimize processes the files it finds in your page and replaces them with optimized copies. The work is grouped into six automatic settings, from Minimum (just process the files) to Optimum (minify, include third-party files, place JavaScript at the bottom of the page and defer it). The quickest way to find the right level for your site is to start at the top and step down only if something breaks.

Find the Highest Automatic Setting That Works

  1. On the dashboard, in the Optimize Files panel, make sure Combine Files Enable is on. (It is on by default.)

  2. Click Optimum.

    Figure 7.6. The automatic settings
    The six automatic settings buttons from Minimum to Optimum, with Optimum selected.

  3. Check your pages with the section called “What to Check After Each Change”.

  4. If everything works, you are done with this part. If something broke, click Premium and check again; keep stepping down until everything works. The level where the problem disappears tells you which kind of setting caused it:

    Problem disappears at…Likely cause
    PremiumDefer JavaScript: a script needs to run before the page is shown
    DeluxeMoving JavaScript to the bottom of the page
    AverageIncluding in-page scripts and styles, files from other domains, or replacing @import rules in CSS
    IntermediateIncluding files from third-party extensions or plugins
    MinimumMinifying a file, or minifying the HTML
    Still broken at MinimumProcessing a particular file at all
  5. Go back up to Optimum and use the next section to find and exclude the file causing the problem. Excluding one file is almost always better than giving up the optimization for every file.

Excluding a File That Breaks Something

When a page breaks after optimizing files, the cause is almost always one particular file, or one script written into the page, that doesn't like being moved, minified or deferred. Find it and tell JCH Optimize to leave it alone.

Is It CSS or JavaScript?

If the page looks wrong, the cause is usually CSS. If something doesn't work (a menu doesn't open, a slider doesn't move), it is JavaScript. To be sure, open the CSS tab of the settings page and set Optimize CSS Files (WordPress: Combine CSS files) to No; if the problem disappears, it is CSS. Otherwise, set it back and do the same with Optimize JavaScript Files (WordPress: Combine JavaScript files) on the JavaScript tab.

Finding the JavaScript File

  1. Make sure Debug Plugin is on (Step 0).

  2. Open the broken page in a private window with the console open, and reload.

  3. Look for a red error that starts with Uncaught, such as Uncaught TypeError: … is not a function or Uncaught ReferenceError: jQuery is not defined.

  4. Click the file name on the right of the error. The source opens at the line where the error happened. Scroll up from that line to the nearest comment that shows a file address: that is the original file the code came from.

    Figure 7.7. Tracing an error back to its original file
    The Sources panel showing an error in an optimized file, with the comment above it giving the original file's address.

If there is no error at all, the problem is usually timing: a script runs before something it needs. Try excluding the scripts of the extension or plugin that owns the broken feature (see below).

Tip

Before blaming JCH Optimize, switch it to Development mode and check the console. If the error is there even without JCH Optimize, it is an existing problem with the file, which optimization only made more visible.

Finding the CSS File

CSS problems are rarer. They usually come from an error in the original CSS, which a browser tolerates in a separate file but which affects the rules after it once files are combined.

  1. Right-click the part of the page that looks wrong and choose Inspect. In the Styles panel, look for the rules that should apply but don't, and click the file name next to them.

  2. As for JavaScript, the comment above the rule shows which original file it came from.

  3. If you can't see it, exclude the CSS files of the extension, plugin or theme that styles the broken part, one at a time, until the problem disappears.

Excluding the File

The exclude settings are on the CSS and JavaScript tabs of the settings page. Each is a list: click in the box to choose from the files JCH Optimize found on your site, or type part of a file address and click Add item. Any file whose address contains what you typed is excluded, so you rarely need the full address.

Figure 7.8. Excluding a JavaScript file
The Exclude JavaScript Files list with a file added and its Ignore execution order and Don't move checkboxes.

One file

Exclude JavaScript Files or Exclude CSS files.

Every file from one extension, plugin or theme

Exclude js files from these extensions or Exclude CSS files from these extensions (on WordPress, …from these plugins). Choose the folder name of the extension, plugin or theme. Useful when its files depend on each other.

A script or style written into the page

Exclude individual internal SCRIPT declarations or Exclude individual internal STYLES declarations (on WordPress, Exclude individual in-page <script>'s and …<style>'s). Type a short, distinctive piece of the script, such as a function name.

Save, clear the cache, and check the page again.

Excluding While Preserving Execution Order

Excluded files stay in their original order relative to the optimized ones, as the section called “Execution Order and Exclusions” explains, so nothing runs before something it depends on. For JavaScript, each excluded file has two extra options:

Ignore execution order

The file doesn't depend on any other script, and no other script depends on it, so the optimized files don't need to be split around it. This gives a slightly better result. Use it for self-contained scripts such as analytics or chat widgets. If ticking it breaks something, untick it.

Don't move

Keep the file exactly where it was on the page, instead of moving it to the bottom with the others. Use it for a script that must run early, for example one that sets up consent for cookies before anything else loads.

Moving and Deferring JavaScript

At Premium and Optimum, JavaScript files are placed at the bottom of the page (Position JavaScript files at bottom of page) and, at Optimum, given the defer attribute (Defer JavaScript; on WordPress, Defer or load JavaScript asychronously), so the browser shows the page first and runs the scripts afterwards. This removes the JavaScript part of PageSpeed's render-blocking resources warning.

Note

Files added to the page later by other scripts can't be moved or deferred, because JCH Optimize never sees them; they are not in the page when it is sent. Reduce Unused JavaScript in Step 6 is the tool for those.

Combining Files

By default, each file is optimized separately. You can also have files combined:

Combine files

Puts all CSS into one file and all JavaScript into another. This made pages faster when browsers fetched files one at a time; on today's servers it usually doesn't, and one change anywhere means visitors download the whole file again. Mostly useful for troubleshooting.

Smart combine (Bundling) Pro

Combines files into a few well-sized bundles instead of one large file, keeping the order intact. A good choice on modern servers. Needs Combine files on.