I am developing a web application. I am using a main javascript file that is used in most of the forms.
<script src="assets/public.js"></script>
This file is being updated regularly and the users can't get the last updated file because the old file is cached with browser. So I am using a version indicator at the end of the file name to enforce browsers to use the latest version of my javascript file :
<script src="assets/public.js?version=54"></script>
I should update the version in every form after each change in the javascript file. this is a manual task which is really time-consuming and some forms might be missed to update.
Is there any effective way to define the last version as a constant and automatically update all forms regarding the last version?
I am developing a web application. I am using a main javascript file that is used in most of the forms.
<script src="assets/public.js"></script>
This file is being updated regularly and the users can't get the last updated file because the old file is cached with browser. So I am using a version indicator at the end of the file name to enforce browsers to use the latest version of my javascript file :
<script src="assets/public.js?version=54"></script>
I should update the version in every form after each change in the javascript file. this is a manual task which is really time-consuming and some forms might be missed to update.
Is there any effective way to define the last version as a constant and automatically update all forms regarding the last version?
Share Improve this question asked Aug 6, 2019 at 7:13 BehnamBehnam 1,0633 gold badges14 silver badges39 bronze badges 1- how are you developing the app? If you are including the mon scripts on every page and not using templates then that itself is bad practice? – Tarun Lalwani Commented Aug 13, 2019 at 6:03
2 Answers
Reset to default 2In your project you need to have a folder [App_Start] there in the [BundleConfig.cs] file you can register bundles:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/public-{version}.js"));
and in the view add this:
<head>
@Scripts.Render("~/bundles/public")
</head>
Every time when change .js version update variable {version} from the file [BundleConfig.cs]
It's possible to use the last modified date of the file as the version indicator. So even no need to define constant.
<script src="asset/public.js?version={{lastModifiedDate('asset/public.js')}}"></script>
Below function returns the last modified date of file:
function lastModifiedDate(url) {
try {
var req = new XMLHttpRequest();
req.open("HEAD", url, false);
req.send(null);
if (req.status == 200) {
return req.getResponseHeader('Last-Modified');
}
else return false;
} catch (er) {
return er.message;
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265517a4619429.html
评论列表(0条)