From 88f88995f65f70ba7e6f2927e3d07a36db7b53a4 Mon Sep 17 00:00:00 2001 From: Jared White Date: Fri, 20 Dec 2024 09:27:48 -0800 Subject: [PATCH] Show generic index file instead of crashing (#963) * show generic index file if none is available * Ensure generic 404 and 500 pages are working --- Gemfile.lock | 44 +++--- .../lib/bridgetown-core/commands/build.rb | 2 +- .../lib/roda/plugins/bridgetown_server.rb | 17 ++- .../lib/roda/plugins/generic_index.html | 127 ++++++++++++++++++ bridgetown-core/lib/roda/plugins/ssg.rb | 5 +- bridgetown-website/Gemfile.lock | 26 ++-- 6 files changed, 176 insertions(+), 45 deletions(-) create mode 100644 bridgetown-core/lib/roda/plugins/generic_index.html diff --git a/Gemfile.lock b/Gemfile.lock index 2d6282ecb..113db07cc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,7 +60,7 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (7.2.2) + activesupport (7.2.2.1) base64 benchmark (>= 0.3) bigdecimal @@ -85,14 +85,14 @@ GEM builder (3.3.0) concurrent-ruby (1.3.4) connection_pool (2.4.1) - csv (3.3.0) + csv (3.3.1) diff-lcs (1.5.1) docile (1.4.1) drb (2.2.1) dry-inflector (1.1.0) e2mmap (0.1.0) - erubi (1.13.0) - faraday (2.12.1) + erubi (1.13.1) + faraday (2.12.2) faraday-net_http (>= 2.0, < 3.5) json logger @@ -109,7 +109,7 @@ GEM concurrent-ruby (~> 1.0) inclusive (1.0.0) jaro_winkler (1.6.0) - json (2.8.2) + json (2.9.1) kramdown (2.5.1) rexml (>= 3.3.9) kramdown-parser-gfm (1.1.0) @@ -119,26 +119,26 @@ GEM listen (3.9.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - logger (1.6.1) + logger (1.6.4) memory_profiler (1.1.0) mini_portile2 (2.8.8) - minitest (5.25.1) + minitest (5.25.4) minitest-profile (0.0.2) minitest-reporters (1.7.1) ansi builder minitest (>= 5.0) ruby-progressbar - net-http (0.5.0) + net-http (0.6.0) uri - nokogiri (1.16.7) + nokogiri (1.17.2) mini_portile2 (~> 2.8.2) racc (~> 1.4) - nokogiri (1.16.7-arm64-darwin) + nokogiri (1.17.2-arm64-darwin) racc (~> 1.4) - nokogiri (1.16.7-x86_64-darwin) + nokogiri (1.17.2-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.7-x86_64-linux) + nokogiri (1.17.2-x86_64-linux) racc (~> 1.4) nokolexbor (0.5.4) nokolexbor (0.5.4-arm64-darwin) @@ -161,28 +161,28 @@ GEM rb-inotify (0.11.1) ffi (~> 1.0) rbs (2.8.4) - regexp_parser (2.9.2) + regexp_parser (2.9.3) reverse_markdown (2.1.1) nokogiri - rexml (3.3.9) - roda (3.86.0) + rexml (3.4.0) + roda (3.87.0) rack rouge (4.5.1) rspec-mocks (3.13.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-support (3.13.1) - rubocop (1.68.0) + rspec-support (3.13.2) + rubocop (1.69.2) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 2.4, < 3.0) - rubocop-ast (>= 1.32.2, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.36.2, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.36.1) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.37.0) parser (>= 3.3.1.0) rubocop-bridgetown (0.6.1) rubocop (~> 1.23) @@ -191,7 +191,7 @@ GEM rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (1.13.0) - securerandom (0.3.2) + securerandom (0.4.1) serbea (2.2.0) erubi (>= 1.10) tilt (~> 2.0) diff --git a/bridgetown-core/lib/bridgetown-core/commands/build.rb b/bridgetown-core/lib/bridgetown-core/commands/build.rb index 1dc6e0adc..3f3b1ecc4 100644 --- a/bridgetown-core/lib/bridgetown-core/commands/build.rb +++ b/bridgetown-core/lib/bridgetown-core/commands/build.rb @@ -91,7 +91,7 @@ def build_site(config_options) # # @param options [Bridgetown::Configuration] options loaded from config and/or CLI def watch_site(config_options) - if config_options["url"].include?("://localhost") + if config_options["url"]&.include?("://localhost") require "socket" external_ip = Socket.ip_address_list.find do |ai| ai.ipv4? && !ai.ipv4_loopback? diff --git a/bridgetown-core/lib/roda/plugins/bridgetown_server.rb b/bridgetown-core/lib/roda/plugins/bridgetown_server.rb index b9a885f76..a2528077d 100644 --- a/bridgetown-core/lib/roda/plugins/bridgetown_server.rb +++ b/bridgetown-core/lib/roda/plugins/bridgetown_server.rb @@ -26,7 +26,7 @@ def self.load_dependencies(app) # rubocop:disable Metrics output_folder = Bridgetown::Current.preloaded_configuration.destination File.read(File.join(output_folder, "404.html")) rescue Errno::ENOENT - "404 Not Found" + "

404 Not Found

" end app.plugin :exception_page app.plugin :error_handler do |e| @@ -35,10 +35,10 @@ def self.load_dependencies(app) # rubocop:disable Metrics ) next exception_page(e) if ENV.fetch("RACK_ENV", nil) == "development" - output_folder = Bridgetown::Current.preloaded_configuration.destination + output_folder = self.class.opts[:bridgetown_preloaded_config].destination File.read(File.join(output_folder, "500.html")) rescue Errno::ENOENT - "500 Internal Server Error" + "

500 Internal Server Error

" end # TODO: there may be a better way to do this, see `exception_page_css` instance method @@ -123,9 +123,12 @@ def initialize_bridgetown_root # rubocop:todo Metrics/AbcSize hook_result = instance_exec(&self.class.opts[:root_hook]) if self.class.opts[:root_hook] next hook_result if hook_result - status, headers, body = self.class.opts[:ssg_server].serving( - request, File.join(self.class.opts[:ssg_root], "index.html") - ) + root_file = [ + File.join(self.class.opts[:ssg_root], "index.html"), + File.expand_path("generic_index.html", __dir__), + ].find { File.exist?(_1) } + + status, headers, body = self.class.opts[:ssg_server].serving(request, root_file) response_headers = response.headers response_headers.replace(headers) @@ -133,7 +136,7 @@ def initialize_bridgetown_root # rubocop:todo Metrics/AbcSize rescue StandardError => e Bridgetown.logger.debug("Root handler error: #{e.message}") response.status = 500 - "

ERROR: cannot find index.html in the output folder.

" + "

ERROR: cannot serve the root index file.

" end end diff --git a/bridgetown-core/lib/roda/plugins/generic_index.html b/bridgetown-core/lib/roda/plugins/generic_index.html new file mode 100644 index 000000000..c6dae5e4b --- /dev/null +++ b/bridgetown-core/lib/roda/plugins/generic_index.html @@ -0,0 +1,127 @@ + + + + + + Bridgetown has loaded + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

The server has booted successfully.

+
+ +
+

However, it seems like the index file is missing from your source folder.

+ +

If you add a file like index.erb or index.md to your source folder, + you'll be able to see that instead of this generic message.

+ +

Still stuck? Our developer community may be able to lend you a hand. Good luck!

+
+ + diff --git a/bridgetown-core/lib/roda/plugins/ssg.rb b/bridgetown-core/lib/roda/plugins/ssg.rb index 51aead908..a59a32f46 100644 --- a/bridgetown-core/lib/roda/plugins/ssg.rb +++ b/bridgetown-core/lib/roda/plugins/ssg.rb @@ -5,7 +5,7 @@ class Roda module RodaPlugins - # This is a simplifed and modified variant of Roda's Public core plugin. It adds additional + # This is a simplified and modified variant of Roda's Public core plugin. It adds additional # functionality so that you can host an entire static site through Roda. What's necessary for # this to work is handling "pretty" URLs, aka: # @@ -29,6 +29,7 @@ def ssg path = PARSER.unescape(real_remaining_path) return if path.include?("\0") + # @type [Rack::Files] server = roda_class.opts[:ssg_server] path = File.join(server.root, *segments_for_path(path)) @@ -40,7 +41,7 @@ def ssg halt [status, response_headers, body] end - # TODO: this could be refactored a bit + # TODO: this could be refactored a bit for clarity def segments_for_path(path) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity segments = [] diff --git a/bridgetown-website/Gemfile.lock b/bridgetown-website/Gemfile.lock index bacc5bab4..1acf2c53e 100644 --- a/bridgetown-website/Gemfile.lock +++ b/bridgetown-website/Gemfile.lock @@ -60,7 +60,7 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (7.2.2) + activesupport (7.2.2.1) base64 benchmark (>= 0.3) bigdecimal @@ -90,11 +90,11 @@ GEM nokogiri concurrent-ruby (1.3.4) connection_pool (2.4.1) - csv (3.3.0) + csv (3.3.1) drb (2.2.1) dry-inflector (1.1.0) - erubi (1.13.0) - faraday (2.12.1) + erubi (1.13.1) + faraday (2.12.2) faraday-net_http (>= 2.0, < 3.5) json logger @@ -109,7 +109,7 @@ GEM i18n (1.14.6) concurrent-ruby (~> 1.0) inclusive (1.0.0) - json (2.8.2) + json (2.9.1) kramdown (2.5.1) rexml (>= 3.3.9) kramdown-parser-gfm (1.1.0) @@ -118,14 +118,14 @@ GEM listen (3.9.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - logger (1.6.1) - minitest (5.25.2) - net-http (0.5.0) + logger (1.6.4) + minitest (5.25.4) + net-http (0.6.0) uri nio4r (2.7.4) - nokogiri (1.16.7-arm64-darwin) + nokogiri (1.17.2-arm64-darwin) racc (~> 1.4) - nokogiri (1.16.7-x86_64-linux) + nokogiri (1.17.2-x86_64-linux) racc (~> 1.4) nokolexbor (0.5.4-arm64-darwin) nokolexbor (0.5.4-x86_64-linux) @@ -144,14 +144,14 @@ GEM rb-inotify (0.11.1) ffi (~> 1.0) regexp_parser (2.1.1) - rexml (3.3.9) - roda (3.86.0) + rexml (3.4.0) + roda (3.87.0) rack rouge (4.5.1) ruby2js (5.1.2) parser regexp_parser (~> 2.1.1) - securerandom (0.3.2) + securerandom (0.4.1) serbea (2.2.0) erubi (>= 1.10) tilt (~> 2.0)