Skip to content
  1. Extras
  2. PageSpeed

PageSpeed

Description

This add-on integrates PageSpeed Insights optimizations for MODX Revolution. It can:

  • Run automatically with standard configuration.
  • Convert gif, jpg and png to webp when the browser supports it, with or without caching. Adjust size and quality of converted images.
  • Generate critical CSS, detect and preload fonts.
  • Add font-display to @font-face declarations.
  • Set crossorigin and compute SRI hashes.
  • Use multiple configs with efficient caching.
  • Apply native lazy loading for img and iframe.
  • Minify styles, scripts, JSON and HTML.
  • Add defer or async to script tags.
  • Fetch page resources via cdnjs.com API and download fonts from Google Fonts.
  • Process meta and link tags with http-equiv and preconnect.
  • Output MODX timing tags to the browser console for Administrator group members.

You can purchase this add-on at Modstore.

Modes

ModeDescription
AutomaticWhen subresources is not set, the plugin finds resources in HTML and processes them.
ManualProcesses only resources from the subresources option.

Syntax

This is not a working config, but an overview of all available parameters.

modx
[[!PageSpeed?
  &bundle=`link script`
  &convert=`static`
  &critical=`true`
  &crossorigin=`anonymous`
  &display=`swap`
  &integrity=`sha256`
  &lifetime=`604800`
  &loading=`lazy`
  &minify=`html link script`
  &quality=`80`
  &resize=`true`
  &script=`defer`
  &subresources=`{
    "link": [
      { "name" : "", "version" : "", "filename" : "", "crossorigin" : "", "integrity" : "", "media" : "" },
      { "url" : "", "crossorigin" : "", "integrity" : "", "media" : "" }
    ],
    "script": [
      { "name" : "", "version" : "", "filename" : "", "async" : "", "crossorigin" : "", "defer" : "", "integrity" : "", "nomodule" : "" },
      { "url" : "", "async" : "", "crossorigin" : "", "defer" : "", "integrity" : "", "nomodule" : "" }
    ]
  }`
]]

Parameters

ParameterDescription
bundleOptional. Default link script. Content types to bundle. Case insensitive. Values: link, script, any combination or empty.
  • link - CSS files.
  • script - JS files.
convertOptional. Default static. Converts gif, jpg, png to webp. Values: disable, dynamic, static.
  • disable - no conversion.
  • dynamic - no cache after conversion; more CPU.
  • static - cached; more disk.
criticalOptional. Default true. Critical CSS generator. Boolean.
crossoriginOptional. Default anonymous. crossorigin for all resources. Values: anonymous, use-credentials, or empty.
displayOptional. Default swap. CSS font-display. Values: auto, block, swap, fallback, optional.
integrityOptional. Default sha256. SRI hash algorithm. Values: sha256, sha384, sha512, combination or empty.
lifetimeOptional. Default 604800. Resource cache lifetime.
loadingOptional. Default lazy. loading for img and iframe. Values: auto, eager, lazy.
minifyOptional. Default html link script. Content types to minify. Values: css, html, js, json, link, script, any combination.
  • css - inline CSS.
  • html - HTML.
  • js - inline JS.
  • json - inline JSON and JSON+LD.
  • link - CSS files.
  • script - JS files.
qualityOptional. Default 80. webp quality. Integer 0-100.
resizeOptional. Default true. Resize images in img tags. Boolean.
scriptOptional. Default defer. Attribute for script tags. Values: async, defer.
subresourcesOptional. Default auto. JSON with resource info. Either url or name (for cdnjs.com) is required; other properties get defaults from the API.

Examples

Latest jQuery with daily updates from jsdelivr.net:

modx
[[!PageSpeed?
  &lifetime=`86400`
  &script=`async``
  &subresources=`{
    "script" : [
      { "url" : "https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js" }
    ]
  }`
]]

Latest Bootstrap with defer for all script resources and weekly updates from cdnjs.com:

modx
[[!PageSpeed?
  &subresources=`{
    "link": [
      { "name" : "twitter-bootstrap", "filename" : "css/bootstrap.min.css" }
    ],
    "script": [
      { "name" : "jquery" },
      { "name" : "popper.js", "filename" : "umd/popper.min.js" },
      { "name" : "twitter-bootstrap" }
    ]
  }`
]]

Add any inline style or script using PHx. Note: this creates a new config instance if data differs on page load. Do not use for third-party code such as Google Analytics:

modx
[[+phx:input=`data:text/css,
  html {
    color: [[+color]];
  }
`:cssToHead]]

[[+phx:input=`data:text/javascript,
  console.log('[[+id]]');
`:jsToHead]]

[[+phx:input=`data:text/javascript,
  if (typeof performance === 'object')
    performance.mark('[[++site_name]]');
`:jsToBottom]]

Notes

Automatic mode may not work on every MODX configuration.

Cache can be cleared manually in Manager / Clear cache / PageSpeed.

For manual mode add them to the script section of the subresources parameter.

Animated gif support requires Image Processing (ImageMagick) for PHP.

To prevent concurrent execution, install Semaphore, Shared Memory and IPC for PHP.

This add-on uses PHP CSS Parser and Minify.