Sha256: 0ea7997a25fff847b56995b3efca798e147f4fc17c7e1bdfa30d2705bd63dbf1

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    module DevServer
      class HotReload
        def initialize(ctx, app, theme:, watcher:, ignore_filter: nil)
          @ctx = ctx
          @app = app
          @theme = theme
          @streams = SSE::Streams.new
          @watcher = watcher
          @watcher.add_observer(self, :notify_streams_of_file_change)
          @ignore_filter = ignore_filter
        end

        def call(env)
          if env["PATH_INFO"] == "/hot-reload"
            create_stream
          else
            status, headers, body = @app.call(env)

            body = inject_hot_reload_javascript(body) if request_is_html?(headers)

            [status, headers, body]
          end
        end

        def close
          @streams.close
        end

        def notify_streams_of_file_change(modified, added, _removed)
          files = (modified + added).reject { |file| @ignore_filter&.ignore?(file) }
            .map { |file| @theme[file].relative_path }

          unless files.empty?
            @streams.broadcast(JSON.generate(modified: files))
            @ctx.debug("[HotReload] Modified #{files.join(", ")}")
          end
        end

        private

        def request_is_html?(headers)
          headers["content-type"]&.start_with?("text/html")
        end

        def inject_hot_reload_javascript(body)
          hot_reload_js = ::File.read("#{__dir__}/hot-reload.js")
          hot_reload_script = "<script>\n#{hot_reload_js}</script>"
          body = body.join.gsub("</body>", "#{hot_reload_script}\n</body>")

          [body]
        end

        def create_stream
          stream = @streams.new

          @ctx.debug("[HotReload] Connected to SSE stream")

          [
            200,
            {
              "Content-Type" => "text/event-stream",
              "Cache-Control" => "no-cache",
              "webrick.chunked" => true,
            },
            stream,
          ]
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
shopify-cli-2.8.0 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.7.4 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.7.3 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.7.2 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.7.1 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.7.0 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.6 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.5 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.4 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.3 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.2 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.1 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.6.0 lib/shopify_cli/theme/dev_server/hot_reload.rb
shopify-cli-2.5.0 lib/shopify_cli/theme/dev_server/hot_reload.rb