Assets Compile
When your app is running on AWS Lambda, the Jets helper methods like stylesheet_link_tag
, javascript_include_tag
, javascript_importmap_tags
, asset_path
, etc will point to the s3 bucket url. You can also configure settings so that the assets are served from a CDN like CloudFront that you manage yourself.
Compile Config
config/jets/deploy.rb
Jets.deploy.configure do
config.assets.build.enable = true
config.assets.build.precompile_command = "JETS_DOTENV=0 SECRET_KEY_BASE=dummy SECRET_KEY_BASE_DUMMY=1 bin/rails assets:precompile"
config.assets.build.nodejs.enable = "auto"
config.assets.build.nodejs.version = "20.12.1"
config.assets.upload.enable = true
config.assets.upload.folders = ["public"]
config.assets.upload.cache_control = nil
config.assets.upload.max_age = 3600
end
Rails Asset Pipeline
There are two ways you can use the Rails asset pipeline with Jets.
- Compiling assets locally.
- Compiling assets during docker build.
It works similar to Rails Asset Pipeline with Heroku with a few more configuration options.
Compiling assets locally
If a public/assets/manifest-*.js
is detected, Jets assumes that you have already precompiled assets yourself and will not attempt to compile assets.
Advantage: Compile assets locally before deployment may make sense if you have a complicated compiliation process and need more control over it. Additionally, compiling adds another 1m to the deploy overhead. You can get compiling out of the deployment band and decide when compilation is necessary.
Disadvantage: It’s one thing you have to do and take responsbility of. You need to install the toolchain and make sure it’s consistent among developers or on a dedicated CI machine.
Note: It is important to not .gitignore
or .dockerignore
the public/assets
folder if you want to use assets that you have compiled yourself. Otherwise, the files will not be present in the uploaded zip file to the remote runner. You can also use config.code.always_keep
, see: Code Zip.
Compiling assets remotely
When public/assets/manifest-*.js
is not detected, Jets will compile assets by running the bin/rails assets:precompile
command as part of the docker build
process.
Assets Upload
Jets will upload the assets files to s3 and configure JETS_ASSET_HOST to point to the s3 bucket so your Rails application can serve the assets out of s3, which is faster.
NodeJS
As of Rails 7, nodejs is no longer required to compile assets. See: Rails 7 will have three great answers to JavaScript in 2021+. If you need nodejs, see: Assets Nodejs
Reference
The table below covers each setting. Each option is configured with config.OPTION
. The config.
portion is not shown for conciseness. IE: logger.level
vs config.logger.level
.
Name | Default | Description |
---|---|---|
assets.build.detect_local | true | If public/assets/manifest-*.js is detected in the source code, Jets assumes you have locally precompiled assets and the remote runner will not attempt to compile assets in the remote runner docker build process. |
assets.build.enable | “conditional” | When Rails, it’s set to true. For other frameworks like sinatra, it’s set to false. |
assets.build.nodejs.enable | auto | The auto value means that it will be auto-detected. When a package.json is detected in the source code, it’ll assume you’ll need nodejs and yarn installed. |
assets.build.nodejs.version | 20.12.1 | The nodejs version to use. We try to default to the latest LTS node. |
assets.build.nodejs.deployment_stage | true | When is nodejs.enable , it will install for both build and deployment stages for the multi-stage docker build process. You can use this to disable it for the deployment phase. This means the Docker image you’re running won’t have access to the nodejs runtime. It’s only use to build artifacts. |
assets.build.precompile_command | “conditional” | When Rails, the default command is a jets precompile_assets.sh , which calls rails assets:precompile if it’s available. Otherwise, it is nil . |
assets.upload.cache_control | nil | The cache control header to use for assets uploaded to s3. Example: public, max-age=3600 . |
assets.upload.enable | true | Upload the assets from assets.upload.folders , IE: public, to s3. |
assets.upload.folders | [“public”] | Folders to upload to s3. |
assets.upload.max_age | 3600 | The max age in seconds for the cache control header. This is a shorter way to set the cache_control. IE: 3600 => public, max-age=3600 |