Skip to content

Commit

Permalink
Serve gem css, including Bootstrap, via routes/controller (#16)
Browse files Browse the repository at this point in the history
* Serve gem css, including Bootstrap, via routes/controller

# Conflicts:
#	config/routes.rb

* Remove application.css

* Fix rubocop violations
  • Loading branch information
bensheldon authored Nov 28, 2024
1 parent a221459 commit b78027b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
1 change: 0 additions & 1 deletion app/assets/config/inner_performance_manifest.js

This file was deleted.

Empty file.
Empty file.
32 changes: 32 additions & 0 deletions app/controllers/inner_performance/frontends_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module InnerPerformance
class FrontendsController < ApplicationController
protect_from_forgery with: :exception
skip_after_action :verify_same_origin_request, raise: false

before_action do
expires_in 1.year, public: true
end

class << self
def asset_path(path)
Engine.root.join("app/frontend/inner_performance", path)
end
end

STATIC_ASSETS = {
css: {
bootstrap: asset_path("vendor/bootstrap.min.css"),
},
}.freeze

def static
file_stem = params[:stem]&.to_sym
file_format = params[:format]&.to_sym

file_path = STATIC_ASSETS.dig(file_format, file_stem) || raise(ActionController::RoutingError, "Not Found")
send_file(file_path, disposition: "inline")
end
end
end
5 changes: 5 additions & 0 deletions app/frontend/inner_performance/vendor/bootstrap.min.css

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions app/views/layouts/inner_performance/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag "inner_performance/application", media: "all" %>

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<%= tag.link rel: "stylesheet", href: frontend_static_path(:bootstrap, format: :css), nonce: content_security_policy_nonce %>
</head>
<body>

Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
resources :events, only: [:index]

root to: "dashboard#index"

scope :frontend, controller: :frontends, defaults: { version: InnerPerformance::VERSION.tr(".", "-") } do
get "static/:version/:stem", action: :static, as: :frontend_static
end
end

0 comments on commit b78027b

Please sign in to comment.