Skip to content

Commit

Permalink
only read webpack/gulp manifests 1x/req even in dev
Browse files Browse the repository at this point in the history
We already have logic in here to only read these once ever in prod
but in dev mode we were reading the files multiple times. We purposely
don’t want to cache it for the entire life of the rails process in dev
mode so that if you change anything and have gulp or webpack running
in watch mode, it picks up on your change. but the way it was, it would
read these json files multiple times per request. I didn’t care
because it was dev mode. But this should make even dev mode a little 
faster while still being able to handle picking up any changes between
requests

Change-Id: Ic0ee13df1727e998b828eec012fc2701cf5f8ab7
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/218100
Tested-by: Jenkins
Tested-by: Service Cloud Jenkins <[email protected]>
Reviewed-by: Clay Diffrient <[email protected]>
QA-Review: Steven Burnett <[email protected]>
Product-Review: Ryan Shaw <[email protected]>
  • Loading branch information
ryankshaw committed Nov 22, 2019
1 parent cae08c5 commit e56c941
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions lib/canvas/cdn/rev_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ module Canvas
module Cdn
module RevManifest
class << self
include ActiveSupport::Benchmarkable

# ActiveSupport::Benchmarkable#benchmark needs a `logger` defined
def logger
Rails.logger
end

def include?(source)
if webpack_request?(source)
Expand Down Expand Up @@ -93,27 +99,35 @@ def url_for(source)
private
def load_gulp_data_if_needed
return if ActionController::Base.perform_caching && defined? @gulp_manifest
file = Rails.root.join('public', 'dist', 'rev-manifest.json')
if file.exist?
Rails.logger.debug "reading rev-manifest.json"
@gulp_manifest = JSON.parse(file.read).freeze
elsif Rails.env.production?
raise "you need to run `gulp rev` first"
else
@gulp_manifest = {}.freeze
RequestCache.cache("rev-manifest") do
benchmark("reading rev-manifest") do
file = Rails.root.join('public', 'dist', 'rev-manifest.json')
if file.exist?
Rails.logger.debug "reading rev-manifest.json"
@gulp_manifest = JSON.parse(file.read).freeze
elsif Rails.env.production?
raise "you need to run `gulp rev` first"
else
@gulp_manifest = {}.freeze
end
@gulp_revved_urls = Set.new(@gulp_manifest.values.map{|s| "/dist/#{s}" }).freeze
end
end
@gulp_revved_urls = Set.new(@gulp_manifest.values.map{|s| "/dist/#{s}" }).freeze
end

def load_webpack_data_if_needed
return if (ActionController::Base.perform_caching || webpack_prod?) && defined? @webpack_manifest
file = Rails.root.join('public', webpack_dir, 'webpack-manifest.json')
if file.exist?
Rails.logger.debug "reading #{file}"
@webpack_manifest = JSON.parse(file.read).freeze
else
raise "you need to run webpack" unless Rails.env.test?
@webpack_manifest = Hash.new(["Error: you need to run webpack"]).freeze
RequestCache.cache("webpack_manifest") do
benchmark("reading webpack_manifest") do
file = Rails.root.join('public', webpack_dir, 'webpack-manifest.json')
if file.exist?
Rails.logger.debug "reading #{file}"
@webpack_manifest = JSON.parse(file.read).freeze
else
raise "you need to run webpack" unless Rails.env.test?
@webpack_manifest = Hash.new(["Error: you need to run webpack"]).freeze
end
end
end
end

Expand Down

0 comments on commit e56c941

Please sign in to comment.