heroku_rails_deflate 项目教程
1. 项目的目录结构及介绍
heroku_rails_deflate
项目的目录结构如下:
heroku_rails_deflate/
├── lib/
│ └── heroku_rails_deflate/
│ ├── middleware.rb
│ └── version.rb
├── spec/
│ └── heroku_rails_deflate_spec.rb
├── .gitignore
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
└── heroku_rails_deflate.gemspec
目录结构介绍
- lib/: 包含项目的核心代码,包括自定义中间件
middleware.rb
和版本信息version.rb
。 - spec/: 包含项目的测试代码,
heroku_rails_deflate_spec.rb
是主要的测试文件。 - .gitignore: 指定 Git 版本控制系统忽略的文件和目录。
- Gemfile: 定义项目的依赖关系。
- Gemfile.lock: 锁定项目依赖的版本。
- LICENSE.txt: 项目的许可证文件。
- README.md: 项目的介绍和使用说明。
- Rakefile: 定义项目的 Rake 任务。
- heroku_rails_deflate.gemspec: 项目的 gemspec 文件,包含 gem 的元数据和依赖。
2. 项目的启动文件介绍
heroku_rails_deflate
项目没有传统的启动文件,因为它是一个 gem,主要通过 Gemfile
引入并在 Rails 应用中使用。
Gemfile
# Gemfile
gem 'heroku_rails_deflate', group: :production
在 Rails 应用的 Gemfile
中添加以上代码,然后运行 bundle install
即可安装并使用该 gem。
3. 项目的配置文件介绍
heroku_rails_deflate
项目的配置主要通过 Rails 应用的配置文件进行。
配置文件
在 config/environments/production.rb
中进行以下配置:
# config/environments/production.rb
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
# 可选:设置静态资源的缓存控制头
config.static_cache_control = "public, max-age=31536000"
配置说明
- config.serve_static_assets: 启用静态文件服务。
- config.assets.compress: 启用资产压缩。
- config.assets.compile: 启用资产编译。
- config.assets.digest: 启用资产摘要。
- config.static_cache_control: 设置静态资源的缓存控制头,默认缓存时间为一年。
通过以上配置,heroku_rails_deflate
将自动激活 Rack::Deflate
并为预编译的 gzip 资产提供服务,从而提高 Heroku 上 Rails 应用的性能。