Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

【中间件】协商缓存

  • 安装

    npm i -S koa-conditional-get koa-etag
    
  • 使用写法

    import conditional from "koa-conditional-get";
    import etag from "koa-etag";
    
    app.use(conditional());
    app.use(etag());
    
    • 这里conditional需要配合etag,防止资源变了,但修改时间没变
      • 会给我们的返回头加上ETag
      • 发出的请求也会自动携带If-None-Match
    • 这里如果单独请求资源,也是无效的,需要html内置的js和css才容易生效
  • 注意!!!

    • 这两个中间件,必须要在 koa-static-server之前插入,否则无效