Sha256: 649d6484b2426027a54b5d5418f17f5fc9f6e3e3e7030c8176f11f5654888bb2

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require 'uri'

module RailsReactSSR
  class WebpackerUtils
    ##
    # Return the hashed name from the +bundle+
    def self.hashed_bundle_name!(bundle)
      Webpacker.manifest.lookup! bundle
    rescue Webpacker::Manifest::MissingEntryError
      raise RailsReactSSR::MissingBundleError.new(bundle, "The ReactJS package '#{bundle}' is missing from the manifest.json file.")
    end

    ##
    # Open the +bundle+ file for reading
    #
    # Returns IO stream with the +bundle+ contents. If +bundle+ cannot be found,
    # raises +RailsReactSSR::MissingBundleError+
    def self.open_bundle(bundle, max_tries: 10, delay: 1000)
      hashed = hashed_bundle_name! bundle

      if Webpacker.dev_server.running?
        dev_server_bundle hashed, max_tries, delay
      else
        local_file_bundle hashed
      end
    end

    private

    def self.dev_bundle_uri(path)
      URI::Generic.new(
          Webpacker.dev_server.protocol,
          nil,
          Webpacker.dev_server.host,
          Webpacker.dev_server.port,
          nil,
          path,
          nil,
          nil,
          nil
      ).to_s
    end

    def self.bundle_fullpath(path)
      File.join Rails.root, 'public', path
    end

    def self.dev_server_bundle(hashed_bundle, max_tries, delay, tries = 0)
      tries += 1

      uri = self.dev_bundle_uri hashed_bundle

      Rails.logger.debug "Reading remote bundle #{uri}"

      open uri
    rescue OpenURI::HTTPError => e
      # On the first page hit my not be available on the dev server so we need to wait for it to compile
      if tries < max_tries
        Rails.logger.debug "The remote bundle is not ready trying again in #{delay}ms - #{tries} of #{max_tries}"
        sleep delay / 1000
        retry
      else
        raise e
      end
    end

    def self.local_file_bundle(hashed_bundle)
      full_path = File.join Rails.root, 'public', hashed_bundle

      File.open full_path, 'rb'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails-react-ssr-0.1.2 lib/rails_react_ssr/webpacker_utils.rb
rails-react-ssr-0.1.1 lib/rails_react_ssr/webpacker_utils.rb
rails-react-ssr-0.1.0 lib/rails_react_ssr/webpacker_utils.rb