Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

CSS Compression Job

I would like to dispatch a job to compress published CSS once the CSS file has been uploaded to Digital Ocean Spaces and then replace uncompressed file by compressed file and update path in database.

So the controller I have something like:

// Copy latest version of the published.stylesheet
 \Storage::disk('do')->put($cssSource, file_get_contents(public_path('css/published.css')), 'public');

// activate job and send css source connected to project along
 \App\Jobs\CompressCSS::dispatch($cssSource);

Still new to Laravel and Jobs so I wonder, do I need to set the $cssSource in the constructor? So I can use the CSS path to get the not compressed file from the CDN and then gzencode it? How do I do that?

\Storage::disk('do')->get($cssSource, file_get_contents(public_path('css/published.css')), 'public');
$gzippedContent = gzencode($cssSource); // gzip compress the content

in the handler is still looking for $cssSource and $projectPath so I am not sure if dispatch is sending this along to the job..

When I use

public function handle()
    
    {
        $projectPath = 'assets/' . $this->project->subdomain_url;
        $cssSource = $projectPath . '/css/site.css';
        \Storage::disk('do')->get($cssSource, file_get_contents(public_path('css/published.css')), 'public');
        $gzippedContent = gzencode($cssSource); // gzip compress the content
        
    }

It does not complain with \App\Jobs\CompressCSS::dispatch($this->project->subdomain_url); for the dispatch.

I guess my question is..

Can I send along the existing cssSource path in dispatch so I can use it in the job?

If so, should I use it as a variable in the constructor, which is not being used at all now?

0 likes
2 replies
Snapey's avatar

seems like a lot of trouble to save a few kilobytes

Are you sure you have optimised all your images? These will have a far greater impact.

1 like
rhand's avatar
Level 6

Well, yes, done quite well. During GTMetrix tests the main issue for getting a D is because we are not yet compressing CSS and JavaScript stored on the Digital Ocean Spaces CDN. Normally Nginx would gzip them, but now on DO Spaces we need to ourselves. And it would save us 30-70% in file size. Especially JS could benefit.

So I thought to perhaps first store them and then queue a gzencode job. This as we will have many of these stored (one js and one css per project) in the future. So to not overload our current web server I though.. enqueue the compression for a little later so we do not get 5000 request on this all at the same time.

Issue is that I am still learning about jobs, throttling and so on. So takes a bit more time here to understand all the steps needed.

Please or to participate in this conversation.