diff --git a/src/web/ResponseBehavior.php b/src/web/ResponseBehavior.php index 4a07f1b..f703ab4 100644 --- a/src/web/ResponseBehavior.php +++ b/src/web/ResponseBehavior.php @@ -34,13 +34,26 @@ public function events(): array ]; } - public function afterPrepare(Event $event): void + public function gzip(): void { - $this->joinMultiValueHeaders(); + $accepts = preg_split( + '/\s*\,\s*/', + Craft::$app->getRequest()->getHeaders()->get('Accept-Encoding') ?? '' + ); - if ($this->owner->stream) { - $this->serveBinaryFromS3(); + if (Collection::make($accepts)->doesntContain('gzip')) { + return; } + + $this->owner->content = gzencode($this->owner->content, 9); + $this->owner->getHeaders()->set('Content-Encoding', 'gzip'); + } + + public function afterPrepare(Event $event): void + { + $this->joinMultiValueHeaders(); + $this->gzip(); + $this->serveBinaryFromS3(); } /** @@ -48,6 +61,10 @@ public function afterPrepare(Event $event): void */ protected function serveBinaryFromS3(): void { + if (!$this->owner->stream) { + return; + } + /** @var TmpFs $fs */ $fs = Craft::createObject([ 'class' => TmpFs::class,