Skip to content

Commit cd69101

Browse files
ElviaBthmicrostudi
andauthored
Remove globally assigned mime type for text/javascript (#129)
* remove globally asigned mime type * add rake task * apply review suggestion * fix readme --------- Co-authored-by: Ivan Vergés <[email protected]>
1 parent 9c632e0 commit cd69101

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ Depending on your Decidim version, you can choose the corresponding version to e
5151
| 0.4.x | 0.29.x |
5252
5353
54+
## Required MIME types
55+
56+
Latests versions of PDFjs require that the web server to send the mime type `text/javascript` for files ending in `.mjs`
57+
58+
### Usage with Puma + Rack
59+
60+
If you are using Puma with Rack (< v3.1.0) you might have to add and initializer to ensure files with `.mjs` are served correctly. This is usually the most common approach when docker is used or when the ENV var `RAILS_SERVE_STATIC_FILES=true` is active.
61+
62+
Execute the following command to create an initializer that does that:
63+
64+
```
65+
bin/rails decidim_participatory_documents:install_mjs_initializer
66+
```
67+
5468
### Usage with external static file servers
5569
5670
A common setup is to use standalone web server (such as Nginx or Apache) to serve the static files present in the `public` folder.

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def install_module(path)
99
system("bundle exec rake decidim_participatory_documents:install:migrations")
1010
system("bundle exec rake db:migrate")
1111
system("bundle exec rake decidim_participatory_documents:install_pdf_js")
12+
system("bundle exec rake decidim_participatory_documents:install_mjs_initializer")
1213
end
1314
end
1415

lib/decidim/participatory_documents/engine.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ class Engine < ::Rails::Engine
3232
Rails.logger.error("Error while trying to include Decidim::ReportingProposals::ParticipatorySpaceUserRoleOverride: #{e.message}")
3333
end
3434

35-
# Older versions of Rack does not support the .mjs extension, so we register it here.
36-
initializer "decidim_participatory_documents.rack_mime" do
37-
Mime::Type.register "text/javascript", :mjs
38-
Rack::Mime::MIME_TYPES[".mjs"] = "text/javascript"
39-
end
40-
4135
initializer "decidim_participatory_documents.overrides", after: "decidim.action_controller" do
4236
config.to_prepare do
4337
Decidim::ParticipatorySpaceRoleConfig::Valuator.include(Decidim::ParticipatoryDocuments::ValuatorOverride)

lib/tasks/decidim_participatory_documents.rake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ namespace :decidim_participatory_documents do
88
apply_path_corrections_to_pdfjs "public"
99
end
1010

11+
# Since older versions of Rack does not support the .mjs extension, so we register it here."
12+
desc "Create rack mime type initializer for .mjs files"
13+
task :install_mjs_initializer do
14+
initializer_path = Rails.root.join("config/initializers/rack_mjs_type.rb")
15+
16+
if File.exist?(initializer_path)
17+
puts "⚠️ Initializer already exists at #{initializer_path}, skipping creation."
18+
next
19+
end
20+
21+
content = <<~RUBY
22+
Mime::Type.register "text/javascript", :mjs
23+
Rack::Mime::MIME_TYPES[".mjs"] = "text/javascript"
24+
RUBY
25+
26+
File.write(initializer_path, content)
27+
puts "Created initializer at #{initializer_path}"
28+
end
29+
1130
private
1231

1332
def decidim_participatory_documents_path
@@ -63,6 +82,7 @@ end
6382

6483
Rake::Task["decidim:upgrade:webpacker"].enhance do
6584
Rake::Task["decidim_participatory_documents:install_pdf_js"].invoke
85+
Rake::Task["decidim_participatory_documents:create_mjs_initializer"].invoke
6686
end
6787

6888
Rake::Task["decidim:choose_target_plugins"].enhance do

0 commit comments

Comments
 (0)