Instead of relying on the server to gizip compress css and js file, is it a good/bad idea to gzip the file, store these on the server and link to those files in the html.
Instead of
<script src="../Scripts/compiled.js"></script>
Have this:
<script src="../Scripts/compiled.js.gzip"></script>
And same with CSS?
I tried but it's not working; the files don't seem to decompress. I get Resource interpreted as Stylesheet but transferred with MIME type application/x-gzip: Is this even possible?
Your Gzip file should have response header Content-Encoding: gzip while Content-Type should be text/javascript for JavaScript files or text/css for CSS files.
For me, following .htaccess rules does work:
AddEncoding x-gzip .gz
RewriteEngine On
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
<FilesMatch \.js\.gz$>
ForceType "text/javascript; charset=utf-8"
Header set Cache-control: private
</FilesMatch>
Header set Vary: Accept-Encoding can be used instead of Header set Cache-control: private to prevent returning Gzipped version to user-agents that do not support Gzip compression.
.htaccess file should be placed in directory that contains js.
Gzipped and nongzipped versions should be placed side by side (filename for gzipped version contains .gz postfix). Gzip-encoded version is returned transparently (if .gz version exists and browser supports Gzip which Accept-encoding request header is responsible for) when requesting usual file without Gzip mentioned explicitly in its URL.
P.S. Ah, you are using ASP.net, and therefore it's apparently running under IIS. Well, recent versions of IIS have .htaccess-like functionality, AFAIK.
Related
I have enabled GZIP in cPanel (Software > Optimize Website). It's set to "Compress All Content". GTmetrix speed test reports that one CSS file is not being compressed:
https://www.swphotovideo.com.au/wp-content/cache/min/1/2affb9ab843ac1d569fdadbf67636031.css
Any idea why this might? It's quite large (1mb~) so I'd like to sort it out. GZIP seems to be working for everything else, but this file.
We use GZIP to compress files so that Websites speed will become fast. As your problem shows you are able to compress all other files but not able to compress "CSS" files.
First of all, you should check your .htaccess file and check whether "TEXT/CSS" is there under "mod_deflate" or not.
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP
Also, to speed up your WordPress website you can use mod_expires to have Browser cache in WordPress which will speed up your website.
You can refer this article to speed up your WordPress application.
https://hoststud.com/resources/tips-to-leverage-browser-caching-in-wordpress.156/
Or, you should ask your hosting provider regarding this issue.
Note: I am using Wordpress and serving the media files, css, js, etc. through Amazon CloudFront/S3.
Hello,
I know there are a lot of posts like this but I am still struggling. I was able to fix this issue for a majority of the font files that I am loading, however, this one continues to be an issue.
Access to Font at 'http://mycloudfrontID.cloudfront.net/wp-content/themes/bridge/css/font-awesome/fonts/fontawesome-webfont.woff2?v=4.6.3' from origin 'http://mydomainname' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://maxmajor.net' is therefore not allowed access.
The other font files are fine after I added this to my CORS policy on AWS S3:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
and this is in my .htaccess:
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
Any ideas why this error is still firing?
Thanks,
Brian
Try to change:
Header set Access-Control-Allow-Origin "*"
with this:
Header add Access-Control-Allow-Origin "*"
Also I read
How does Access-Control-Allow-Origin header work?
Font from origin has been blocked from loading by Cross-Origin Resource Sharing policy
that for security reasons you have to include your URL.
So, your .htaccess should have this:
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css)$">
<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "http://mysitename.com"
</IfModule>
</FilesMatch>
Works for me!
Have you considered referring the following thread?
CORS Issue with woff2 fonts behind CDN in Chrome
It says
Turns out that the problem was actually with the Content-Type. As soon as I changed the content type to application/font-woff2 and invalidated the cache of these woff2 files, everything went through smoothly.
Are you sure that you are sending Authorization headers with your GET requests for the fonts?
If not, in your S3 CORS policy change
<AllowedHeader>Authorization</AllowedHeader>
to
<AllowedHeader>*</AllowedHeader>.
This tiny miss had bugged me for a month.
Given below is the code in my .htaccess file. On checking PageSpeed Insights, it still tells me to Leverage browser caching only for my png and jpg files. Could someone point out what could be wrong with the code? Would appreciate help! (The site is using a wordpress theme)
<IfModule mod_headers.c>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|svg)$">
Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
Header set Cache-Control "public"
</FilesMatch>
</IfModule>
Assuming it is working correctly for the other extensions, my best guess is that perhaps your .jpg and .png files have uppercase letters in their extension (e.g. .JPG or .PNG). If that's the case try making your FilesMatch regular expression be case insensitive (see How to make this .htaccess rule case insensitive?).
I've been using this app to minify my files which works great.
Link - http://minifyapp.com/
From the app's website I am given the following code to place in my .htaccess
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+)$ $1.gz [L]
However, this code is a little outdated because I noticed the filed weren't coming to the browser as css.
So I've dug around all day trying to fix this and added the following with the above
AddEncoding gzip .gz
AddEncoding x-gzip .gz
AddType text/css .gz
AddType text/javascript .gz
AddType text/x-javascript .gz
Header set Content-Encoding "gzip" env=is_gzip
This works to enable the browser to understand the .gz files as css.
But, I'm not getting the the content of those .gz files to be actually applied to my theme's styles.
So specifically in my source code I can see my .gz file being referenced instead of my normal .css.
And if I click to view the .gz file in the browser it renders as it should, css instead of a archived file.
But my theme has no actual styles applied to it.
What do you think is the problem and how can I fix it.
I've tested this with plugins and my theme's stylesheets, still the same result.
I have used the following rule to allow our static domain to host fonts, but I've run into a problem font's not being used by the browser (firefox, safari) when the browser cache is enabled.
<Directory "/site/http/web/assets/fonts">
<FilesMatch "\.(eot|otf|woff|ttf)$">
SetEnvIf Origin "^http://(.*)?main-domain.com$" origin_is=$0
Header set Access-Control-Allow-Origin %{origin_is}e env=origin_is
</FilesMatch>
</Directory>
I've also used the below rule to let browsers know it's ok to cache static content:
ExpiresActive On
ExpiresDefault "modification plus 10 years"
If I browse the site with the browser cache off, the fonts load and display every time.
If I turn my browser cache off and load a page, the fonts get loaded (firebug > net) but aren't displayed!
Is the 304 Content Not Modified Header causing the browser to ignore my efforts?
If you want your header addition to be used on a non-2xx response, use
Header set always Access-Con...