Eric J.
Eric J.

Reputation: 150228

Control Browser Caching of Generated JavaScript

Background

An MVC 4 application needs to include JavaScript that is generated from the database. The generated file is relatively large, and changes only occasionally (changes every 2-10 days).

The accepted answer of

Generate javascript file on the fly in asp.net mvc

provides a good framework for generating the JavaScript. However, the URL of the JavaScript resource is static. As I understand it, the browser will cache that resource. That caching can be controlled by the OutputCacheAttribute.

Question

How can I cause the browser cache to expire whenever the generated JavaScript changes, similar to the way that bundles work? A SqlDependency for the OutputCacheAttribute is not an option.

Upvotes: 1

Views: 246

Answers (1)

Jack
Jack

Reputation: 1352

I have a similar situation, and what I do is append a random Guid to the end of the query string in the script tag every time I regenerate the JavaScript file.

For Example:

<script type="text/javascript" src="/scripts/generated.js?id=7c97aa32-29d9-dd11-a926-001d096d84f2"></script>

You could use a random string or even a time-stamp, as long as the query parameter is changed,the new script file will be downloaded.

Upvotes: 1

Related Questions