简体   繁体   English

在视图中强制重载Javascript文件而不发布服务器代码

[英]Force reload Javascript-file on publish WITHOUT server code in view

I have a project that uses a Javascript-File for its logic: 我有一个使用Javascript-File作为其逻辑的项目:

<script src="myMagicCode.js" ></script>

When I publish a new Version, clients have to clear the cache for the new code to work. 当我发布新版本时,客户端必须清除缓存以使新代码正常工作。

I usually can get around this problem by adding a parameter: 我通常可以通过添加参数来解决此问题:

<script src="myMagicCode.js?version=1.23" ></script>

But I am too lazy to do this on each release. 但是我懒得在每个版本上都这样做。 So I want to automate this behavior. 所以我想使这种行为自动化。

The page containing the script-reference is pure html and can not contain any server code. 包含脚本引用的页面是纯html,并且不能包含任何服务器代码。

I have however server code that returns the version. 但是,我有返回版本的服务器代码。

So my idea would be to change the script - code to something like 所以我的想法是将脚本-代码更改为

<script src="api/GetJsUrl"></script>

Where api/GetJsUrl is a GET-Action which contains something like 其中api/GetJsUrl是一个GET-Action,其中包含类似

string url = "myMagicCode.js?Version="+GetCurrentVersion();
HttpContext.Response.AddHeader("Location", url);
return new HttpStatusCodeResult(307);

Will this force the browser to reload the JS-File when a new version is published? 发布新版本时,这会迫使浏览器重新加载JS文件吗? Or will just the api/GetJsUrl -Request be cached and I have the same issue like before? 还是只是将api/GetJsUrl -Request缓存起来,而我遇到了和以前一样的问题?

If there are still issues: Are there better solutions? 如果仍然存在问题:是否有更好的解决方案?

Remark : I do NOT search for a solution that reloads the JavaScript-File every time. 备注 :我不会搜索每次都重新加载JavaScript文件的解决方案。 I need a solution that only reloads the file when I deploy a new release (the version number changes). 我需要一个仅在部署新版本(版本号更改)时重新加载文件的解决方案。

You can use a task runner like Grunt or Gulp: 您可以使用任务运行程序,例如Grunt或Gulp:

http://gruntjs.com/ http://gruntjs.com/

http://gulpjs.com/ http://gulpjs.com/

They can lint, minify, build some things AND do what you want, looking to some properties file that contains the project version and editing your html. 他们可以查找,缩小,构建某些东西并做您想要的事情,寻找包含项目版本的属性文件并编辑html。 They even can join your js file to one and replace it on your index.html: 他们甚至可以将您的js文件加入一个文件并将其替换到index.html上:

 <script src="compiled.js?_=1234567890"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM