Skip to content

Commit

Permalink
Don't add ActionDispatch::Static middleware continued (#1892)
Browse files Browse the repository at this point in the history
* Don't add ActionDispatch::Static middleware

In the case that rails do not serve static files is not
necessary to add ActionDispatch::Static middleware. Making
possible to deploy ViewComponenets previews on a production
environment where Nginx or Apache is managing the static files.

* Apply suggestions from code review

Co-authored-by: Joel Hawksley <joelhawksley@github.com>

* Update index.md

* add: prism cdn if no asset precompiler found

* edit: change prism cdn version to 1.28

* Update app/helpers/preview_helper.rb

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* Update app/helpers/preview_helper.rb

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* Update app/helpers/preview_helper.rb

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* Update app/helpers/preview_helper.rb

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* Update lib/view_component/engine.rb

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* edit: fix brocken tests

* edit: refactor tests

* edit: fix for rails > 6.1

* edit: fix lint issues

* Update lib/view_component/engine.rb

Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>

---------

Co-authored-by: Daniel Gonzalez <danigonza86@gmail.com>
Co-authored-by: Joel Hawksley <joelhawksley@github.com>
Co-authored-by: Cameron Dutro <camertron@gmail.com>
Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
  • Loading branch information
5 people committed Nov 7, 2023
1 parent b11ba1f commit 00cdde8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
14 changes: 14 additions & 0 deletions app/helpers/preview_helper.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module PreviewHelper
include ActionView::Helpers::AssetUrlHelper if Rails.version.to_f < 6.1

AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml]
FALLBACK_LANGUAGE = "ruby"

Expand All @@ -10,6 +12,14 @@ def preview_source
render "preview_source"
end

def prism_css_source_url
serve_static_preview_assets? ? asset_path("prism.css", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/themes/prism.min.css"
end

def prism_js_source_url
serve_static_preview_assets? ? asset_path("prism.min.js", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/prism.min.js"
end

def find_template_data(lookup_context:, template_identifier:)
template = lookup_context.find_template(template_identifier)

Expand Down Expand Up @@ -62,4 +72,8 @@ def prism_language_name_by_template_path(template_file_path:)

language
end

def serve_static_preview_assets?
ViewComponent::Base.config.show_previews && Rails.application.config.public_file_server.enabled
end
end
4 changes: 2 additions & 2 deletions app/views/view_components/_preview_source.html.erb
@@ -1,4 +1,4 @@
<link href="<%= asset_path('prism.css', skip_pipeline: true) %>" media="screen" rel="stylesheet" type="text/css">
<link href="<%= prism_css_source_url %>" media="screen" rel="stylesheet" type="text/css">
<div class="view-component-source-example">
<h2>Source:</h2>
<pre class="source">
Expand All @@ -14,4 +14,4 @@
<% end %>
</pre>
</div>
<script type="text/javascript" src="<%= asset_path('prism.min.js', skip_pipeline: true) %>"></script>
<script type="text/javascript" src="<%= prism_js_source_url %>"></script>
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Expand Up @@ -10,6 +10,11 @@ nav_order: 5

## main

* Don't add ActionDispatch::Static middleware unless `public_file_server.enabled`.

*Daniel Gonzalez*
*Reegan Viljoen*

* Resolve an issue where slots starting with `call` would cause a `NameError`

*Blake Williams*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -204,6 +204,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/xronos-i-am?s=64" alt="xronos-i-am" width="32" />
<img src="https://avatars.githubusercontent.com/yykamei?s=64" alt="yykamei" width="32" />
<img src="https://avatars.githubusercontent.com/matheuspolicamilo?s=64" alt="matheuspolicamilo" width="32" />
<img src="https://avatars.githubusercontent.com/danigonza?s=64" alt="danigonza" width="32" />
<img src="https://avatars.githubusercontent.com/erinnachen?s=64" alt="erinnachen" width="32" />
<img src="https://avatars.githubusercontent.com/ihollander?s=64" alt="ihollander" width="32" />
<img src="https://avatars.githubusercontent.com/svetlins?s=64" alt="svetlins" width="32" />
Expand Down
6 changes: 5 additions & 1 deletion lib/view_component/engine.rb
Expand Up @@ -110,11 +110,15 @@ class Engine < Rails::Engine # :nodoc:
end

initializer "static assets" do |app|
if app.config.view_component.show_previews
if serve_static_preview_assets?(app.config)
app.middleware.use(::ActionDispatch::Static, "#{root}/app/assets/vendor")
end
end

def serve_static_preview_assets?(app_config)
app_config.view_component.show_previews && app_config.public_file_server.enabled
end

initializer "compiler mode" do |_app|
ViewComponent::Compiler.mode = if Rails.env.development? || Rails.env.test?
ViewComponent::Compiler::DEVELOPMENT_MODE
Expand Down
32 changes: 32 additions & 0 deletions test/sandbox/test/preview_helper_test.rb
Expand Up @@ -155,5 +155,37 @@ def test_raises_with_conflict_in_template_resolution
assert_equal("Found multiple templates for #{template_identifier}.", exception.message)
end
end

def test_prism_css_source_url_with_asset_pipeline
Rails.application.config.public_file_server.stub(:enabled, true) do
if Rails.version.to_f >= 6.1
assert_equal "/assets/prism.css", PreviewHelper.prism_css_source_url
else
assert_equal "/prism.css", PreviewHelper.prism_css_source_url
end
end
end

def test_prism_css_source_url_with_no_asset_pipeline
Rails.application.config.public_file_server.stub(:enabled, false) do
assert_equal "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/themes/prism.min.css", PreviewHelper.prism_css_source_url
end
end

def test_prism_js_source_with_asset_pipeline
Rails.application.config.public_file_server.stub(:enabled, true) do
if Rails.version.to_f >= 6.1
assert_equal "/assets/prism.min.js", PreviewHelper.prism_js_source_url
else
assert_equal "/prism.min.js", PreviewHelper.prism_js_source_url
end
end
end

def test_prism_js_source_url_with_no_asset_pipeline
Rails.application.config.public_file_server.stub(:enabled, false) do
assert_equal "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/prism.min.js", PreviewHelper.prism_js_source_url
end
end
end
end
10 changes: 10 additions & 0 deletions test/view_component/engine_test.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "test_helper"

class ViewComponent::EngineTest < ActionDispatch::IntegrationTest
def test_serve_static_previews?
app.config.public_file_server.enabled = false
refute ViewComponent::Engine.instance.serve_static_preview_assets?(app.config)
end
end

0 comments on commit 00cdde8

Please sign in to comment.