如何为Gitlab Pages启用GZip压缩?

8
当使用 Gitlab Pages 渲染我的网站时,页面排名较慢。我无法在 GitLab(非企业版)中找到以下解决方案:
  1. 指定各种页面资源的 HTTP 缓存头信息,例如图片,以便可以缓存。

  2. 指定/启用 GZip 的压缩方式,因为页面排名表明 gitlab.io 上未启用压缩。

3个回答

5
  1. 看起来目前还无法指定HTTP缓存头。但至少他们在这里对所有资源硬编码了"max-age=600"。
  2. 你可以通过.gitlab-ci.yml压缩你的public文件夹中的内容:

    脚本:

    • npm install
    • npm run build
    • gzip -k -6 -r public

Alpine 的构建将失败,因为它没有 -r 选项。我使用了常规的 node:12,它使用 debian,这样就可以工作了。 - Dylan Landry

3
GitLab支持在CI作业的pages中预先压缩静态资源以提供压缩服务。详情请查看文档
请注意,您还应该使用优化了网页内容且被大多数现代浏览器支持的压缩。
同时,本文还提供了一个建议的.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 {} \;

我还没有找到影响缓存行为的方法。我也在寻找这个。


1

启用GitLab Pages的GZip压缩

如果您添加了静态站点文件的预压缩.gz版本,则nginx可以提供它们而不是常规文件。将以下行添加到您的.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

此命令使用最大压缩比压缩在public目录中找到的所有文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接