In-Place Resource Optimization (IPRO)

System Configuration

Note: Enabled by default as of PageSpeed 1.9.32.1

Normally PageSpeed rewrites a resource such as an image by finding its URL on your page, fetching and optimizing the data in the background, and then replacing that URL by an optimized pagespeed URL. But what about resources that are loaded by JavaScript code? And what about links to your images from pages outside your domain? In-Place Resource Optimization (IPRO) will optimize the content of a resource that’s requested using the original (non-pagespeed) URL, ensuring you are serving optimized content even when that content isn’t explicitly linked in your html. This should be especially helpful for sites with slide shows that use JavaScript to load images in the background; those images can now be optimized by PageSpeed by adding this command to your configuration file:

pagespeed InPlaceResourceOptimization on

Some sites employ JavaScript that is sensitive to the original resource URL syntax and won't work with PageSpeed's altered resource URLs. You can choose to preserve the URLs of your resources; these will still be rewritten by IPRO, but they won't be replaced by a pagespeed URL. This is especially useful as an "optimization is on by default" setting for hosting providers and optimizing forward proxies – cases where site-specific settings to blacklist URLs are impractical.

Doing browser-specific in-place optimization

PageSpeed has a number of optimizations that are browser-specific. For example, WebP conversion is performed only for browsers that can display WebP images. Ordinarily PageSpeed accomplishes this by serving different rewritten URLs to different browsers depending upon their capabilities. For resources that are rewritten in place, this isn't possible; instead, appropriate headers (such as Vary: Accept and Vary: User-Agent) are added to rewritten resources as necessary. Enable these browser-specific optimizations as follows:

pagespeed EnableFilters in_place_optimize_for_browser

By default, when in_place_optimize_for_browser is enabled, appropriate Vary: headers are added to resources that are subject to browser-specific optimization. CSS files are served with a Vary: User-Agent header. Photographic images that are candidates for WebP conversion are served with Vary: Accept to browsers that include Accept: image/webp in their headers. Note that while the most recent versions of browsers that display WebP images include this header in image requests, older WebP-capable browsers do not — as a result, a slightly smaller subset of browsers will receive WebP images than would be the case if the URLs were rewritten.

Internet Explorer has difficulty caching resources with Vary: headers (they are either not cached or require revalidation on every resource access). As a result, browser-specific in-place resources are instead marked Cache-Control: private when served to all versions of Internet Explorer.

Setting the inplace resource rewrite deadline

When InPlaceResourceOptimization is enabled, PageSpeed uses a default deadline of 10ms the when optimizing the resource. If the optimization cannot be completed in 10ms, then the original resource is served to the client, while the optimization continues in the background. Once cached, the optimized resource will be served for further requests. Note that a value less than zero will cause PageSpeed to wait indefinitely.

Also note that in-place-optimized resources generally take at least two refreshes to optimize regardless of the deadline, due to the current architecture of the server modules.

pagespeed InPlaceRewriteDeadlineMs deadline_value_in_milliseconds

This directive can be used at all configuration levels.

Considerations

Resources optimized by in-place resource optimization are optimized differently from resources found in HTML, JS, and CSS. First, rewritten pagespeed URLs contain a content hash that enables the optimized data to be cached for a year by browser and proxy caches; in-place resources are not cache extended. Second, in-place resources can't be optimized specially for the context in which they occur on the page: images can't be resized to the size they appear on the page, and multiple resources on a page can’t be combined together. Finally, in-place resources that are eligible for browser-specific optimizations (such as conversion to the WebP image format) will be served with the Vary: User-Agent caching header, reducing caching at intermediate proxies.

Risks

In-place resource optimization will add a new cache entry for every unique URL requested. It will also copy each request into memory once. If you have a large site with many uncacheable resources, this could quickly fill up your cache or cause a lot of expensive string copies.

In-place optimization will also add a small delay to every server response, this should not be large and we have not been able to measure any noticeable slow-down, but if most of your resources are uncacheable, you may wish to avoid this cost.