Module: Bridgetown::Site::FastRefreshable

Included in:
Bridgetown::Site
Defined in:
bridgetown-core/lib/bridgetown-core/concerns/site/fast_refreshable.rb

Instance Method Summary collapse

Instance Method Details

#fast_refresh(paths = [], reload_if_needed: false) ⇒ Object

rubocop:todo Metrics



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/fast_refreshable.rb', line 7

def fast_refresh(paths = [], reload_if_needed: false) # rubocop:todo Metrics
  FileUtils.rm_f(Bridgetown.build_errors_path)

  @fast_refresh_ordering = 0
  full_abort = false
  found_gen_pages = false
  found_route_file = false
  paths.each do |path| # rubocop:todo Metrics
    found_res = resources.select do |resource|
      resource.id.start_with?("repo://") && in_source_dir(resource.relative_path) == path
    end

    layouts_to_reload = Set.new
    locate_resource_layouts_and_partials_for_fash_refresh(path, layouts_to_reload) unless
      found_res.any?

    locate_components_for_fast_refresh(path) unless found_res.any?

    pages = locate_layouts_and_pages_for_fast_refresh(path, layouts_to_reload)

    layouts_to_reload.each do |layout|
      layouts[layout.label] = Bridgetown::Layout.new(
        self, layout.instance_variable_get(:@base), layout.name
      )
    end

    if config.key?(:routes) # carve out fast refresh track for the routes plugin
      found_route_file = config.routes.source_paths.any? do |routes_dir|
        path.start_with?(in_source_dir(routes_dir))
      end
    end
    next unless found_res.any? || pages.any? || found_route_file

    if pages.any?
      found_gen_pages = true
      mark_original_page_resources_for_fast_refresh(pages)
      next
    end

    found_res.each do |res|
      res.prepare_for_fast_refresh!.tap { full_abort = true unless _1 }
      next unless res.collection.data?

      res.collection.merge_data_resources.each do |k, v|
        data[k] = v
        signals[k] = v
      end
    end
  end

  marked_resources = resources.select(&:fast_refresh_order).sort_by(&:fast_refresh_order)
  if full_abort || (marked_resources.empty? && !found_gen_pages && !found_route_file)
    # Darn, a full reload is needed (unless we're on a super-fast track)
    if reload_if_needed
      Bridgetown::Hooks.trigger :site, :pre_reload, self, paths
      Bridgetown::Hooks.clear_reloadable_hooks
      loaders_manager.reload_loaders
      Bridgetown::Hooks.trigger :site, :post_reload, self, paths
      process # bring out the big guns
    end
    return
  end

  Bridgetown::Hooks.trigger :site, :fast_refresh, self

  unless found_route_file
    liquid_renderer.reset
    transform_resources_for_fast_refresh(marked_resources, found_gen_pages)
    transform_generated_pages_for_fast_refresh
  end

  Bridgetown::Hooks.trigger :site, :post_write, self
  Bridgetown.touch_live_reload_file
end