Using CDNJS from Wordpress


#1

Hi all,

I have been searching for an answer for quite some time without luck. Apologies if this has been answered already. I have a client with a WordPress site that relies on a number of common JS libraries and CSS.

I want to change them to use the Cloudflare CDN rather than the copies hosted in our server. I was wondering if there is away to configure this in Cloudflare (using injection) or if I have to manually change the address within WordPress to point to the CDN.

The client has a “Pro” subscription for Cloudflare.

Thanks in advance.


#2

I’m pretty sure there’s not automated way to do it. There are so many JS sources that may or may not exist in CDNJS.

What’s wrong with hosting them on your server? Hopefully up to date, and they’ll be cached plus you won’t need an additional DNS lookup.


#3

Thanks for the response, that certainly seems to be the case. I wasn’t looking for it to replace all, just wondered if there was anything like this.


#4

If you’re using nginx you can use nginx substitution filter to replace html contents of a page with the cdn served version too https://www.nginx.com/resources/wiki/modules/substitutions/

i.e. example in wordpress of serving gravatar images locally from domain.com/avatar

  sub_filter 'https://secure.gravatar.com/avatar/' 'https://domain.com/avatar/';
  sub_filter_last_modified on;
  sub_filter_once off;

  location /avatar {
   proxy_pass https://secure.gravatar.com$request_uri;
  }

#5

Ha! That somehow seems much easier than:


#6

This option is faster since you are doing it at the edge, but for things the first is easier to implement, especially of the server is close to one of the remote origins (same data center) which won’t impact things a lot expect for bandwidth usage.


#7

Yeah nginx sub filter is easier for end users to implement - shouldn’t make much difference if you have css/js cached with long TTL at cloudflare edge too.

Yeah there’s many ways to do it :slight_smile:


#8

Thanks for all the great responses.


#9

This topic was automatically closed after 30 days. New replies are no longer allowed.