fghrsh / live2d_demo

Live2D 看板娘插件 (https://www.fghrsh.net/post/123.html) 的前端 HTML 源码
https://live2d.fghrsh.net
GNU General Public License v2.0
1.39k stars 246 forks source link

在 Network 中看到很多重复API请求 #6

Closed imaegoo closed 4 years ago

imaegoo commented 4 years ago

Issue

在页面空闲时,会连续不断地请求 https://live2d.fghrsh.net/api/model/....mtn,导致自己博客的进度条异常闪动,也可能会增加 @fghrsh 的服务器的压力。

Workaround

我目前的解决方法是修改 live2d.js 里的 loadBytes 方法,记录相同请求的返回数据,在重复调用时直接返回之前记录的数据,测试有效。

index b69e93e..ed1aaa4 100644
@@ -3981,11 +3981,18 @@ default = o;
    }), i.
 default = r;
    var o = e(2);
+   var requestCache = {};
    r.prototype.loadBytes = function(t, i) {
+       // Cache 相同的请求,减少请求数量
+       if (requestCache[t] !== undefined) {
+           i(requestCache[t]);
+           return;
+       }
        var e = new XMLHttpRequest;
        e.open("GET", t, !0), e.responseType = "arraybuffer", e.onload = function() {
            switch (e.status) {
            case 200:
+               requestCache[t] = e.response;
                i(e.response);
                break;
            default:

20200412214823

imaegoo commented 4 years ago

Over 7 days, closed.

fghrsh commented 4 years ago

的确,为了降低流量负担,加上 302 跳转之后会产生回源请求 ...

0933b321ce5b5eb6b166046102ab38c4.png

a0f9d6d4860bda67c5ef8f9e1f9a0734.png

@imaegoo 你提一下 live2d.js 的 pr ?我合并一下 ...