美文网首页
前后端分离系列-缓存篇

前后端分离系列-缓存篇

作者: 汤姆猫_6074 | 来源:发表于2020-01-21 19:16 被阅读0次

demo https://github.com/757566833/webpack-guide

1.我们需要在静态文件返回给浏览器的时候header中的缓存变长,这样就可以在本地秒加载,省去了带宽和加载时间
修改server.js

const path = require('path');
const Koa = require('koa');
const send = require('koa-send');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
// 匹配了所有url 
router.get(['/', '/**'], async (ctx) => {
  const url = ctx.path;
  await send(ctx, './index.html', {
      root: path.join(__dirname, 'dist'),
      maxAge: 365 * 24 * 60 * 60 * 1000
    });
})
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000);

这样,我们将文件的缓存时间改为十年,当然也可以改为永久,看心情
2.但是这样,如果更新了js,用户就没办法看到新的网页,永远在读缓存
我们改一下webpack,
webpack从0开始搭建react的ts开发环境

webpack.prod.ts


import path from "path";
import webpack from "webpack";
import {BundleAnalyzerPlugin} from "webpack-bundle-analyzer";
import merge from "webpack-merge";
import common from "./webpack.common";
const config: webpack.Configuration = merge(common, {
    mode: "production",
    devtool: "source-map",
    output: {
        filename: "app.[hash].js",
        path: path.resolve(__dirname, "..", "dist"),
    },
    plugins: [
    
        new BundleAnalyzerPlugin(),
    ],
});

export default config;

这样我们在运行

npm run build

的时候,生成的js会带有hash后缀,当我们没有更新代码的时候,hash不变,一旦更新代码,生成的hash也会跟着变化,这样就可以保证未更新时候的极致用户体验

css部分
webpack.common.ts

...
 plugins: [
        new HtmlWebpackPlugin({
            title: "test",
           template: path.resolve(__dirname,'template.html'),
        }),
        new MiniCssExtractPlugin({
            filename: "app.[hash].css",
        }),
    ],
...

3.但是还有一个问题,我们将html也缓存了,没办法获取新的html,就没办法拿到新的文件的hash
改一下server.js

const path = require('path');
const Koa = require('koa');
const send = require('koa-send');
const Router = require('koa-router');

const app = new Koa();

const router = new Router();
router.get(['/', '/**'], async (ctx) => {
  const url = ctx.path;
// 当访问静态文件的时候 十年缓存 用.来区分是url还是静态文件,别的方法也是可以的
  if (url.includes('.')) {
    await send(ctx, ctx.path, {
      root: path.join(__dirname, 'dist'),
      maxAge: 365 * 24 * 60 * 60 * 1000
    });
  }else{
    await send(ctx, './index.html', {
      root: path.join(__dirname, 'dist'),
      maxAge: 0
    });
  }
})

app.use(router.routes()).use(router.allowedMethods())

app.listen(3000);

前后端分离系列-chunk

相关文章

  • 前后端分离系列-缓存篇

    demo https://github.com/757566833/webpack-guide 1.我们需要在静态...

  • 我们为什么要尝试前后端分离

    前端 前后端分离 这不是一篇纯技术文章,而是一篇分享我个人在前后端分离路上收获的点点滴滴的文章,以此来为准备尝试前...

  • 前后端分离系列

    本系列不仅是文章,也会改动一些webpack的配置基于webpack从0开始搭建react的ts开发环境系列 在w...

  • 手机秒杀系统高并发性能处理

    1、页面缓存:将商品列表存入redis中 2、动静分离、页面静态化:前后端分离 3、redis预减库存:将商品id...

  • 2019-08-29

    前后端分离 知识点 前后端分离对表单进行增删该查 views models 创建一个系列化的文件夹 serial...

  • 从壹开始前后端分离 [ Vue2.0+.NET Core2.1]

    缘起 昨天说到了《从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十五 ║ Vue前篇:JS对象...

  • 缓存随谈系列之三:动态缓存

    作为缓存系列的最后一篇,也是我重点想要介绍的。 缓存随谈系列之一:数据库缓存 缓存随谈系列之二:静态缓存(使用静态...

  • 011--【秒杀】缓存优化

    1、写作背景 前面提到,当前的项目不是前后端分离,所有页面的跳转都是后端的页面跳转,这样做浏览器不能进行页面缓存,...

  • 前后端分离系列-chunk

    demo https://github.com/757566833/webpack-guide 在加入缓存之后,我...

  • 前后端分离——token超时刷新策略

    前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信...

网友评论

      本文标题:前后端分离系列-缓存篇

      本文链接:https://www.haomeiwen.com/subject/mtgezctx.html