Skip to content

Commit

Permalink
Use importmap for avo stimulus component
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Feb 19, 2024
1 parent 11c620b commit ec4a166
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 16 deletions.
4 changes: 1 addition & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class ApplicationController < ActionController::Base
:self,
"'sha256-#{Digest::SHA256.base64digest(Rails.application.importmap.to_json(resolver: ApplicationController.helpers))}'",
"https://secure.gaug.es",
"https://www.fastly-insights.com",
"https://unpkg.com/@hotwired/stimulus/dist/stimulus.umd.js",
"https://unpkg.com/stimulus-rails-nested-form/dist/stimulus-rails-nested-form.umd.js"
"https://www.fastly-insights.com"
)
end

Expand Down
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)

import NestedForm from "stimulus-rails-nested-form";
application.register("nested-form", NestedForm);

// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
// lazyLoadControllersFrom("controllers", application)
7 changes: 0 additions & 7 deletions app/views/avo/partials/_head.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
<%= javascript_tag 'avo.custom', defer: true, type: :module, nonce: true do %>
import * as Stimulus from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.umd.js";
import * as NestedForm from "https://unpkg.com/stimulus-rails-nested-form/dist/stimulus-rails-nested-form.umd.js";

const application = window.Stimulus.Application.start();
application.register("nested-form", window.StimulusRailsNestedForm);
<% end %>
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
pin "github-buttons"
# vendored from github in the before times, not compatible with newest version without changes
pin "webauthn-json"
pin "stimulus-rails-nested-form", preload: false # @4.1.0
8 changes: 2 additions & 6 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
policy.object_src :none
# NOTE: This scirpt_src is overridden for all requests in ApplicationController
# This is the baseline in case the override is ever skipped
policy.script_src :self,
"https://secure.gaug.es",
"https://www.fastly-insights.com",
"https://unpkg.com/@hotwired/stimulus/dist/stimulus.umd.js",
"https://unpkg.com/stimulus-rails-nested-form/dist/stimulus-rails-nested-form.umd.js"
policy.style_src :self, "https://fonts.googleapis.com"
policy.script_src :self, "https://secure.gaug.es", "https://www.fastly-insights.com",
policy.style_src :self, "https://fonts.googleapis.com"
policy.connect_src :self, "https://s3-us-west-2.amazonaws.com/rubygems-dumps/", "https://*.fastly-insights.com", "https://fastly-insights.com",
"https://api.github.com", "http://localhost:*"
policy.form_action :self, "https://github.com/login/oauth/authorize"
Expand Down
2 changes: 2 additions & 0 deletions vendor/javascript/stimulus-rails-nested-form.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec4a166

Please sign in to comment.