spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

Some files pushed via node-spdy can not be found in browser cache #262

Open lewispham opened 8 years ago

lewispham commented 8 years ago

I've tried to serve some files via http2. Here is my code.

Server

if (req.url === '/') {
  let files = [
    'avatar.png',
    'avatar180.png',
    'bcrypt.js',
    'favicon-32x32.png',
    'language.js',
    'logo.png',
    'logo320.png',
    'main.css',
    'main.htm',
    'privacy.htm',
    'svw.js',
    'sw.js',
    'terms.htm',
    'xregexp.js'
  ];
  for (let name of files) {
    // all those files are stored in memory.
    // `CACHE` object is their container
    let file = CACHE[name];
    if (file) {
      let push = res.push('/app/' + name, {
        response: file.headers
      });
      push.on('error', er => console.log(er));
      push.end(file.content);
    }
  }
  let file = CACHE['main.htm'];
  res.writeHead(200, file.headers);
  res.end(file.content);
}

Client

[
  'avatar.png',
  'avatar180.png',
  'bcrypt.js',
  'favicon-32x32.png',
  'language.js',
  'logo.png',
  'logo320.png',
  'main.css',
  'main.htm',
  'privacy.htm',
  'svw.js',
  'sw.js',
  'terms.htm',
  'xregexp.js'
].forEach(name => fetch('/app/' + name));

Result untitled

As you can see from the above result, 6 out of 14 files are not fetched from cache. And I still can't figure out the reason behind this strange behaviour.

P/s. This test is run on Chrome v51.0.2704.106 m (64-bit), Node.js v6.2.2 and node-spdy v3.3.3

vigneshshanmugam commented 8 years ago

@tresdin Since you are running Service worker script on the page, resources will be fetched through that. Can you post the sw.js snippet which you have on the page.

lewispham commented 8 years ago

@vigneshshanmugam Those requests won't be fetched through ServiceWorker simply because the client script is executed within the ServiceWorker.

lewispham commented 8 years ago

Anyone interested in confirming this bug?

sericaia commented 7 years ago

@tresdin I think I had the same problem. You're missing cache headers or your don't have a secure connection implemented.

See the 2nd hint (in the bottom) from this blogpost https://blog.yld.io/2017/03/01/optimize-with-http-2-server-push-and-service-workers/

I hope it helps!

lewispham commented 7 years ago

@sericaia Thanks for the information. But I do fetch those files via HTTPS connections and they are also cached for 60 seconds (use Cache-Control header). From my above example, all of the file have the same caching header, but only 8 out of 14 files are cached properly. It's the primary reason making me believe it's a bug belonging to spdy.