I made an angular website which contains an "add" and a "delete" form to manipulate data on a table on the same page.
When I test it locally or with Chrome Dev Console (disabled cache), when I add a new item or delete one, the table refreshes itself automatically. When I test it on the production server (IIS Server) of my client, it works only with the Chrome Dev Console opened. Otherwise, they have to use CTRL+F5 to refresh the cache and show the changes on the page.
This is the code on components:
addProduct() {
this._productsService.addProduct(this.addFormProductItem)
.subscribe(v => {
this._productsService.getProducts().subscribe(
products => {this.products = products, this.onChangeTable(this.config)}
);
});
this.addmodalWindow.hide();
return;
}
onChangeTable(config: any, page: any = {
page: this.page,
itemsPerPage: this.itemsPerPage
}): any {
if (config.filtering) {
Object.assign(this.config.filtering, config.filtering);
}
if (config.sorting) {
Object.assign(this.config.sorting, config.sorting);
}
this.ng2TableData = this.products;
this.length = this.ng2TableData.length;
let filteredData = this.changeFilter(this.ng2TableData, this.config);
let sortedData = this.changeSort(filteredData, this.config);
this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData;
this.length = sortedData.length;
}
My guess is that it is either related to some server configuration or to the webpack code. However, I am not familiar with the latter and I just left it as it was on the starting package I used.
I created a gist with the webpack since it is a long file
Edit 1: After some extra research, I tried to add the following on a web.config file on the root folder of the app.
<caching enabled="false" enableKernelCache="false">
<profiles>
<add extension=".css" policy="DontCache" kernelCachePolicy="DontCache" />
<add extension=".html" policy="DontCache" kernelCachePolicy="DontCache" />
<add extension=".js" policy="DontCache" kernelCachePolicy="DontCache" />
</profiles>
</caching>
However, I still have the same behavior. Adding an item with Dev Console closed, it doesn't update the table. But if I have the dev console opened and disabled the cache, then it updates it without the need of a refresh.
Edit 2: Working on Incognito Window doesn't fix the problem.
Edit 3: Adding meta tags on index.html doesn't fix the problem, again.
<meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
Edit 4:
getProducts() {
return this._http.get(this.API + '/products/all')
.map((response: Response) => <Product[]>response.json().products);
}
addProduct(product:Product) {
if ( this._loggedInGuard.isLoggedIn() ) {
let token = localStorage.getItem('my.token');
let body = JSON.stringify(product);
let headers = new Headers(
{ 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token});
return this._http
.post(this.API + "/products/store", body, {headers: headers} )
.map(res => res.json())
.catch(this._responseService.catchBadResponse)
.do(() => this._responseService.success('Success. You added a product!'));
}
}
Edit 5
The only solution I can find is to reload the window every time I do an update on data with location.reload(true). But again, this works only on Firefox and not on Chrome. And I will not accept that you have to abandon the only reason of having a Single Page App to make this works.
getProducts()andaddProduct()from your service. Also, what happens when you calladdProductin you component twice?addProduct()function is triggered by a click event. TheaddProductservice is called by the previous function. Which one do you want to test for a double call? And should I do it inside the first subscription, to wait for the first one finish and then call the second?addProduct()function, which is triggerd by the click event, twice. Also I was wondering whether the request bygetProducts()andaddProduct()from the service are displayed in your console.