Unable to update javascript file in .net core

Viewed 804

I trying to study .net core with a test project.

In this project there is a user javascript javascript file called "booklist.js" that call some functions like edit and delete button.

For some strange think any modifications of this file don't do any effect.

After delete this file, the project continue to work.

But if i disable these lines:

@section Scripts{ 
<script src="~/js/bookList.js"></script>
}

on the view page the project not read the data.

I'am new with MVC and I don't have any idea about.

Can you help me please ?

Thanks !

1 Answers

One of the reasons of this issue could be browser caching the old js in file, as a solution is to append a parameter to your js source like a timestamp

Then your js tag becomes:

<script type="text/javascript" language="javascript">  
    var versionUpdate = (new Date()).getTime();  
    var script = document.createElement("script");  
    script.type = "text/javascript";  
    script.src = "~/js/bookList.js?v=" + versionUpdate;  
    document.body.appendChild(script);  
</script>

Reference: https://www.c-sharpcorner.com/article/how-to-force-the-browser-to-reload-cached-js-css-files-to-reflect-latest-chan/

or

You can use the new feature in .Net Core

asp-append-version="true" 

Then your js tag becomes:

<script src="~/js/bookList.js" asp-append-version="true"></script>    

Reference: https://www.c-sharpcorner.com/blogs/aspappendversion-feature-in-asp-net-core

Related