Cannot optimize mobile theme
-
Hi!
Installed AO 2.0.2.
I’m using custom mobile swithing plugin: it detects mobile browsers and switches to mobile WP theme.
The problem is: AO works fine on desktop version (http://dev2.kuponarama.ru/) but on mobile (http://m.dev2.kuponarama.ru/) it works only for html and inlined CSS and JS – so it cannot optimize external CSS (for example with URL “/wp-content/themes/mobile/css/all.css”).Some theme switching code:
case "m.dev2.kuponarama.ru": define('WP_HOME', 'http://m.dev2.kuponarama.ru'); define('WP_SITEURL','http://m.dev2.kuponarama.ru'); //define('WP_CONTENT_URL','http://m.dev2.kuponarama.ru/wp-content'); break;Changing theme:
/** * @return mobile theme name */ function kuponarama_getMobileTheme() { switch($_SERVER["HTTP_HOST"]) { case "m.kuponarama.ru": case "m.dev.kuponarama.ru": case "m.dev2.kuponarama.ru": return "mobile"; case "dev.kuponarama.ru": case "kuponarama.ru": case "www.kuponarama.ru": return "clipper-child"; } } function ts_get_template($template) { $theme = kuponarama_getMobileTheme(); if (empty($theme)) { return $template; } $theme = ts_get_one_theme($theme); if (empty($theme)) { return $template; } // Don't let people peek at unpublished themes. if (isset($theme['Status']) && $theme['Status'] != 'publish') return $template; return $theme['Template']; } add_filter('template', 'ts_get_template'); function ts_get_stylesheet($stylesheet) { $theme = kuponarama_getMobileTheme(); if (empty($theme)) { return $stylesheet; } $theme = ts_get_one_theme($theme); // Don't let people peek at unpublished themes. if (isset($theme['Status']) && $theme['Status'] != 'publish') return $stylesheet; if (empty($theme)) { return $stylesheet; } return $theme['Stylesheet']; } add_filter('stylesheet', 'ts_get_stylesheet'); // helper function to replace get_theme() function ts_get_one_theme($theme) { $themes = wp_get_themes(); if (array_key_exists($theme, $themes)) return $themes[$theme]; return NULL; }
-
AO doesn’t aggregate JS/ CSS if it’s not on the same domain, so you’ll have to feed the alternate domains to AO using the
autoptimize_filter_cssjs_multidomainfilter (array $in, return array $out).hope this helps,
frankJust added filter, no luck.
I have relative css URLs like this: “/wp-content/themes/mobile/css/all.css” without domain.
relative URL’s are normally aggregated as well … don’t see any sign of autoptimize on http://m.dev2.kuponarama.ru/ actually, not even for inline code?
frank
I’ve enabled inline code optimization. You can see optimized JS file.
My CSS and JS links are located in wrapper.php in mobile theme. They are plain text, no PHP code.
does the linked CSS/ JS get autoptimized if you use the mobile theme as the default theme for desktop as well (so without it being called by your mobile detection code)?
frank
Still no luck. Tried changing default theme to mobile version – no optimization on mobile domain (m.dev2.kuponarama.ru/).
Then using desktop domain dev2.kuponarama.ru – everything works fine even with mobile theme.Nick.
OK, looks like a domain mismatch error anyhow; AO prepends your relative URL’s with the site-url, which is probably different from the current URL.
Can you overwrite wp-content/plugins/autoptimize/classes/autoptimizeBase.php with this version and then check your php error log for debug messages?
frank
Got some debug info:
[20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:41] autoptimize debug: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js does not match host in ajax.googleapis.com [20-Apr-2016 09:18:41] autoptimize debug: getpath got multidomains but no dice for https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:41] autoptimize debug: http://counter.rambler.ru/top100.jcn does not match host in counter.rambler.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath got multidomains but no dice for http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:41] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/css/all.css [20-Apr-2016 09:18:41] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css [20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:41] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:43] autoptimize debug: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js does not match host in ajax.googleapis.com [20-Apr-2016 09:18:43] autoptimize debug: getpath got multidomains but no dice for https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:43] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:43] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:43] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:43] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:43] autoptimize debug: http://counter.rambler.ru/top100.jcn does not match host in counter.rambler.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath got multidomains but no dice for http://counter.rambler.ru/top100.jcn [20-Apr-2016 09:18:43] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/css/all.css [20-Apr-2016 09:18:43] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css [20-Apr-2016 09:18:43] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 09:18:43] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.cssthis is the relevant entry;
[20-Apr-2016 09:18:41] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js using tmp_ao_root ://dev2.kuponarama.ru
tmp_ao_root is based on content_url(), which in this case apparently returns dev2.kuponarama.ru which does not match m.dev2.kuponarama.ru
as a quick workaround/ test, can you set m.dev2.kuponarame.ru as CDN in AO settings?
frank
After setting CDN http://m.dev2.kuponarama.ru:
[20-Apr-2016 10:26:22] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:26:22] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:26:22] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/search.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 10:26:22] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:26:22] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:26:22] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:26:22] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:26:22] autoptimize debug: getpath URL before normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:26:22] autoptimize debug: getpath URL after normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:26:22] autoptimize debug: http://counter.rambler.ru/top100.jcn does not match host in counter.rambler.ru [20-Apr-2016 10:26:22] autoptimize debug: getpath got multidomains but no dice for http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:26:22] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/css/all.css [20-Apr-2016 10:26:22] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css [20-Apr-2016 10:26:22] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:26:22] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.cssAfter setting CDN http://m.dev2.kuponarama.ru:
in Autoptimize’s settings? because I still see
<script type="text/javascript" defer src="http://dev2.kuponarama.ru/wp-content/cache/autoptimize/js/autoptimize_653291585ab021db96c3bc704d4d5796.js"></script>on m.dev2.kuponarama.ru, which seems to indicate the CDN is not set in AO?
frank
actually, changing
case "m.dev2.kuponarama.ru": define('WP_HOME', 'http://m.dev2.kuponarama.ru'); define('WP_SITEURL','http://m.dev2.kuponarama.ru'); //define('WP_CONTENT_URL','http://m.dev2.kuponarama.ru/wp-content'); break;into
case "m.dev2.kuponarama.ru": define('WP_HOME', 'http://m.dev2.kuponarama.ru'); define('WP_SITEURL','http://m.dev2.kuponarama.ru'); define('WP_CONTENT_URL','http://m.dev2.kuponarama.ru/wp-content'); break;(so forcing wp_content_url to the correct domain) could be enough to fix this.
After setting CDN in AO settings and changing WP_CONTENT_URL still the same:
[20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 10:51:18] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/jquery.main.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:51:18] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/libs/sly.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/search.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 10:51:18] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:51:18] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/js/loadStyles.js [20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:51:18] autoptimize debug: http://counter.rambler.ru/top100.jcn does not match host in counter.rambler.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath got multidomains but no dice for http://counter.rambler.ru/top100.jcn [20-Apr-2016 10:51:18] autoptimize debug: getpath URL before normalization: /wp-content/themes/mobile/css/all.css [20-Apr-2016 10:51:18] autoptimize debug: getpath URL after normalization: http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css [20-Apr-2016 10:51:18] autoptimize debug: path for http://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css is ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.css using tmp_ao_root ://dev2.kuponarama.ru [20-Apr-2016 10:51:18] autoptimize debug: getpath considers not local as path still starts with :// ://m.dev2.kuponarama.ru/wp-content/themes/mobile/css/all.cssIn that case I can only conclude that AO doesn’t work with dynamically switched themes I’m afraid; both setting the WP_CONTENT_URL and setting a CDN in AO should allow it to pick up the JS/ CSS-files.
What you could try next is setting the URL’s of the CSS & JS to include the full main domain, so you’d have;
http://dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js
instead of
/wp-content/themes/mobile/js/search.jsWhile thinking about this; it is possible your theme switching code somehow messes up the way AO gets called, so alternatively you could play with the action-hook which AO hooks into, where you could INIT earlier by setting the AUTOPTIMIZE_INIT_EARLIER constant or change the priority of the action on template_redirect.
but that’s about all I can think of, really …
frank
Changing to http://dev2.kuponarama.ru/wp-content/themes/mobile/js/search.js doesn’t help.
But anyway thanks a lot for your time!
Good luck, Frank!
The topic ‘Cannot optimize mobile theme’ is closed to new replies.