Support » Plugin: Autoptimize » Separate CDN Base URL for JS and CSS
Separate CDN Base URL for JS and CSS
-
Hi,
Is there a way to have different CDN Base URLs for JS and CSS?
I would like to use statically.io for this, but they provide:
https://cdn.statically.io/js/example.com for JS
https://cdn.statically.io/css/example.com for CSSThank you,
Roman.
-
hmmm … a question first; does statically honor the directory structure the JS or CSS is found in? e.g. https://cdn.statically.io/js/example.com/wp-content/plugins/autoptimize/classes/external/js/lazysizes.min.js ?
Yes, it should work exactly like that.
They have their own plugin for wordpress which handles this automatically, but with their plugin enabled there is no way to benefit from AO optimizations in that case. They serve JS and CSS as they are. They only way to make it work with AO is via AO settings apparently.
OK, something like this code snippet might help;
add_filter( 'autoptimize_filter_cssjs_multidomain', function(){ return array( 'https://cdn.statically.io/js/example.com', 'https://cdn.statically.io/css/example.com' ); } );
Did that code snippet help woorooo ?
No, as far as I remember it didn’t, however I solved the problem the other way. I inlined all CSS, so that AO would output only aggregated js file.
However again, after testing properly with Pagespeed Insights from Google, I decided to serve js from my domain. This combination of inlined CSS and JS without CDN gave best performance. So I serve only images via Statically CDN since then.
yeah, loading critical resources from a different domain (often) does not help performance (due to the overhead of setting up the connection and encryption).
happy you found a sweet spot in your configuration 🙂
frankI’ve made a few changes on the website and would like to test again serving JS and CSS from separate URLs with the snippet you provided
add_filter( 'autoptimize_filter_cssjs_multidomain', function(){ return array( 'https://cdn.statically.io/js/example.com', 'https://cdn.statically.io/css/example.com' ); } );
Unfortunately I cannot make it work. I’ve left CDN Base field in the AO settings blank, I’ve inserted the snippet via Snippet Plugin and changed the urls to my domain. But still nothing. Am I doing something wrong?
very hard to say I’m afraid based only that info. forgetting about the code snippet for a second: what happens if you -with aggregate JS on- set the CDN to
https://cdn.statically.io/js/example.com
? does the JS get aggregated?Yes, it works as it should in this case. Aggregated and excluded js files are served via that URL
- This reply was modified 2 years, 7 months ago by woorooo.
and the other way around, if you enter
https://cdn.statically.io/css/example.com
does the CSS get aggregated/ optimized?Yes it does.
When only one of the CDN urls is used, everything works as expected.
- This reply was modified 2 years, 7 months ago by woorooo.
OK, maybe try to keep one of those two set as CDN and then activate the code snippet?
Yep, I’ve tried different combination of that as well. The snippet just doesn’t seem to be doing anything. Can there be some mistake in the code itself?
try adding error-logging to the code snippet and then check the PHP error-log to see if the logged string is there in which case we at least know if the snippet gets triggered?
add_filter( 'autoptimize_filter_cssjs_multidomain', function(){ error_log( 'snippet triggered' ); return array( 'https://cdn.statically.io/js/example.com', 'https://cdn.statically.io/css/example.com' ); } );
- The topic ‘Separate CDN Base URL for JS and CSS’ is closed to new replies.