-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significant refactor of the Routes plugin (#882)
* Significant refactor of the Routes plugin - More modular and OOP - Better Roda plugin layout - Dynamic `index` route now supported instead of static index.html - Downstream plugins can leverage file-based routing themselves * Move monkey-patched Roda Public code into streamlined standalone SSG plugin - Also serve static `index.html` using the same `Rack::Files` server * # SSR mode and Fast Refresh mode are mutually exclusive * add comment * Simplify by moving more Roda-specific code into the server plugin
- Loading branch information
1 parent
1cdd32b
commit 24b39fa
Showing
13 changed files
with
285 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
bridgetown-core/lib/bridgetown-core/rack/static_indexes.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require "uri" | ||
require "rack/files" | ||
|
||
class Roda | ||
module RodaPlugins | ||
# This is a simplifed 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: | ||
# | ||
# /path/to/page -> /path/to/page.html or /path/to/page/index.html | ||
# /path/to/page/ -> /path/to/page/index.html | ||
# | ||
# It does not support serving compressed files, as that should ideally be handled through a | ||
# proxy or CDN layer in your architecture. | ||
module SSG | ||
PARSER = URI::DEFAULT_PARSER | ||
|
||
def self.configure(app, opts = {}) | ||
app.opts[:ssg_root] = app.expand_path(opts.fetch(:root, "public")) | ||
app.opts[:ssg_server] = Rack::Files.new(app.opts[:ssg_root]) | ||
end | ||
|
||
module RequestMethods | ||
def ssg | ||
return unless is_get? | ||
|
||
path = PARSER.unescape(real_remaining_path) | ||
return if path.include?("\0") | ||
|
||
server = roda_class.opts[:ssg_server] | ||
path = File.join(server.root, *segments_for_path(path)) | ||
|
||
return unless File.file?(path) | ||
|
||
status, headers, body = server.serving(self, path) | ||
response_headers = response.headers | ||
response_headers.replace(headers) | ||
halt [status, response_headers, body] | ||
end | ||
|
||
# TODO: this could be refactored a bit | ||
def segments_for_path(path) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | ||
segments = [] | ||
|
||
path.split("/").each do |seg| | ||
next if seg.empty? || seg == "." | ||
|
||
seg == ".." ? segments.pop : segments << seg | ||
end | ||
|
||
path = File.join(roda_class.opts[:ssg_root], *segments) | ||
unless File.file?(path) | ||
path = File.join(path, "index.html") | ||
if File.file?(path) | ||
segments << "index.html" | ||
else | ||
segments[segments.size - 1] = "#{segments.last}.html" | ||
end | ||
end | ||
|
||
segments | ||
rescue IndexError | ||
nil | ||
end | ||
end | ||
end | ||
|
||
register_plugin :ssg, SSG | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ output | |
|
||
# Dependency folders | ||
node_modules | ||
vendor | ||
|
||
# Caches | ||
.sass-cache | ||
|
Oops, something went wrong.