Cache busting via params

前端 未结 12 1167
耶瑟儿~
耶瑟儿~ 2020-11-22 03:27

We want to cache bust on production deploys, but not waste a bunch of time off the bat figuring out a system for doing so. My thought was to apply a param to the end of css

12条回答
  •  醉话见心
    2020-11-22 04:21

    Another similar approach is to use htaccess mod_rewrite to ignore part of the path when serving the files. Your never-cached index page references the latest path to the files.

    From a development perspective it's as easy as using params for the version number, but it's as robust as the filename approach.

    Use the ignored part of the path for the version number, and the server just ignores it and serves the uncached file.

    1.2.3/css/styles.css serves the same file as css/styles.css since the first directory is stripped and ignored by the htaccess file

    Including versioned files

    
    
    
      
        
        
        
        
        
        
      
      
        
      
    
    

    Note that this approach means you need to disable caching of your index page - Using tags to turn off caching in all browsers?

    .htaccess file

    RewriteEngine On
    
    # if you're requesting a file that exists, do nothing
    RewriteCond %{REQUEST_FILENAME} !-f 
    # likewise if a directory that exists, do nothing
    RewriteCond %{REQUEST_FILENAME} !-d 
    
    # otherwise, rewrite foo/bar/baz to bar/baz - ignore the first directory
    RewriteRule ^[^/]+/(.+)$ $1 [L] 
    

    You could take the same approach on any server platform that allows url rewriting

    (rewrite condition adapted from mod_rewrite - rewrite directory to query string except /#!/)

    ... and if you need cache busting for your index page / site entry point, you could always use JavaSript to refresh it.

提交回复
热议问题