我放弃了通过WebPacker How do 我加一句 jQuery to Mastodon 4.02 (using Rails 6 and Webpacker 4)?将jQuery添加到Mastodon 4.0.2的try

因此,现在我只想在Mastodon头文件application.html.haml中添加一个指向jQuery CDN的链接.

我加一句

= javascript_pack_tag "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"

设置为application.html.haml,然后运行

RAILS_ENV=production bundle exec rake tmp:cache:clear
RAILS_ENV=production bundle exec rails assets:generate_static_pages
RAILS_ENV=production bundle exec rails assets:precompile

然后从根帐户退出并运行

systemctl restart mastodon-*

but Mastodon crashes with the app's generic error screen.

Edit 2/1/23

vbl.使用

%script src: "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"

也坠毁了乳齿象.

Questions:

• How can I simply add a lnk to the jQuery CDN in the header of Mastodon?

Edit 2/6/23 alex points out that the CDN will be blocked due to CSP headers (you could add an exception in Nginx, but that's for another question); the best solution is to use alex's Sprockets answer below.

• Where are the error logs that would show me more info on the crash?

Edit 2/6/23 The most relevant logs are viewed by running. journalctl -f -u mastodon-web.

which outputs:

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]ActionView::模板::错误 (不一致的缩进:1个制表符用于缩进,但其余的 该文档使用2个空格缩进.)

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]32:=Java脚本包标签 "Locale_#{I18n.Locale}",交叉登录:‘匿名

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]33:= JAVASCRIPT_INCLUDE_标记"jQuery"

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]34:=CSRF_META_TAG

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]35:%META{名称: ‘style-nonce’,内容:请求.Content_Security_Policy_Nonce}

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]36

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]37:=style heet_link_tag ‘/inet.css’,跳过管道:TRUE,媒体:‘ALL’,id:‘惰性样式’

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]38:=style heet_link_tag 自定义css路径,跳过管道:真,主机:根URL,媒体:‘全部’

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06]

Feb 06 15:12:28 ExampleMastodon包[9057]: [44fb4365-61e3-4bbb-b495-704b84c0ba06] app/views/layouts/application.html.haml:35

which shows the cause of the crash: "Inconsistent indentation"

Arrgg...

Fixed that and it works.


这是Github的application.html.haml:https://github.com/mastodon/mastodon/blob/a5a00d7f7adff5e0afbd23ac1e1b16120137509a/app/views/layouts/application.html.haml

!!! 5
%html{ lang: I18n.locale }
  %head
    %meta{ charset: 'utf-8' }/
    %meta{ name: 'viewport', content: 'width=device-width, initial-scale=1' }/

    - if cdn_host?
      %link{ rel: 'dns-prefetch', href: cdn_host }/
      %meta{ name: 'cdn-host', content: cdn_host }/

    - if storage_host?
      %link{ rel: 'dns-prefetch', href: storage_host }/

    %link{ rel: 'icon', href: '/favicon.ico', type: 'image/x-icon' }/

    - %w(16 32 48).each do |size|
      %link{ rel: 'icon', sizes: "#{size}x#{size}", href: asset_pack_path("media/icons/favicon-#{size}x#{size}.png"), type: 'image/png' }/

    - %w(57 60 72 76 114 120 144 152 167 180 1024).each do |size|
      %link{ rel: 'apple-touch-icon', sizes: "#{size}x#{size}", href: asset_pack_path("media/icons/apple-touch-icon-#{size}x#{size}.png") }/

    %link{ rel: 'mask-icon', href: asset_pack_path('media/images/logo-symbol-icon.svg'), color: '#6364FF' }/
    %link{ rel: 'manifest', href: manifest_path(format: :json) }/
    %meta{ name: 'theme-color', content: '#191b22' }/
    %meta{ name: 'apple-mobile-web-app-capable', content: 'yes' }/

    %title= content_for?(:page_title) ? safe_join([yield(:page_title).chomp.html_safe, title], ' - ') : title

    = stylesheet_pack_tag 'common', media: 'all', crossorigin: 'anonymous'
    = stylesheet_pack_tag current_theme, media: 'all', crossorigin: 'anonymous'
    = javascript_pack_tag 'common', crossorigin: 'anonymous'
    = javascript_pack_tag "locale_#{I18n.locale}", crossorigin: 'anonymous'
    = csrf_meta_tags
    %meta{ name: 'style-nonce', content: request.content_security_policy_nonce }

    = stylesheet_link_tag '/inert.css', skip_pipeline: true, media: 'all', id: 'inert-style'
    = stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'

    = yield :header_tags

  %body{ class: body_classes }
    = content_for?(:content) ? yield(:content) : yield

    .logo-resources{ 'tabindex' => '-1', 'inert' => true, 'aria-hidden' => true }
      = render_symbol :icon
      = render_symbol :wordmark

推荐答案

成功地让乳齿象在开发模式下流浪运行,但在生产中应该是一样的,你只需预编译并重新启动即可.


Webpack

$ yarn add jquery
Done in 999.72s # <= at least it worked on the first try

Add it to plugins:
https://github.com/mastodon/mastodon/blob/v4.0.2/config/webpack/shared.js#L69

// config/webpack/shared.js

plugins: [
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
  }),
  ...

100

这意味着您的模块中会自动包含$jQuery.每application.js人中有console.log($);人会给我这样的结果:

ƒ jQuery(selector, context) {                                 application.js?2e28:4 
      // The jQuery object is actually just the init constructor 'enhanced'
      // Need init if jQuery is called (just allow error to be thrown if not included)
      re…

如果需要,您可以try 将jquery添加到Window:

// app/javascript/application.js

window.$ = window.jQuery = jQuery;
//                         ^ this is automatically given by wepback.ProvidePlugin

现在您在模块之外有$jQuery:

// app/views/layouts/application.html.haml

:javascript
  console.log($) // works in development

我认为,由于CSP政策,它将在生产中失败,所以没有内联脚本.


CDN

# app/views/layouts/application.html.haml

= javascript_include_tag "https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js"

or

%script{ src: "https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js" }

网络选项卡显示不起作用的原因:

jquery.min.js          (blocked:csp)        script

内容安全策略配置为不接受来自外部主机的脚本,并且仅为样式设置了nonce:

100

你正在寻找script_src配置,它在那里的几个地方,我认为更新这section应该足够了:

if Rails.env.development?
  webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" }

  p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls

  p.script_src  :self, :unsafe_inline, :unsafe_eval, assets_host, host_to_url("cdn.jsdelivr.net")
  # this one worked for me                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  p.child_src   :self, :blob, assets_host
  p.worker_src  :self, :blob, assets_host
else
  p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url

  p.script_src  :self, assets_host, "'wasm-unsafe-eval'", host_to_url("cdn.jsdelivr.net")
  # this one should work for you                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  p.child_src   :self, :blob, assets_host
  p.worker_src  :self, :blob, assets_host
end

主页是explore,如果你在网络选项卡中点击它,你可以看到响应头:

# just showing the relevant part
Content-Security-Policy:
# this is present only in development, that means no inline scripts in production
#                   vvvvvvvvvvvvvvv
  script-src 'self' 'unsafe-inline' 'unsafe-eval' http://mastodon.local http://cdn.jsdelivr.net;
# ^^^^^^^^^^                                                            ^^^^^^^^^^^^^^^^^^^^^^^

https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
https://api.rubyonrails.org/classes/ActionDispatch/ContentSecurityPolicy.html


链轮

下载jqueryvendor/assets/javascripts/jquery.js并添加到预编译中:

# config/initializers/assets.rb

Rails.application.config.assets.precompile += ["jquery.js"]

现在您不需要与CSP打交道:

# app/views/layouts/application.html.haml

= javascript_include_tag "jquery"

在生产中

To double check, nginx.conf should be configured to serve assets from public/assets:
https://github.com/mastodon/mastodon/blob/v4.0.2/dist/nginx.conf#L67

Logger is set to STDOUT:
https://github.com/mastodon/mastodon/blob/v4.0.2/config/environments/production.rb#L26

根据系统和配置值的不同,查找日志(log)是GoFish的任务.它说的第一件事是journalctl -u mastodon-web来查看日志(log):

root@mastodon:~$ journalctl -u mastodon-web
No journal files were found.
-- No entries --

# nothing ever works the first time

root@mastodon:~$ systemctl restart systemd-journald
root@mastodon:~$ systemctl restart mastodon-web.service
root@mastodon:~$ journalctl -f -u mastodon-web
-- Logs begin at Mon 2023-02-06 11:01:52 UTC. --
Feb 06 11:05:29 mastodon systemd[1]: Started mastodon-web.
Feb 06 11:05:30 mastodon bundle[10473]: [10473] Puma starting in cluster mode...
Feb 06 11:05:30 mastodon bundle[10473]: [10473] * Puma version: 5.6.5 (ruby 3.0.4-p208) ("Birdie's Version")
# ...
# refresh the page after puma boots all the way, you should get something here
# in case that ^ didn't help, try this:
tail -f /var/log/syslog

我将需要查看您将进一步帮助的错误.

mastodon@mastodon:~/live$ RAILS_ENV=production bin/rails assets:precompile
yarn install v1.22.19
Done in 2.50s.
INFO -- : Writing /home/mastodon/live/public/assets/jquery-6c97f936439a920f7055f372924fa39fb6acaec5a89af8467f0847ebdbe047ee.js
INFO -- : Writing /home/mastodon/live/public/assets/jquery-6c97f936439a920f7055f372924fa39fb6acaec5a89af8467f0847ebdbe047ee.js.gz
root@mastodon:~$ systemctl restart mastodon-web.service
# journalctl -f -u mastodon-web

method=GET path=/explore format=html controller=HomeController action=index status=200 duration=424.95 view=364.50 db=40.52
                                                                            ^^^^^^^^^^
# tail -f /var/log/nginx/access.log

"GET /assets/jquery-6c9...47ee.js HTTP/2.0" 200 31123
             ^^^^^^                         ^^^

Jquery相关问答推荐

ASP.NET Core 8 MVC:从jQuery发布控制器中的所有值为空

提交表单时运行自定义验证脚本

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

$ 不是函数 - jQuery 错误

将 HTML5 Canvas 转换为要上传的文件?

使用 jQuery 按文本内容 Select 选项

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

如何使用 jQuery 清空输入值?

不使用插件的 jQuery 缓动函数

使用 jQuery 将行添加到表的 tbody

按值对 Html Select 的选项进行排序的最有效方法是什么,同时保留当前选定的项目?

$.each(selector) 和 $(selector).each() 有什么区别

jquery beforeunload 关闭(不离开)页面时?

Jquery 和 HTML FormData 返回未捕获的 TypeError:非法调用

在jQuery中将逗号添加到数字

Javascript 使用哈希值重新加载页面

javascript过滤对象数组

当 iframe 在 jQuery 中完成加载时,如何触发事件?

JavaScript 等效于 jQuery 的扩展方法

在 jquery 中使用 AJAX Post 从强类型 MVC3 视图传递模型的正确方法