apache / incubator-pagespeed-mod

Apache module for rewriting web pages to reduce latency and bandwidth.
http://modpagespeed.com
Apache License 2.0
696 stars 158 forks source link

Limit ModPagespeedAddResourceHeader by file type #1300

Open JoyceBabu opened 8 years ago

JoyceBabu commented 8 years ago

I have compiled a custom mod_pagespeed build by cherrypicking https://github.com/pagespeed/mod_pagespeed/pull/1200 on to latest-stable and is now able to add custom headers to optimized resources.

I want to add Timing-Allow-Origin to all optimized resources and Access-Control-Allow-Origin to just font/css/js files. I tried putting the ModPagespeedAddResourceHeader inside a <LocationMatch> block. But the headers are not being applied then.

How can I set the header by file type?

oschaaf commented 8 years ago

The feature is tested with this configuration:

<Location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+">
  # For testing that AddResourceHeader gets passed through
  ModPagespeedAddResourceHeader "X-Foo" "Bar"
</Location>

This example adds X-Foo: Bar to the response headers for all .pagespeed. resources. It think it's not possible to serve different headers based on the content-type, but perhaps you could use multiple location blocks that match different paths and/or extensions?

jmarantz commented 8 years ago

All .pagespeed. resources are served with canonical extensions, so it's possible to reliably create a location regexp based on filename that targets content-type.

E.g. all javascript files end in ".js" even if the origin resource did not.

-Josh

On Fri, May 6, 2016 at 8:42 AM, Otto van der Schaaf < notifications@github.com> wrote:

The feature is tested with this configuration:

<Location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+">

For testing that AddResourceHeader gets passed through

ModPagespeedAddResourceHeader "X-Foo" "Bar"

This example adds X-Foo: Bar to the response headers for all .pagespeed. resources. It think it's not possible to serve different headers based on the content-type, but perhaps you could use multiple location blocks that match different paths and/or extensions?

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/pagespeed/mod_pagespeed/issues/1300#issuecomment-217429093

JoyceBabu commented 8 years ago

Thank you @oschaaf, @jmarantz .

I am sorry, I had placed it inside <FilesMatch> instead of <LocationMatch>. <LocationMatch> is working as expected.

<LocationMatch "\.(woff|woff2|ttf|otf|eot|js|css)$">
    <IfModule mod_headers.c>
        Header set Timing-Allow-Origin: *
        Header set Access-Control-Allow-Origin: *
    </IfModule>
    ModPagespeedAddResourceHeader "Timing-Allow-Origin" "*"
    ModPagespeedAddResourceHeader "Access-Control-Allow-Origin" "*"
</LocationMatch>
JoyceBabu commented 7 years ago

The above configuration is not working as expected. For some requests, the CORS headers are being added twice. Chrome merges the duplicated headers to *, * and rejects the files.

kzap commented 7 years ago

I have this problem also. I am trying to get headers added on the original files and on files modified by Pagespeed using ModPagespeedAddResourceHeader ....