Question:
I ran Google pagespeed and it suggests compressing my .js and .css
Eliminate render-blocking JavaScript and CSS in above-the-fold content
Show how to fix
1 2 3 4 5 6 7 |
Enable compression Compressing resources with gzip or deflate can reduce the number of bytes sent over the network. Enable compression for the following resources to reduce their transfer size by 210.9KiB (68% reduction). Compressing http://xx.com/content/bundles/js.min.js could save 157.3KiB (65% reduction). Compressing http://xx.com/content/bundles/css.min.css could save 35.5KiB (79% reduction). Compressing http://xx.com/ could save 18.1KiB (79% reduction). |
During my publish I have a step that uses Windows Powershell to move a .js and .css mimified bundle to S3 and this goes to cloudfront.
Is there some step I could add in the PowerShell script that would compress the .js and .css files?
Also once the files are compressed then do I have to do anything more than change the name so as to tell my browser that it will need to try and accept a gzip file?
Answer:
You can add to your upload script the needed code to gzip compress the files.
Some example code could be this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
function Gzip-FileSimple { param ( [String]$inFile = $(throw "Gzip-File: No filename specified"), [String]$outFile = $($inFile + ".gz"), [switch]$delete # Delete the original file ) trap { Write-Host "Received an exception: $_. Exiting." break } if (! (Test-Path $inFile)) { "Input file $inFile does not exist." exit 1 } Write-Host "Compressing $inFile to $outFile." $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read) $buffer = New-Object byte[]($input.Length) $byteCount = $input.Read($buffer, 0, $input.Length) if ($byteCount -ne $input.Length) { $input.Close() Write-Host "Failure reading $inFile." exit 2 } $input.Close() $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None) $gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress) $gzipStream.Write($buffer, 0, $buffer.Length) $gzipStream.Close() $output.Close() if ($delete) { Remove-Item $inFile } } |
From this site: Gzip creation in Powershell