Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assets GZIP compression with Apache #6650

Closed
marc-farre opened this issue Nov 11, 2023 · 8 comments
Closed

Add assets GZIP compression with Apache #6650

marc-farre opened this issue Nov 11, 2023 · 8 comments

Comments

@marc-farre
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

Without compression, a Humhub page loads for about 4,5 Mb of files.
With Apache compression, it's reduced to 2,5 Mb.

E.g. the humhub-bundle.js is 1,5 Mb. When sent with Apache GZIP compression, it's only 455 Kb.

Describe the solution you'd like

Add this to the .htaccess file:

# GZIP compression for text files: HTML, CSS, JS, Text, XML, fonts
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>
@marc-farre
Copy link
Collaborator Author

@luke- if you think it's a good idea, I can send a PR.
But I'm not an Apache expert.
This solution works well for me, but I don't know if it's the best practice nowadays.

@ArchBlood
Copy link
Contributor

You should attach gzip to event handlers on Response, instead of doing this in the .htaccess.

@marc-farre
Copy link
Collaborator Author

You should attach gzip to event handlers on Response, instead of doing this in the .htaccess.

@ArchBlood Thanks. I've founded this about your recommendation: https://stackoverflow.com/a/36705085

However, I've had a look to other Humhub instances I manage and files are already compressed!
The difference is the server, but I didn't add any extra code on the instances where compression works.

@luke- Do you know what to do to have gzip compression on assets?

  • Do you want me to add something about it on https://docs.humhub.org/ (I didn't find anything)?
  • Maybe we could add a check in Administration -> Information -> Prerequisistes?

@luke-
Copy link
Contributor

luke- commented Nov 12, 2023

@marc-farre It would probably be best, at least for the time being, to include notes and sample configurations in our wiki: https://community.humhub.com/s/installation-and-setup/wiki/Performance+and+scaling

With our cloud hosting, for example, compression is handled by the front-end proxy (NGINX).

Apart from that, we should perhaps include compression in the ".htaccess" or NGINX example configuration.

@ArchBlood
Copy link
Contributor

ArchBlood commented Nov 12, 2023

You should attach gzip to event handlers on Response, instead of doing this in the .htaccess.

@ArchBlood Thanks. I've founded this about your recommendation: https://stackoverflow.com/a/36705085

However, I've had a look to other Humhub instances I manage and files are already compressed! The difference is the server, but I didn't add any extra code on the instances where compression works.

@luke- Do you know what to do to have gzip compression on assets?

  • Do you want me to add something about it on https://docs.humhub.org/ (I didn't find anything)?
  • Maybe we could add a check in Administration -> Information -> Prerequisites?

Personally the event handler is the best option here with a check to make sure that gzencode, gzcompress, gzfile, ob_gzhandler, Phar::compress are all installed, and requires less work.

GZip Extensions Check

        // Checks GZip Extensions
        $title = 'PHP - ' . Yii::t('AdminModule.information', '{phpExtension} Extension', ['phpExtension' => 'GZip']);
        if (class_exists('gzencode' && 'gzcompress' && 'gzfile' && 'ob_gzhandler' && 'Phar::compress')) {
            $checks[] = [
                'title' => $title,
                'state' => 'OK'
            ];
        } else {
            $checks[] = [
                'title' => $title,
                'state' => 'ERROR',
                'hint' => Yii::t('AdminModule.information', 'Install {phpExtension} Extension', ['phpExtension' => 'PHP GZip'])
            ];
        }

If any of the gzip extensions aren't installed then GZip would be counterproductive if this were implemented anyway.

As for your other question, you can group asset bundles, but this would require a lot of work because you have to define each asset bundle into a group, I believe Yii has this documented in their docs.

@luke-
Copy link
Contributor

luke- commented Nov 13, 2023

@ArchBlood Thanks for the example. All files from the /static/ or /assets directory are not delivered by PHP or the HumHub application, but instead directly from the web server. Therefore, the webserver should also take over the transfer including compression.

@marc-farre
Copy link
Collaborator Author

@luke- PR #6654

@ArchBlood

check to make sure that gzencode, gzcompress, gzfile, ob_gzhandler, Phar::compress are all installed

This is done in .htaccess with <IfModule mod_deflate.c>

@luke-
Copy link
Contributor

luke- commented Nov 14, 2023

@marc-farre Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants