Skip to content

Add X-Fragments header support for rendering phlex fragments #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/views/posts/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Views
module Posts
class Index < Phlex::HTML
def view_template
div(id: "posts") do
fragment("post_1") do
div(id: "post_1") do
h1 { "Post 1" }
p { "This is the first post." }
end
end
fragment("post_2") do
div(id: "post_2") do
h1 { "Post 2" }
p { "This is the second post." }
end
end
end
end
end
end
end
15 changes: 14 additions & 1 deletion config/quickdraw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
class App < Rails::Application
config.eager_load = false
config.hosts.clear
config.active_support.to_time_preserves_timezone = :zone
config.autoload_paths << "#{root}/app/view_components"
config.secret_key_base = "secret-key"
config.action_dispatch.show_exceptions = :rescuable

routes.append do
resources :posts
end
end

class PostsController < ActionController::Base
def index
render Views::Posts::Index.new
end
end

App.initialize!

class Quickdraw::Test
Expand All @@ -35,4 +41,11 @@ def view_context
def controller
@controller ||= ActionView::TestCase::TestController.new
end

def get(url, headers:)
response = Rack::MockRequest.new(Rails.application).get(
url,
**headers.transform_keys { |key| "HTTP_#{key.to_s.upcase.tr('-', '_')}" }
)
end
end
6 changes: 4 additions & 2 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def render_in(view_context, &erb)
context = { rails_view_context: view_context }
end

fragments = context[:rails_view_context].request.headers.fetch("X-Fragments", "").split(",").map(&:strip).presence

if erb
call(context:) { |*args|
call(context:, fragments:) { |*args|
if args.length == 1 && Phlex::SGML === args[0] && !erb.source_location&.[](0)&.end_with?(".rb")
unbuffered = Phlex::Rails::Unbuffered.new(args[0])
raw(view_context.capture(unbuffered, &erb))
Expand All @@ -115,7 +117,7 @@ def render_in(view_context, &erb)
end
}.html_safe
else
call(context:).html_safe
call(context:, fragments:).html_safe
end
end

Expand Down
13 changes: 13 additions & 0 deletions test/fragments.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

test "rendering with an X-Fragments header" do
response = get("/posts", headers: { "X-Fragments" => "post_1" })

assert_equal response.status, 200
assert_equivalent_html response.body, <<~HTML.strip
<div id="post_1">
<h1>Post 1</h1>
<p>This is the first post.</p>
</div>
HTML
end