Re: mod_pagespeed forgets optimizations after a while

323 views
Skip to first unread message

Joshua Marantz

unread,
Jun 19, 2012, 7:31:13 AM6/19/12
to mod-pagesp...@googlegroups.com
Short answer, please try ModPagespeedLoadFromFile.  If you are able to use this feature to read resources directly from the file system into mod_pagespeed then you will not see this behavior.

This occurs on low-traffic server due to your origin resources expiring on mod_pagespeed's server.  For example, http://twitterbogen.dk/css/blueprint/screen.css has these response-headers:

Date: Tue, 19 Jun 2012 11:16:02 GMT
Server: Apache
Last-Modified: Sat, 16 Jun 2012 23:51:48 GMT
Etag: "23de7-3034-4c29f9db1efb5"
Accept-Ranges: bytes
Content-Length: 12340
Content-Type: text/css

As there are no caching headers, mod_pagespeed interprets this to mean "cache on the server for 5 minutes".   In a debug server, for example, the only one making queries is you.  If you don't do a query for more than 5 minutes, then the next time you do one, mod_pagespeed must consider all the resources to be expired, and fetch them again to see if they've changed.  This strategy allows mod_pagespeed to react to changes in your source files, but still serve pre-optimized content to your users.

 If you are getting a query per minute, this is usually no problem, as after 4 minutes, mod_pagespeed will still consider screen.css to be valid in its cache, but will also notice that it's 80% expired and will "freshen" it by fetching it again from origin.  So severs in production usually don't serve unoptimized content.

ModPagespeedLoadFromFile bypasses this issue because on every request, mod_pagespeed can check whether the file is updated by looking at its timestamp directly in the file system.

If you can't use ModPagespeedLoadFromFile you can also set the origin TTL of your resources to be longer.  To set it to 10 minutes use:
    <FilesMatch "\.(jpg|jpeg|gif|png|js|css)$">
      Header set Cache-control "max-age=600"
    </FilesMatch>

The tradeoff is it might be harder to develop while you are editing these files; you'd have to flush the mod_pagespeed cache: https://developers.google.com/speed/docs/mod_pagespeed/system#flush_cache.

Hope this helps,
-Josh

On Tue, Jun 19, 2012 at 3:16 AM, Jacob Friis Saxberg <jacob....@gmail.com> wrote:
I've setup mod_pagespeed and it seems to work fine, but only when I reload a page. Minutes later the optimization is gone.

Here's a static site where full optimization is only done when you reload the page:

On the first load the CSS files have no .pagespeed. in them. If you reload they do.
Any idea why?


And here's my mod_pagespeed configuration:

ModPagespeed on
AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html
ModPagespeedFileCachePath            "/var/www/mod_pagespeed/cache/"
ModPagespeedGeneratedFilePrefix      "/var/www/mod_pagespeed/files/"
ModPagespeedEnableFilters rewrite_javascript,rewrite_css
ModPagespeedEnableFilters collapse_whitespace,elide_attributes
ModPagespeedEnableFilters extend_cache,combine_css,move_css_above_scripts,move_css_to_head,inline_css,inline_import_to_link,flatten_css_imports,inline_javascript,outline_javascript,rewrite_style_attributes,rewrite_images,sprite_images
ModPagespeedAvoidRenamingIntrospectiveJavascript on
ModPagespeedMessageBufferSize 100000


Joshua Marantz

unread,
Jun 19, 2012, 9:37:20 AM6/19/12
to mod-pagesp...@googlegroups.com
On Tue, Jun 19, 2012 at 9:25 AM, Jacob Friis Saxberg <jacob....@gmail.com> wrote:
Thanks for a great answer.

You're welcome! 
 
Short answer, please try ModPagespeedLoadFromFile.  If you are able to use this feature to read resources directly from the file system into mod_pagespeed then you will not see this behavior.

Since the whole site is static, can I thus do like this?
ModPagespeedLoadFromFile "http://twitterbogen.dk/" "/var/www/twitterbogen/"

Yes that looks perfect. 

If you can't use ModPagespeedLoadFromFile you can also set the origin TTL of your resources to be longer.  To set it to 10 minutes use:
    <FilesMatch "\.(jpg|jpeg|gif|png|js|css)$">
      Header set Cache-control "max-age=600"
    </FilesMatch>

I thought extend_cache takes care of that?

There are two caches in question: the browser cache and mod_pagespeed's cache.  extend_cache helps make sure that as long as an origin resource  never changes, users with a warm browser cache won't have to do a round-trip to the server to get it.

The origin TTL tells mod_pagespeed how often to check back with the origin to see whether the resource has changed.  The challenge is that in general, if mod_pagespeed has to do an HTTP request to check whether the resource has changed, it won't wait till that's finished before sending back HTML to the browser.  We want to avoid making the user wait longer to get optimized content; they are better off getting unoptimized content immediately.  The benefit comes to the next user when mod_pagespeed has a warm cache.

-Josh

Reply all
Reply to author
Forward
0 new messages