Skip to content

Commit

Permalink
Cache Webpacker builds between deploys
Browse files Browse the repository at this point in the history
 ## Goal
Facilitate Webpacker's existing caching to skip running Webpack on
builds where no changes are made.

 ## Approach
Cache Webpacker's default pack output and cache paths between deploys.

 ## Context
Currently all deploys using Webpacker will recompile on every deploy
even if there are no files changed. This is because the digest file and
packs directory is not cached between deploys.

By default, Webpacker stores a cache digest in `tmp/cache/webpacker` and
its packs in `public/packs`. Webpacker iterates over the watched paths to
compute a digest based on the mtime of the files. If none of the files
have changed and the packs directory is in place, running Webpack is
skipped, saving a lot of time (e.g. my company's Rails app takes 8
minutes to run webpack).

 ## Caveats
The environment variable `CI` needs to be set as the mtime does not
seem to be preserved correctly across deploys. If `ENV['CI']` is set,
Webpacker will use the file contents to compute the cache digest file
instead of the files' mtime.

I'm not familiar enough with the Heroku infrastructure to understand
why the mtime isn't being preserved. I did notice that `cp`ing is done
via `-a` which preserves timestamps via `--preserve=all` but that
doesn't seem to be enough.

 ## Further Notes
This could technically be solved without modifying either Webpacker or
the Heroku build pack but I thought this would be the best place as it
fixes all existing configurations provided they used the default
settings. This would also make the default "just work" instead of
requiring end users to configure their Rails apps specifically for
Heroku.

I didn't mean for this pull request to be the canonical place to fix
this issue but merely an issue report with a solution attached.

 ## Related
* rails/webpacker#1347

Signed-off-by: Andrew Kane <[email protected]>
  • Loading branch information
ericboehs authored and timm-oh committed Jan 24, 2020
1 parent fe5bf0f commit 49cc290
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/language_pack/rails4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def default_assets_cache
"tmp/cache/assets"
end

def public_packs_folder
"public/packs"
end

def default_webpacker_cache
"tmp/cache/webpacker"
end

def cleanup
super
return if assets_compile_enabled?
Expand All @@ -87,7 +95,9 @@ def run_assets_precompile_rake_task
topic("Preparing app for Rails asset pipeline")

@cache.load_without_overwrite public_assets_folder
@cache.load_without_overwrite public_packs_folder
@cache.load default_assets_cache
@cache.load default_webpacker_cache

precompile.invoke(env: rake_env)

Expand All @@ -100,7 +110,9 @@ def run_assets_precompile_rake_task

cleanup_assets_cache
@cache.store public_assets_folder
@cache.store public_packs_folder
@cache.store default_assets_cache
@cache.store default_webpacker_cache
else
precompile_fail(precompile.output)
end
Expand Down

0 comments on commit 49cc290

Please sign in to comment.