Skip to content

Commit

Permalink
Show generic index file instead of crashing (#963)
Browse files Browse the repository at this point in the history
* show generic index file if none is available

* Ensure generic 404 and 500 pages are working
  • Loading branch information
jaredcwhite authored Dec 20, 2024
1 parent e05a2ad commit 88f8899
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 45 deletions.
44 changes: 22 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/commands/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
17 changes: 10 additions & 7 deletions bridgetown-core/lib/roda/plugins/bridgetown_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"<h1>404 Not Found</h1>"
end
app.plugin :exception_page
app.plugin :error_handler do |e|
Expand All @@ -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"
"<h1>500 Internal Server Error</h1>"
end

# TODO: there may be a better way to do this, see `exception_page_css` instance method
Expand Down Expand Up @@ -123,17 +123,20 @@ 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)

request.halt [status, response_headers, body]
rescue StandardError => e
Bridgetown.logger.debug("Root handler error: #{e.message}")
response.status = 500
"<p>ERROR: cannot find <code>index.html</code> in the output folder.</p>"
"<p>ERROR: cannot serve the root <code>index</code> file.</p>"
end
end

Expand Down
127 changes: 127 additions & 0 deletions bridgetown-core/lib/roda/plugins/generic_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bridgetown has loaded</title>
<style>
html {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
a:link, a:visited {
color: rgb(30,40,38);
}
header {
margin-block: 2rem;
}
main {
margin-block: 2rem;
margin-inline: auto;
max-inline-size: 70ch;
}
</style>
</head>
<body>
<header style="text-align: center">
<svg aria-label="Bridgetown" width="300" viewBox="0 0 1863 572" version="1.1" xmlns="http://www.w3.org/2000/svg" style="display: inline-block; fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<g transform="matrix(1,0,0,1,-1520.67,-710.833)">
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,374.543,268.081)">
<path d="M0,13.784C1.387,13.784 2.283,12.968 2.283,11.664C2.283,10.48 1.387,9.502 0,9.502L-3.629,9.502L-3.629,13.784L0,13.784ZM-0.775,4.404C0.611,4.404 1.591,3.466 1.591,2.12C1.591,0.856 0.611,-0.122 -0.775,-0.122L-3.629,-0.122L-3.629,4.404L-0.775,4.404ZM-9.583,-5.342L0.286,-5.342C4.363,-5.342 7.626,-2.324 7.626,1.508C7.626,3.792 6.687,5.22 4.975,6.524L4.975,6.606C7.504,8.4 8.563,10.114 8.563,12.642C8.563,16.026 5.873,19.126 1.224,19.126L-9.583,19.126L-9.583,-5.342Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,404.955,276.605)">
<path d="M0,-3.264C1.345,-3.264 2.242,-4.65 2.242,-6.078C2.242,-7.504 1.182,-8.77 -0.165,-8.77L-4.813,-8.77L-4.813,-3.264L0,-3.264ZM-10.808,-13.866L-0.571,-13.866C4.688,-13.866 8.114,-10.686 8.114,-6.078C8.114,-1.796 5.954,0.61 2.323,1.508L8.931,10.602L1.427,10.602L-4.813,1.468L-4.813,10.602L-10.808,10.602L-10.808,-13.866Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,0,76.979)">
<rect x="424.149" y="185.76" width="5.995" height="24.468" style="fill:rgb(30,40,38);"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,450.609,268.203)">
<path d="M0,13.54C3.834,13.54 6.77,10.848 6.77,6.728C6.77,2.936 4.201,-0.04 0,-0.04L-2.406,-0.04L-2.406,13.54L0,13.54ZM-8.401,-5.464L0.123,-5.464C7.341,-5.464 12.928,-0.164 12.928,6.728C12.928,13.254 7.953,19.004 0.123,19.004L-8.401,19.004L-8.401,-5.464Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,485.832,288.024)">
<path d="M0,-26.101C4.527,-26.101 7.667,-24.429 10.236,-21.819L6.076,-17.985C4.404,-19.495 2.325,-20.433 0,-20.433C-4.364,-20.433 -7.177,-17.171 -7.177,-13.051C-7.177,-8.525 -3.956,-5.668 0.122,-5.668C4.404,-5.668 6.484,-8.035 6.646,-10.401L-0.326,-10.401L-0.326,-15.579L13.132,-15.579L13.132,-13.255C13.132,-6.037 8.89,0 0,0C-7.748,0 -13.254,-5.75 -13.254,-13.051C-13.254,-20.351 -7.748,-26.101 0,-26.101" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,507.961,287.207)">
<path d="M0,-24.468L16.191,-24.468L16.191,-19.168L5.995,-19.168L5.995,-14.762L15.417,-14.762L15.417,-9.542L5.995,-9.542L5.995,-5.138L16.64,-5.138L16.64,0L0,0L0,-24.468Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,539.963,281.907)">
<path d="M0,-13.868L-5.464,-13.868L-5.464,-19.168L11.583,-19.168L11.583,-13.868L6.036,-13.868L6.036,5.3L0,5.3L0,-13.868Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,574.253,267.591)">
<path d="M0,14.765C4.322,14.765 7.095,11.5 7.095,7.383C7.095,3.263 4.322,0 0,0C-4.364,0 -7.096,3.263 -7.096,7.383C-7.096,11.5 -4.364,14.765 0,14.765M0,-5.668C7.707,-5.668 13.172,0.082 13.172,7.383C13.172,14.683 7.707,20.433 0,20.433C-7.749,20.433 -13.173,14.683 -13.173,7.383C-13.173,0.082 -7.749,-5.668 0,-5.668" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,595.415,288.023)">
<path d="M0,-25.284L6.28,-25.284L11.216,-13.744L14.192,-21.41L15.683,-25.284L18.842,-25.284L20.489,-21.412L23.735,-13.784L28.221,-25.284L34.461,-25.284L24.266,0L23.449,0L17.413,-14.11L11.583,0L10.808,0L0,-25.284Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,639.899,287.207)">
<path d="M0,-24.468L6.322,-24.468L15.825,-9.95L15.825,-24.468L21.779,-24.468L21.779,0L15.539,0L5.995,-14.558L5.995,0L0,0L0,-24.468Z" style="fill:rgb(30,40,38);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,724.324,209.37)">
<path d="M0,29.41C-72.335,-66.167 -179.131,-55.549 -222.588,25.781C-227.373,34.738 -221.818,40.631 -219.109,39.985C-214.298,38.838 -197.926,32.314 -192.966,30.318C-190.894,29.485 -189.207,25.502 -187.375,21.877C-150.071,-51.956 -100.392,-60.124 -29.16,95.577C-32.607,77.837 -25.455,57.889 -2.274,37.237C0.394,34.86 2.589,32.831 0,29.41" style="fill:rgb(58,109,98);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(0.249711,-0.96832,-0.96832,-0.249711,486.176,197.302)">
<path d="M-12.21,-30.272C-28.365,-22.83 -44.336,-18.635 -46.819,-29.09C-48.223,-18.969 -9.68,-0.335 -12.21,21.62C-12.329,22.654 -10.713,22.914 -10.274,21.97C-1.487,3.05 -6.95,-23.042 -12.21,-30.272" style="fill:rgb(58,109,98);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(1,0,0,1,810.402,309.68)">
<path d="M0,-88.196C1.54,-88.196 2.104,-85.899 0.603,-85.55C-30.253,-78.37 -43.813,-48.307 -44.559,-31.561C-44.807,-26.006 -49.573,-25.025 -55.084,-22.473C-61.232,-19.625 -90.024,-7.312 -100.946,-3.404C-106.293,-1.49 -114.206,0.003 -114.576,-8.251C-116.052,-41.137 -81.521,-88.199 0,-88.196" style="fill:rgb(58,109,98);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(-126.776,-126.776,-126.776,126.776,684.306,302.422)">
<path d="M0.982,0.451C0.969,0.497 0.927,0.51 0.915,0.489C0.908,0.475 0.904,0.461 0.922,0.407C1.064,-0.03 0.945,-0.409 -0,-0.064C0.038,-0.102 0.068,-0.161 0.081,-0.234C0.45,-0.351 0.717,-0.297 0.868,-0.146C1.006,-0.008 1.047,0.21 0.982,0.451Z" style="fill:url(#_Linear1);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(0,-49.0052,-49.0052,0,756.207,273.884)">
<path d="M1,-0.813C0.989,-0.345 0.777,0.495 0.044,0.821C0.016,0.833 -0.014,0.798 0.007,0.775C0.271,0.479 0.447,0.027 0.429,-0.27C0.429,-0.28 0.427,-0.288 0.436,-0.292C0.61,-0.373 0.828,-0.554 0.969,-0.821C0.977,-0.837 1,-0.831 1,-0.814L1,-0.813Z" style="fill:url(#_Linear2);fill-rule:nonzero;"/>
</g>
</g>
<g transform="matrix(4.16667,0,0,4.16667,0,0)">
<g transform="matrix(30.5248,-8.50108,-8.50108,-30.5248,478.044,231.713)">
<path d="M0.489,-0.516C0.521,-0.344 0.815,-0.062 1.05,0.111C0.884,0.153 0.635,0.169 0.489,0.081C0.474,0.152 0.308,0.424 0.152,0.616C0.317,0.306 0.438,-0.424 0.489,-0.516Z" style="fill:url(#_Linear3);fill-rule:nonzero;"/>
</g>
</g>
</g>
<defs>
<linearGradient id="_Linear1" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(1,0,0,-1,0,0.100447)"><stop offset="0" style="stop-color:rgb(58,109,98);stop-opacity:1"/><stop offset="0.95" style="stop-color:rgb(44,73,73);stop-opacity:1"/><stop offset="1" style="stop-color:rgb(44,73,73);stop-opacity:1"/></linearGradient>
<linearGradient id="_Linear2" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(1,0,0,-1,0,-0.00347458)"><stop offset="0" style="stop-color:rgb(58,109,98);stop-opacity:1"/><stop offset="0.95" style="stop-color:rgb(44,73,73);stop-opacity:1"/><stop offset="1" style="stop-color:rgb(44,73,73);stop-opacity:1"/></linearGradient>
<linearGradient id="_Linear3" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(-0.268287,-0.963339,-0.963339,0.268287,0.634136,0.481664)"><stop offset="0" style="stop-color:rgb(58,109,98);stop-opacity:1"/><stop offset="0.95" style="stop-color:rgb(44,73,73);stop-opacity:1"/><stop offset="1" style="stop-color:rgb(44,73,73);stop-opacity:1"/></linearGradient>
</defs>
</svg>
<h1>The server has booted successfully.</h1>
</header>

<main>
<p>However, it seems like the <code>index</code> file is missing from your source folder.</p>

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

<p>Still stuck? <a href="https://www.bridgetownrb.com/community">Our developer community may be able to lend you a hand</a>. Good luck!</p>
</main>
</body>
</html>
5 changes: 3 additions & 2 deletions bridgetown-core/lib/roda/plugins/ssg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand All @@ -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))

Expand All @@ -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 = []

Expand Down
26 changes: 13 additions & 13 deletions bridgetown-website/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 88f8899

Please sign in to comment.