motainzhang

motainzhang

企业微信侧边栏h5应用白屏时间过长优化方案

2023-08-01
企业微信侧边栏h5应用白屏时间过长优化方案
  1. vue项目在入口文件index.html增加loading动画
 <div id="app">
    <div id="Loading">
      <div class="loader-inner ball-beat">
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>

css

  <style type="text/css">
    #Loading {
      top:50%;
      left:50%;
      position: absolute;
      -webkit-transform: translateY(-50%)  translateX(-50%);
      transform: translateY(-50%)  translateX(-50%);
      z-index:100;
    }
    @-webkit-keyframes ball-beat {
      50% {
        opacity: 0.2;
        -webkit-transform: scale(0.75);
        transform: scale(0.75); }

      100% {
        opacity: 1;
        -webkit-transform: scale(1);
        transform: scale(1); } }

    @keyframes ball-beat {
      50% {
        opacity: 0.2;
        -webkit-transform: scale(0.75);
        transform: scale(0.75); }

      100% {
        opacity: 1;
        -webkit-transform: scale(1);
        transform: scale(1); } }

    .ball-beat > div {
      background-color: #4A4A4A;
      width: 15px;
      height: 15px;
      border-radius: 100% !important;
      margin: 2px;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
      display: inline-block;
      -webkit-animation: ball-beat 0.7s 0s infinite linear;
      animation: ball-beat 0.7s 0s infinite linear; }
    .ball-beat > div:nth-child(2n-1) {
      -webkit-animation-delay: 0.35s !important;
      animation-delay: 0.35s !important; }
  </style>
  1. 开启GZIP压缩,webpack使用compression-webpack-plugin插件,注意版本,新版本会出现报错问题
 configureWebpack: (config) => {
    if (process.env.NODE_ENV === 'production') {
      config.plugins.push(
        new CompressionPlugin({
          // gzip压缩配置
          test: /\.js$|\.html$|\.css/, // 匹配文件名
          threshold: 10240, // 对超过10kb的数据进行压缩
          deleteOriginalAssets: false, // 是否删除原文件
        })
      )
    }
  1. 抽离vue,vue-router,vant,axios使用cdn外链加载
  2. 使用微信api onNetworkStatusChange监听网络状态,如果是弱网环境,或者是无网环境给出用户合理提示,注意调用之前,需要wx.config,wx.agentconfig调用成功才行,不然会报错