Skip to content
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

Don't add ActionDispatch::Static middleware continued #1892

Merged
merged 22 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1d67670
Don't add ActionDispatch::Static middleware
danigonza Oct 7, 2021
a57894a
Apply suggestions from code review
danigonza Sep 21, 2022
90fffb8
Merge branch 'main' into fix/static-assets-conf
danigonza Sep 21, 2022
07d3200
Update index.md
danigonza Sep 21, 2022
5bc580f
Merge branch 'main' into fix/static-assets-conf
danigonza Sep 22, 2022
66d0800
Merge branch 'main' into fix/static-assets-conf
reeganviljoen Nov 1, 2023
99d64f4
add: prism cdn if no asset precompiler found
reeganviljoen Nov 1, 2023
e979f41
edit: change prism cdn version to 1.28
reeganviljoen Nov 1, 2023
2d57de0
Update app/helpers/preview_helper.rb
reeganviljoen Nov 2, 2023
541f7db
Update app/helpers/preview_helper.rb
reeganviljoen Nov 2, 2023
7e9251b
Update app/helpers/preview_helper.rb
reeganviljoen Nov 2, 2023
cee8b12
Update app/helpers/preview_helper.rb
reeganviljoen Nov 2, 2023
8029c6c
Update lib/view_component/engine.rb
reeganviljoen Nov 2, 2023
02f7f32
edit: fix brocken tests
reeganviljoen Nov 2, 2023
07f726b
Merge branch 'main' into fix/static-assets-conf
reeganviljoen Nov 2, 2023
de40a07
edit: refactor tests
reeganviljoen Nov 2, 2023
670578c
edit: fix for rails > 6.1
reeganviljoen Nov 4, 2023
073c724
Merge branch 'main' into fix/static-assets-conf
reeganviljoen Nov 4, 2023
d8aac0b
edit: fix lint issues
reeganviljoen Nov 4, 2023
7a7098f
Merge branch 'main' into fix/static-assets-conf
reeganviljoen Nov 6, 2023
fe88feb
Merge branch 'main' into fix/static-assets-conf
reeganviljoen Nov 7, 2023
996f130
Update lib/view_component/engine.rb
camertron Nov 7, 2023
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
14 changes: 14 additions & 0 deletions app/helpers/preview_helper.rb
Original file line number Diff line number Diff line change
@@ -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/[email protected]/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/[email protected]/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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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/[email protected]/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/[email protected]/prism.min.js", PreviewHelper.prism_js_source_url
end
end
end
end
10 changes: 10 additions & 0 deletions test/view_component/engine_test.rb
Original file line number Diff line number Diff line change
@@ -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