【问题标题】:how to caching javascript file如何缓存javascript文件
【发布时间】:2021-12-16 02:47:10
【问题描述】:

我有一个大约 9-8 年前使用“pug”技术构建的大型 Web 应用程序,近年来在创新框架(vue.js)中添加了页面 现在,在旧页面(pug)和 vue 页面之间的任何转换中,Webpack(3.12 版)都会加载一个 build.js 文件(3.8MB!),该文件位于 index.html 中,平均需要 9 秒才能加载,并且显着干扰用户体验。

有什么方法可以将 build.js 保存在缓存中吗? 或者,我很想知道是否有人有其他可以增强用户体验的想法

网站的网络服务器是 Nginx

编辑: 这是我的 nginx 文件(相关部分)

server {
    listen      80;
    server_name xxx.com;
    return 301 https://xxx$request_uri;
}

server {
    listen       443 ssl;
    server_name xxx.com;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;
      location ~ ^/dist/(?:ico|css|js|gif|jpe?g|png)$ {
        expires 30d;
    }
    location ~ ^/public/(\d+)/(.+)$ {
         try_files /lbo_builds/build_$1/public/$1/$2 =404;
    }

    location ~ ^/export/data/(.+)$ {
        gzip_static always;
        add_header Content-Encoding gzip;
        add_header Content-disposition attachment;
        try_files /lbo_exported_files/$1.gz =404;
    }

    location /customer_upload_document_proxy {
        proxy_set_header Cookie JSESSIONID=$cookie_jba_session;
        proxy_pass xxx;
    }

    location / {
        expires off;
        add_header Cache-Control "max-age=0, no-store, no-cache, must-revalidate";

        proxy_set_header X-Forwarded-For $remote_addr;

        proxy_pass http://$backend_upstream$request_uri;
    }
}

【问题讨论】:

    标签: javascript vue.js nginx webpack pug


    【解决方案1】:
    1. Split the single huge build.js into small chunks 利用浏览器的并行下载。
    2. 使用 nginx 配置 gzip 压缩和缓存控制
    server {
        listen      80;
        server_name xxx.com;
        return 301 https://xxx$request_uri;
    }
    
    server {
        listen       443 ssl;
        server_name xxx.com;
    
        gzip on;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_min_length 1000;
    
        location ~ ^/public/(\d+)/(.+)$ {
             try_files /lbo_builds/build_$1/public/$1/$2 =404;
        }
    
        location ~ ^/export/data/(.+)$ {
            gzip_static always;
            add_header Content-Encoding gzip;
            add_header Content-disposition attachment;
            try_files /lbo_exported_files/$1.gz =404;
        }
    
        location /customer_upload_document_proxy {
            proxy_set_header Cookie JSESSIONID=$cookie_jba_session;
            proxy_pass xxx;
        }
    
        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            expires 30d;
            
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://$backend_upstream$request_uri;
        }
    
        location / {
            expires off;
            add_header Cache-Control "max-age=0, no-store, no-cache, must-revalidate";
    
            proxy_set_header X-Forwarded-For $remote_addr;
    
            proxy_pass http://$backend_upstream$request_uri;
        }
    
    
    }
    

    【讨论】:

    • 不仅因为并行下载,而且主要是因为可以根据需要延迟导入块。
    • 我将此代码添加到我的 Nginx conf 文件中,但它仍然执行相同的慢速渲染。 (build.sh 文件位于根目录下的 dist 文件夹下)你知道我缺少什么吗? " 位置 ~ ^/dist/(?:ico|css|js|gif|jpe?g|png)$ "
    • 可以关注这个帖子查看缓存是否生效stackoverflow.com/questions/13140318/…
    • 是的,我查过了,不幸的是它不做缓存,
    • 你是不是在编辑完配置文件后重新加载了nginx?
    猜你喜欢
    • 2010-09-23
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多