Report this

What is the reason for this report?

How do I enable gzip compression on Spaces?

Posted on June 12, 2019

(I’m referring to the DigitalOcean Spaces + CDN Product)

How do I enable gzip compression on web assets that I have in my Space and using a CDN for? I’ve defined the Content-Encoding value to gzip and flushed the cache but it hasn’t worked.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Same for me compression breaks Javascript files.

For (Laravel) PHP you can also use gzencode - php doc - as told by dev.to to gzip encode images or other files before you send them to your CDN. And this applies to any CDN, whether it is Amazon S3 or Digital Ocean Spaces. Laravel example by Nivesh Saharan at Dev.to:

$disk = config('filesystems.cloud'); // Returns s3

$settings = $shop->app_settings(); // Returns an array of all the settings

$content = view()->make('scripts.settings', compact('settings'))->__toString(); // Loads a view with all the settings

$gzippedContent = gzencode($content); // gzip compress the content

$fileName = 'settings/' . $shop->domain . '.js';

// Here we upload the file to S3
$response = \Storage::disk($disk)->put($fileName, $content, [
    'visibility'     => 'public',
    'ContentType'    => 'application/javascript',
    'ContentEncoding'=> 'gzip',
]);

Example is again a js file. Most of us want this for images especially, so you would need to change ContentType and make some other tweaks. But overall a very good example.

This is the main blocker from us moving from cloudfront to Space to serve our static files

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.