koajs / static-cache

[MAINTAINERS WANTED] Static cache for koa
293 stars 47 forks source link

[bug] If I modified the file after loaded, chrome will throw ERR_CONTENT_LENGTH_MISMATCH error #97

Open vnoder opened 3 years ago

vnoder commented 3 years ago

If I modified the file after loaded, chrome will throw ERR_CONTENT_LENGTH_MISMATCH error

翻译:static-cache 加载完成文件后,如果我更新了文件,static-cache没有重新loadFile,就会出现文件信息和缓存不一致的情况,比如长度不一致,浏览器就会报 ERR_CONTENT_LENGTH_MISMATCH 错误

vnoder commented 3 years ago

this is the mini recurrence code

  1. step 1. node app.js
// app.js

const Koa = require('koa');
const path = require('path');
const app = module.exports = new Koa();

var staticCache = require('.');

function delay(duration) {
    return new Promise(function(resolve, reject){
        setTimeout(function(){
            resolve();
        }, duration)
    });
};

app.use(async function(ctx, next){
    await next();

   // do some thing async
    await delay(2000);
});

app.use(staticCache(path.join(__dirname, '.'), {
    preload: false,
    dynamic: true,
    buffer: false
}))

app.listen(3000, function(){
    console.log('http server listen ok')
});
// c.js
// 1
  1. step 2. I fetch c.js. It is ok

    $ curl http://127.0.0.1:3000/c.js
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
    100    24  100    24    0     0     11      0  0:00:02  0:00:02 --:--:--    11▒▒// 1
  2. step 3. I modify c.js to

// 1
// 2

then I fetch c.js. Error occured.

$ curl http://127.0.0.1:3000/c.js
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    24    0     0    0     0      0      0 --:--:--  0:00:07 --:--:--     0
curl: (18) transfer closed with 24 bytes remaining to read

May be, It is caused by

     // update file hash
    if (!file.md5) {
      var hash = crypto.createHash('md5')
      stream.on('data', hash.update.bind(hash))
      stream.on('end', function () {
        file.md5 = hash.digest('base64')
      })
    }