Google App Engine: How to disable cache on 'static' files, or make cache smart

后端 未结 2 1628
春和景丽
春和景丽 2020-12-31 08:54

I\'m using the app engine locally, and sometimes the JS files are being cached between page refreshes, and it drives me crazy because I don\'t know if there\'s a bug in the

相关标签:
2条回答
  • 2020-12-31 09:27

    Based on the docs, you can specify an app-wide cache expiration duration:

    Unless told otherwise, web browsers retain files they load from a website for a limited period of time. You can define a global default cache period for all static file handlers for an application by including the default_expiration element, a top-level element. You can also configure a cache duration for specific static file handler. (Script handlers can set cache durations by returning the appropriate HTTP headers to the browser.)

    default_expiration

    The length of time a static file served by a static file handler ought to be cached in the user's browser, if the handler does not specify its own expiration. The value is a string of numbers and units, separated by spaces, where units can be d for days, h for hours, m for minutes, and s for seconds. For example, "4d 5h" sets cache expiration to 4 days and 5 hours after the file is first loaded by the browser.

    default_expiration is optional. If omitted, the default behavior is to allow the browser to determine its own cache duration.

    ...and if you want to specify the expiration on a directory-by-directory basis:

    expiration

    The length of time a static file served by this handler ought to be cached in the user's browser. The value is a string of numbers and units, separated by spaces, where units can be d for days, h for hours, m for minutes, and s for seconds. For example, "4d 5h" sets cache expiration to 4 days and 5 hours after the file is first loaded by the browser.

    Try setting them to 0d0h or 1s and see if it disables caching entirely.

    0 讨论(0)
  • 2020-12-31 09:31

    A common practice used by the major sites is to cache documents forever but include a unique identifier based on the release version or date into the url for the .js or .css call. For example:

    <script type="text/javascript" src="static/util.js?version=20100310"></script>
    

    This way you get optimum caching as well as always up to date files. The only trick is to figure out how to include an up to date version number in your url, which you can automate based on your deployment environment.

    0 讨论(0)
提交回复
热议问题