【问题标题】:Rails 6 + Webpacker Lazy LoadingRails 6 + Webpacker 延迟加载
【发布时间】:2019-11-18 21:26:59
【问题描述】:

我正在运行rails assets:precompile,它会输出:

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
    This can impact web performance.
    Assets: 
      js/application-c4e0541a90e01b47fbfe.js (1.21 MiB)
      js/chartkick-b109b176a3de896848c7.js (657 KiB)
      js/datatables-fca8769ee16df57bdc4a.js (296 KiB)
      js/application-c4e0541a90e01b47fbfe.js.gz (305 KiB)
      js/datatables-fca8769ee16df57bdc4a.js.map.gz (482 KiB)
      js/chartkick-b109b176a3de896848c7.js.map.gz (493 KiB)
      js/application-c4e0541a90e01b47fbfe.js.map.gz (1.09 MiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
  application (1.21 MiB)
      js/application-c4e0541a90e01b47fbfe.js
  chartkick (657 KiB)
      js/chartkick-b109b176a3de896848c7.js
  datatables (296 KiB)
      js/datatables-fca8769ee16df57bdc4a.js


WARNING in webpack performance recommendations: 
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/

我一直在研究延迟加载,但找不到任何关于 Rails 的信息(我对 webpacker、js 等非常陌生)。 例如,link 解释了拆分代码和延迟加载,但我不知道如何在我的超级简单代码上调用它。

这是我拥有的数据表代码示例:

//Global setting and initializer
// Add DataTables jQuery plugin
require('imports-loader?define=>false!datatables.net')(window, $)
require('imports-loader?define=>false!datatables.net-select')(window, $)
require('imports-loader?define=>false!datatables.net-bs4')(window, $)
require('imports-loader?define=>false!datatables.net-select-bs4')(window, $)

// Load datatables styles
 import 'datatables.net-bs4/css/dataTables.bootstrap4.css'
 import 'datatables.net-select-bs4/css/select.bootstrap4.css'

$(document).on('turbolinks:load', () => {
  $(document.body).on('click', '#check_all', () => {
    var checkBoxes = $('input[type="checkbox"]')
    checkBoxes.prop("checked", !checkBoxes.prop("checked"))
  })
})


// init on turbolinks load
$(document).on('turbolinks:load', function() {
  if (!$.fn.DataTable.isDataTable(".datatable")) {
    $(".datatable").DataTable();
  }
});

// turbolinks cache fix
$(document).on('turbolinks:before-cache', function() {
  var dataTable = $($.fn.dataTable.tables(true)).DataTable();
  if (dataTable !== null) {
    dataTable.destroy();
    return dataTable = null;
  }
});

很想得到一些指示,它阻塞了我的应用程序并且应用程序崩溃了。 谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails webpack webpacker ruby-on-rails-6


    【解决方案1】:

    要添加动态导入,您可能需要对现有代码进行一些更改:

    1. 提取用于有条件地初始化插件实例的函数
    2. 对每个数据表模块或导入这些模块的单独文件使用 import().then() 动态导入语句。

    提取一个初始化函数

    由于您将异步导入数据表,因此您不能指望它会被加载。这只是一个示例,但处理此问题的一种方法是在您想要进行数据表初始化的任何地方使用可重用函数(也许还有另一个用于“缓存前”挂钩),如果尚未加载数据表,它将正常运行。

    function initializeDatatables() {
      if (!$.fn.DataTable) return;
    
      if (!$.fn.DataTable.isDataTable(".datatable")) {
        $(".datatable").DataTable();
      }
    }
    

    使用动态导入

    由于您有四个单独的导入,您可以尝试将它们作为一个“块”从单独的文件中导入:

    // app/javascript/datatables.js
    require('imports-loader?define=>false!datatables.net')(window, $)
    require('imports-loader?define=>false!datatables.net-select')(window, $)
    require('imports-loader?define=>false!datatables.net-bs4')(window, $)
    require('imports-loader?define=>false!datatables.net-select-bs4')(window, $)
    
    // your original initializer file
    import('./datatables').then(initializeDatatables)
    
    $(document).on('turbolinks:load', initializeDatatables);
    

    您也可以尝试使用splitChunks api,这样 Webpack 会负责将您的 JS 放入单独的块中,但这是一个单独的主题。

    【讨论】:

    • 非常感谢您的详细回答!我完全按照你说的做了,但是包含了一个 console.log ,它何时加载,甚至在没有数据表的页面上它仍然会加载它。我错过了什么吗?还要感谢关于 splitchunks 的提示 :)
    • 如果您希望数据表块仅在具有 .datatable 类的页面上加载,那么您需要使用类似以下伪代码的内容来包装导入函数:“在 DOM 加载时,如果 $( '.datatable').length, import('./datatables').then...”
    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2018-12-11
    • 2020-01-31
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多