简体   繁体   English

如何为 Gitlab 页面启用 GZip 压缩?

[英]How to enable GZip compression for Gitlab Pages?

When using Gitlab Pages to render my site which is slow in page-ranking.当使用Gitlab Pages呈现我的网站时,页面排名很慢。 I can't find any solution on how to do following in GitLab ( non-enterprise version )我在 GitLab(非企业版)中找不到关于如何执行以下操作的任何解决方案

  1. Specify HTTP Cache Headers for various page resources like for an image, so that it can be cached.为各种页面资源(如图像)指定HTTP Cache Headers ,以便可以缓存它。

  2. Specify/Enable compression for GZip as page-ranking mentions compression disabled in gitlab.io.指定/启用 GZip 压缩,因为页面排名提到在 gitlab.io 中禁用压缩。

  1. It looks like specifying HTTP Cache Headers is still not possible.看起来仍然无法指定 HTTP 缓存标头。 But at least they have hardcoded "max-age=600" for all the resources here .但至少他们为这里的所有资源硬编码了“max-age=600”
  2. You can compress contents of your public folder via .gitlab-ci.yml :您可以通过.gitlab-ci.yml压缩公共文件夹的内容:

    script:脚本:

    • npm install安装
    • npm run build npm 运行构建
    • gzip -k -6 -r public gzip -k -6 -r 公共

GitLab has support for serving compressed assets if you pre-compress them in the pages CI Job already.如果您已经在pages CI 作业中预先压缩了压缩资产,GitLab 支持提供压缩资产。 See the documentation . 请参阅文档

Note that you can and also should use brotli compression as it's optimized for web content and supported by most modern browsers.请注意,您可以并且也应该使用brotli压缩,因为它针对 Web 内容进行了优化,并得到了大多数现代浏览器的支持。

There is also a suggested snippet for your .gitlab-ci.yml :您的.gitlab-ci.yml也有一个建议的片段:

pages:
  # Other directives
  script:
    # Build the public/ directory first
    - find public -type f -regex '.*\.\(htm\|html\|txt\|text\|js\|css\)$' -exec gzip -f -k {} \;
    - find public -type f -regex '.*\.\(htm\|html\|txt\|text\|js\|css\)$' -exec brotli -f -k {} \;

I haven't found a way of influencing cache behavior.我还没有找到影响缓存行为的方法。 I am also looking for this.我也在找这个。

Enable GZip compression for GitLab Pages为 GitLab 页面启用 GZip 压缩

If you add the precompressed .gz versions of the files of your static site, then nginx can serve them instead of the regular ones.如果您添加静态站点文件的预压缩.gz版本,则nginx可以提供它们而不是常规文件。 Add this line to your .gitlab-ci.yml file:将此行添加到您的.gitlab-ci.yml文件中:

image: alpine:latest

pages:
  stage: deploy
  script:
    - mkdir .temp
    - cp -r * .temp
    - mv .temp public
    - gzip -k -9 $(find public -type f)
  artifacts:
    paths:
      - public
  only:
    - master

This command compresses all files found in the public directory with the maximum compression ratio.此命令以最大压缩率压缩在public目录中找到的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM