Skip to content

Commit

Permalink
Switch to .bridgetown-cache/live_reload.txt as the file to watch fo…
Browse files Browse the repository at this point in the history
…r live reload

Also some routes plugin-related fixes

Fixes #940
  • Loading branch information
jaredcwhite committed Dec 1, 2024
1 parent 7f48f7e commit 884b333
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
17 changes: 12 additions & 5 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,18 @@ def sanitized_path(base_directory, questionable_path)
#
# @return [String] the path to the cached errors file
def build_errors_path
File.join(
(Bridgetown::Current.site&.config || Bridgetown::Current.preloaded_configuration).root_dir,
".bridgetown-cache",
"build_errors.txt"
)
site_config = Bridgetown::Current.site&.config || Bridgetown::Current.preloaded_configuration
File.join(site_config.root_dir, site_config.cache_dir, "build_errors.txt")
end

# This file gets touched each time there's a new build, which then triggers live reload
# in the browser.
#
# @see Bridgetown::Rack::Routes.setup_live_reload
# @return [String] the path to the empty file being watched
def live_reload_path
site_config = Bridgetown::Current.site&.config || Bridgetown::Current.preloaded_configuration
File.join(site_config.root_dir, site_config.cache_dir, "live_reload.txt")
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def fast_refresh(paths = [], reload_if_needed: false) # rubocop:todo Metrics
@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
Expand All @@ -28,7 +29,13 @@ def fast_refresh(paths = [], reload_if_needed: false) # rubocop:todo Metrics
self, layout.instance_variable_get(:@base), layout.name
)
end
next unless found_res.any? || pages.any?

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
Expand All @@ -48,7 +55,7 @@ def fast_refresh(paths = [], reload_if_needed: false) # rubocop:todo Metrics
end

marked_resources = resources.select(&:fast_refresh_order).sort_by(&:fast_refresh_order)
if full_abort || (marked_resources.empty? && !found_gen_pages)
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
Expand All @@ -62,13 +69,14 @@ def fast_refresh(paths = [], reload_if_needed: false) # rubocop:todo Metrics

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

liquid_renderer.reset
transform_resources_for_fast_refresh(marked_resources, found_gen_pages)
transform_generated_pages_for_fast_refresh

FileUtils.touch(in_destination_dir("index.html"))
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
touch_live_reload_file
end

private
Expand Down
6 changes: 6 additions & 0 deletions bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def write
write_redirecting_index if config.prefix_default_locale

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

# Yields all content objects while looping through {#generated_pages},
Expand Down Expand Up @@ -69,4 +70,9 @@ def write_redirecting_index
File.write(in_dest_dir("index.html"), index_html, mode: "wb")
end
end

def touch_live_reload_file
FileUtils.mkdir_p File.dirname(Bridgetown.live_reload_path)
FileUtils.touch Bridgetown.live_reload_path
end
end
3 changes: 1 addition & 2 deletions bridgetown-core/lib/bridgetown-core/rack/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def load_all(roda_app)
# @param app [Roda]
def setup_live_reload(app) # rubocop:disable Metrics
sleep_interval = 0.5
file_to_check = File.join(Bridgetown::Current.preloaded_configuration.destination,
"index.html")
file_to_check = Bridgetown.live_reload_path
errors_file = Bridgetown.build_errors_path

app.request.get "_bridgetown/live_reload" do
Expand Down
4 changes: 4 additions & 0 deletions bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def view(view_class: Bridgetown::ERBView)
module RequestMethods
# This runs through all of the routes in the manifest, setting up Roda blocks for execution
def file_routes
base_path = Bridgetown::Current.preloaded_configuration.base_path.delete_prefix("/")

scope.routes_manifest.routes.each do |route|
file, localized_file_slugs, segment_keys = route

localized_file_slugs.each do |slug|
on("") { scope.run_file_route(file, slug:) } if slug == "index" && !base_path.empty?

# This sets up an initial Roda route block at the slug, and handles segments as params
#
# _routes/nested/[slug].erb -> "nested/:slug"
Expand Down

0 comments on commit 884b333

Please sign in to comment.