Skip to content
Gaurav TiwariDocs

Serve Static Files From a CDN

Version 1.3.6 Updated October 26, 2026

Rewrites same-site static file URLs to an origin-pull CDN hostname. Independent of Cloudflare: you can run both, one, or neither. Cloudflare can keep caching eligible HTML while the browser fetches the selected files from the CDN hostname.

Off by default. Configure it on the CDN tab.

GT Performance does not upload files or configure a provider account. It only changes URLs in the HTML, so the CDN must be able to fetch every original WordPress path from your site.

Configure

  1. Create an origin-pull (pull zone) resource at your CDN provider that fetches from this WordPress site.
  2. Turn on Enable CDN URL rewriting.
  3. Enter the CDN URL: the HTTPS hostname, or hostname plus path, your provider gave you (for example https://cdn.example.com). A port is kept if you include one. Anything that is not a valid https:// URL is discarded on save.
  4. Under Files served by the CDN, tick the exact extensions the CDN should serve.

Rewriting needs both a CDN URL and at least one extension. With either missing, nothing changes.

File types

GroupExtensionsOn by default
Imagesjpg, jpeg, png, gif, webp, avif, svg, icoYes
Styles and scriptscss, js, mjsYes
Fontswoff, woff2, ttf, otf, eotYes
Video and audiomp4, webm, mp3, ogg, wavNo
Downloadspdf, zipNo

Unselected types keep their original WordPress URLs.

What gets rewritten

A URL is rewritten only when all of these hold:

  • It is an http or https URL whose host matches the host of home_url(), site_url(), or content_url(), or it is root-relative (/wp-content/…) or protocol-relative (//example.com/…).
  • Its path ends in a selected extension.
  • It is not already on the CDN base, so running the rewrite twice changes nothing.

The CDN URL (including any path you entered) replaces the scheme and host. The original path, query string, and fragment are kept: https://example.com/wp-content/uploads/a.jpg?v=2 becomes https://cdn.example.com/wp-content/uploads/a.jpg?v=2.

Places it looks:

  • src, href, poster, data-src, data-lazy-src, data-original, and data-bg attributes
  • srcset and data-srcset, candidate by candidate
  • url(…) inside inline style attributes and inside <style> elements

Because href is included, selecting pdf or zip also moves ordinary download links to the CDN.

Left untouched:

  • Third-party URLs, and data:, blob:, mailto:, tel:, and javascript: URLs
  • HTML routes and any path without a selected extension
  • REST, AJAX, cron, feed, and robots.txt responses, and wp-admin
  • URLs inside external stylesheet files
  • SVG <use> references, because browsers refuse to resolve a sprite across origins and every icon would disappear
  • Any element with an integrity attribute, because Subresource Integrity plus a cross-origin URL without CORS makes the browser drop the resource

When a font preload (<link rel="preload" as="font">) is moved to the CDN, crossorigin="anonymous" is added so the browser can use the preloaded file instead of fetching it twice.

Rewriting uses WordPress’s HTML tag processor, so markup it does not change (inline SVG, non-Latin text) is left byte-for-byte as it was. Safe Mode turns rewriting off along with every other HTML transformation.

How it interacts with the page cache

The rewrite runs twice, safely:

  • Inside the page-cache pipeline, so cached HTML already contains CDN URLs and a cache hit serves them with no extra work.
  • As the outermost output buffer, started before the page-cache capture, so pages that bypass the origin cache still get CDN URLs.

Because the rewrite is idempotent, the second pass finds nothing left to change.

Purging

Changing any CDN setting purges the origin page cache (and, through it, the Cloudflare zone when Cloudflare is connected), so no cached page keeps the old URLs. GT Performance never purges the separate CDN. When you replace a file without changing its URL, purge it through your CDN provider.

Unused CSS

The unused-CSS engine reads stylesheets from this site’s own host and from the CDN hostname, so a stylesheet already rewritten to the CDN is still analyzed. The gt_performance_css_allowed_hosts filter can add other hosts you own. See CSS Optimization and Hooks Reference.

Choosing extensions

The defaults cover images, styles, scripts, and fonts. If you are unsure of the CDN’s configuration, start with images only:

  • Add fonts once the CDN sends Access-Control-Allow-Origin. A CDN that drops it breaks web fonts specifically, and that failure looks like a font problem rather than a CDN problem.
  • Add css, js, and mjs once you have confirmed the CDN serves them with correct MIME types.
Share

Was this helpful?

Still stuck? Open a support ticket