I'm writing an app in Rails 7 (was working only on lower versions before, <= 5 ) and I encountered a weird assets behavior. There is a delay in applying assets. I have multiple link_to
's with remote: true
option and when the page is reloaded and i click on link too early it behaves like normal link (not remote) and sends html request (not js). The delay is very significant, I encounter this problem very often. There's also a visible delay in applying css - I can see unstyled page for 0.5/1 second and then styles appear. Images also renders very slowly, but that's maybe because they are in quite high resolution.
Here's some code:
importmap.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "jquery", to: "jquery.min.js", preload: true
pin "jquery_ujs", to: "jquery_ujs.js", preload: true
pin "bootstrap", to: "https://ga.jspm.io/npm:bootstrap@5.2.1/dist/js/bootstrap.esm.js"
pin "@popperjs/core", to: "https://ga.jspm.io/npm:@popperjs/core@2.11.6/lib/index.js"
pin "chosen-jquery", to: "chosen-jquery"
pin "flatpickr", to: "flatpickr"
pin_all_from "app/javascript/custom", under: "custom"
application.js
import "@hotwired/turbo-rails"
import "controllers"
import "jquery"
import "jquery_ujs"
import 'bootstrap'
import 'chosen-jquery'
import 'flatpickr'
import "custom/choosen"
import "custom/datepicker"
import "custom/login_banner"
import "custom/mobile_menu"
import "custom/forms"
one of js files for example
window.form_small_img_display = function() {
$("#user_avatar").change(function(){
const [file] = this.files
if (file) {
$(this).closest(".form-img-display-js").find("img.small-form-img-js").attr("src", URL.createObjectURL(file))
// $("img.small-form-avatar").attr("src", URL.createObjectURL(file))
}
})
};
window.close_inline_form = function(){
$(".close-inline-form-js > a").on("click", function(e){
e.preventDefault();
$(".inline-expedition-logs-form").html("");
})
$(".close-inline-update-form-js > a").on("click", function(e){
e.preventDefault();
parent = $($(this).data('id')).parent();
$($(this).data('id')).remove();
parent.prepend($(this).data('partial'))
})
}
$(document).on('turbo:load', window.form_small_img_display);
$(document).on('turbo:load', window.close_inline_form);
manifest.js
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link chosen-jquery.js
//= link flatpickr
application.html.erb layout is not changed for js
I'm not very familiar with the new approach to js in rails 7, so I'm not sure what to do with that, if even I can do anything about it. Maybe it's just on my local machine. My only suspicion is that the images are too large and take a long time to load, which slows down the loading of the js. I'm just curious why it applies to the remote: true
option.