简体   繁体   English

如何在不阻塞页面加载的情况下加载大型 Javascript 文件

[英]How to load a large Javascript file without blocking the page load

I am using the Google Maps API in my app and have about 300 MB of coordinate data it will use to show features.我在我的应用程序中使用谷歌地图 API 并且有大约 300 MB 的坐标数据将用于显示功能。 I am trying to speed up the initial page load the first time a client uses it (the data will be downloaded, after that it's cached so will be faster).我正在尝试在客户端第一次使用它时加快初始页面加载速度(数据将被下载,之后会被缓存,因此速度会更快)。

The coordinate data is in several javascript files in JSON format and I am loading the js files using the HTML tag like so:坐标数据位于 JSON 格式的几个 javascript 文件中,我正在使用 HTML 标签加载 js 文件,如下所示:

<script type="text/javascript" src="JavaScript/file1.js" async></script>
<script type="text/javascript" src="JavaScript/file2.js" async></script>
...
<script type="text/javascript" src="JavaScript/map.js" async></script>

Each of the "filex.js" files just has a var whose value is the JSON formatted coordinates.每个“filex.js”文件只有一个 var,其值为 JSON 格式的坐标。 There are several because it appears browsers don't like to cache a js file over a certain size.有几个是因为浏览器似乎不喜欢缓存超过一定大小的 js 文件。 The map.js script uses those to add to the google maps api like this: map.js 脚本使用这些脚本添加到谷歌地图 api,如下所示:

layer.addGeoJson(file1var);
layer.addGeoJson(file2var);
...etc.

The problem is the download of those ~300 MB of scripts blocks the page from loading and it takes about a minute for the page to load.问题是下载大约 300 MB 的脚本会阻止页面加载,并且页面加载大约需要一分钟。 After they are cached it's all speedy the next time as you'd expect.在它们被缓存之后,下一次一切都会像您期望的那样快速。

What I want to do is allow the webpage to load and have everything functional EXCEPT for downloading those js files with the JSON data.我想要做的是允许网页加载并拥有所有功能,除了下载带有 JSON 数据的那些 js 文件。 I would like those to only be downloaded in the background after the rest of the page has already loaded so it's basically invisible to the user rather than a minute wait.我希望这些仅在页面的 rest 已经加载后才在后台下载,因此用户基本上看不到它,而不是等待一分钟。

You can see I have tried using the 'async' and 'defer' directives in the tag, but they did not do what I wanted (they still block the rest of the page loading).你可以看到我已经尝试在标签中使用'async'和'defer'指令,但他们没有做我想要的(他们仍然阻止页面加载的rest)。

Is there a way to allow those js files to download without blocking the rest of the page loading?有没有办法让那些js文件下载而不阻塞页面加载的rest?

Alternatively, can I remove those references from the HTML and instead have the map.js script request them when it needs them?或者,我可以从 HTML 中删除这些引用,而是让 map.js 脚本在需要它们时请求它们吗? It would be OK if it needed to download them the first time it needs them, but I would still need them to be cached so they are faster next time.如果它需要在第一次需要它们时下载它们就可以了,但我仍然需要缓存它们,以便下次它们更快。

I figured out what worked for me.我想出了什么对我有用。 I noticed in Chrome's dev tools network tab it showed when the 'DOMContentLoaded' event happened and it was waiting until after my files had all downloaded.我注意到在 Chrome 的开发工具网络选项卡中,它显示了“DOMContentLoaded”事件发生的时间,并且一直等到我的文件全部下载完毕。 Searching on that I found this post which helped me arrive at the answer.搜索后我发现这篇文章帮助我找到了答案。

However, the 'DOMContentLoaded' event was still too early and the page was still blocked from fully loading until the files were all downloaded.但是,“DOMContentLoaded”事件还为时过早,在文件全部下载之前,页面仍然无法完全加载。

I was able to make it work by instead listening for the page's 'onload' event which fires after everything is loaded and adding the scripts like this:我能够通过侦听页面的“onload”事件来使其工作,该事件在所有内容加载后触发并添加如下脚本:

window.addEventListener('load', (event) => {
  let script = document.createElement('script');
  script.src = "JavaScript/huc8-0.js";
  script.async = true;
  document.body.appendChild(script);
  // copy/paste the above 4 lines to add each of the files I needed
});

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

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