简体   繁体   English

Javascript 随机未加载到 Ruby on Rails 5.2.3 生产模式

[英]Javascript randomly not loading on Ruby on Rails 5.2.3 Production Mode

I am still a newbie at Ruby on Rails.我仍然是 Ruby on Rails 的新手。 This framework has been really amazing for me.这个框架对我来说真的很棒。 However, since I uploaded it into the production, I still do not understand why Ruby on Rails 5.2.3 on some random occasion does not load JavaScript properly.但是,自从我将其上传到生产中后,我仍然不明白为什么Ruby on Rails 5.2.3 在某些随机情况下无法正确加载JavaScript。 Sometimes it loads properly at the first try on opening the page, but when it does not, user has to refresh even sometimes several times until it is loaded properly.有时它在第一次尝试打开页面时正确加载,但如果没有,用户有时甚至必须刷新几次,直到它正确加载。 Also I get error from the browser where it says我也从浏览器那里收到错误消息

GET .../assets/jquery-ui-1.8.16.custom.css net::ERR_ABORTED 500 GET .../assets/jquery-ui-1.8.16.custom.css net::ERR_ABORTED 500

when I did not in any way import or even writes a single line of code to import that file since I used the JQuery UI 1.12.1 loaded from CDN.当我没有以任何方式导入甚至编写一行代码来导入该文件时,因为我使用了从 CDN 加载的 JQuery UI 1.12.1。

What is really important is, why Ruby on Rails 5.2.3 on some random occasions does not load the JavaScript properly?真正重要的是,为什么 Ruby on Rails 5.2.3 在某些随机情况下无法正确加载 JavaScript?

Not only does it happen on production mode, also on the development mode this problem occurs on random ocassions as well.它不仅发生在生产模式上,而且在开发模式下,这个问题也会在随机情况下发生。

I will post the application.js and application.scss and also the head part of the application.html.erb to give the insights of what I am doing wrong in this case:我将发布 application.js 和 application.scss 以及 application.html.erb 的头部部分,以了解我在这种情况下做错了什么:

application.js应用程序.js

//= require jquery3
//= require jquery-ui/core
//= require jquery-ui/widgets/slider
//= require rails-ujs
//= require popper
//= require cocoon
//= require social-share-button
//= require social-share-button/wechat
//= require rippleria/js/jquery.rippleria.min
//= require_tree .

application.scss应用程序.scss

/*
 *= require gmaps-auto-complete
 *= require social-share-button
 *= require jquery-ui/core
 *= require jquery-ui/slider
 */

 @import "pretty-checkbox/dist/pretty-checkbox";
 @import "rippleria/css/jquery.rippleria";

application.html.erb应用程序.html.erb

<%= stylesheet_link_tag 'application', media: 'all' %> <%= stylesheet_link_tag 'application', media: 'all' %>

<%= stylesheet_link_tag    'sub/style' %>
<%= stylesheet_link_tag    'sub/components.min' %>
<%= stylesheet_link_tag    'sub/custom' %>
<%= stylesheet_link_tag    'sub/chat.min' %>

<%= javascript_include_tag 'application', async: Rails.env.production? %>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/galleria/1.5.7/galleria.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.3/flatpickr.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

I would hugely appreciate it if you guys could let me know what solutions I can do in order to avoid such random behaviors?如果你们能让我知道我可以采取哪些解决方案来避免这种随机行为,我将不胜感激? Thank you very much for your time.非常感谢您的宝贵时间。 :) :)

Never mind, I already solved the problem.没关系,我已经解决了这个问题。 The main issue that Ruby on Rails had is that if we use the JQuery provided by Rails 5.2.3 by the dependency written below Ruby on Rails 的主要问题是,如果我们通过下面编写的依赖项使用 Rails 5.2.3 提供的 JQuery

gem 'jquery-rails'

it will generate the application CSS file with the following line它将使用以下行生成应用程序 CSS 文件

@import 'jquery-ui.1.8.6.custom.css'

that involuntarily imports JQuery UI Custom CSS although the assets folder does not have the file written above尽管assets文件夹没有上面写的文件,但不由自主地导入了JQuery UI自定义CSS

SOLUTION:解决方案:

I would recommend you AGAINST using the JQuery provided by Rails 5.2.3 and change the JQuery using CDN method for best performance.我建议您反对使用 Rails 5.2.3 提供的 JQuery,并使用 CDN 方法更改 JQuery 以获得最佳性能。 So you can remove the following line on application.js所以你可以删除application.js上的以下行

//= require jquery3

then load order of JQuery before application.js on application.html.erb like the following line:然后在application.html.erb上的application.js之前加载 JQuery 的顺序,如下所示:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<%= javascript_include_tag 'application', async: Rails.env.production?, :cache => 'cached/all' %>

Hope my solution could help.希望我的解决方案能有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM