网络与工程化

代码分包(Code Splitting)

1颗粒化分包

javascript 复制代码
import { defineConfig } from 'vite';
import react from '@vitejs/react-refresh'; // 或 vue

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        // 核心:精细化控制分包逻辑
        manualChunks(id) {
          // 1. 把极其稳定的基础框架/库单独打包,它们几乎一两年都不会变
          if (id.includes('node_modules/react') || id.includes('node_modules/react-dom')) {
            return 'framework';
          }
          if (id.includes('node_modules/vue') || id.includes('node_modules/pinia')) {
            return 'framework';
          }

          // 2. 把体积大且相对独立的三方库单独抽离
          if (id.includes('node_modules/lodash-es')) {
            return 'utils-lodash';
          }
          if (id.includes('node_modules/echarts') || id.includes('node_modules/zrender')) {
            return 'lib-charts';
          }

          // 3. 剩下的公共第三方依赖,按照包名自动拆分(颗粒化)
          // 这样只要某个依赖没变,它的 chunk 缓存就永远有效
          if (id.includes('node_modules')) {
            const parts = id.toString().split('node_modules/')[1].split('/');
            const packageName = parts[0].startsWith('@') ? `${parts[0]}/${parts[1]}` : parts[0];
            return `vendor-${packageName.replace('@', '')}`;
          }
        },
        // 优化输出的文件名结构,方便 CDN 强缓存
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
      },
    },
    // 调整 chunk 大小警告阈值
    chunkSizeWarningLimit: 600,
  },
});

浏览器只会精准下载这两个改变的小文件

2浏览器知道该怎么去接这些包

a 模块预加载
  • 网络加速原理: 浏览器在刚解析到 HTML 头部时,就会利用 HTTP/2+ 的并发能力去并行下载这些 JS 模块,同时进行预解析和编译
ini 复制代码
默认生成的index.html中 :<link rel="modulepreload" href="xxx.js">
b 开启 HTTP/2 强缓存与资源压缩
bash 复制代码
server {
    # 1. 开启 HTTP/2 或 HTTP/3 (取决于你的 Nginx 版本和证书)
    listen 443 ssl http2; 
    
    # 2. 针对带 hash 的打包产物(js/css/images),配置强缓存(一年)
    # 因为只要内容变了,hash 必变,所以可以死死缓存住,绝不重复请求
    location /assets/ {
        alias /usr/share/nginx/html/assets/;
        expires 365d;
        add_header Cache-Control "public, immutable"; # immutable 告诉浏览器别拿 F5 烦我,绝对不用去后端校验
    }

    # 3. 针对 index.html,必须配置协商缓存(每次都得问后端文件变了没)
    location / {
        root /usr/share/nginx/html;
        index index.html;
        try_files $uri $uri/ /index.html;
        expires -1;
        add_header Cache-Control "no-cache, no-store, must-revalidate";
    }

    # 4. 开启 Brotli 压缩(比 Gzip 压缩率高 20%+,降低网络传输体积)
    brotli on;
    brotli_types text/plain text/css application/javascript application/json image/svg+xml;
}
相关推荐
wuhen_n5 小时前
RAG 第一步:多格式文档加载与文本预处理实战
前端·langchain·ai编程
程序员黑豆5 小时前
全新系列开启:AI 全栈开发
前端·后端·全栈
小小小小宇5 小时前
Partial Clone
前端
小小小小宇5 小时前
git sparse-checkout(稀疏检出)
前端
ZC跨境爬虫6 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript
夜焱辰6 小时前
WebMCP 的正确打开方式:只注册 2 个工具,代理 N 个——CreatorWeave 的 On-Demand 实践
前端
用户7459571748406 小时前
Fabric:Python SSH 远程执行利器
前端
用户288391927476 小时前
Elasticsearch DSL:用 Python 对象写查询,不用再手写 JSON
前端
一拳小和尚LXY7 小时前
我开发了一款免费 Chrome 插件 TabScribe:一键复制所有标签页为 Markdown/JSON,完全离线零追踪
前端·chrome·json
dust_and_stars7 小时前
ubuntu24上安装chrome和edge浏览器
前端·chrome·edge