Class: Sitepress::Renderers::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/renderers/server.rb

Overview

Renders resources by invoking a rack call to the Rails application. From my experiments rendering as of 2021, this is the most reliable way to render resources. Rendering via ‘Renderers::Controller` has lots of various subtle issues that are surprising. People don’t like surprises, so I opted to render through a slightly heavier stack.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, rails_app = Rails.application) ⇒ Server

Returns a new instance of Server.



11
12
13
14
# File 'lib/sitepress/renderers/server.rb', line 11

def initialize(resource, rails_app = Rails.application)
  @rails_app = rails_app
  @resource = resource
end

Instance Attribute Details

#rails_appObject (readonly)

Returns the value of attribute rails_app.



9
10
11
# File 'lib/sitepress/renderers/server.rb', line 9

def rails_app
  @rails_app
end

#resourceObject (readonly)

Returns the value of attribute resource.



9
10
11
# File 'lib/sitepress/renderers/server.rb', line 9

def resource
  @resource
end

Instance Method Details

#renderObject



16
17
18
19
20
21
# File 'lib/sitepress/renderers/server.rb', line 16

def render
  code, headers, response = rails_app.routes.call env
  response.body
rescue => e
  raise RenderingError.new "Error rendering #{resource.request_path.inspect} at #{resource.asset.path.expand_path.to_s.inspect}:\n#{e.message}"
end