location.reload(true) is deprecated

JavascriptAngularReloadTslint

Javascript Problem Overview


I know that it is not ideal to reload an Angular Single Page Application. However there is a place that I need to reload the full application.

TSLint says that reload is deprecated.

Is there any alternative for this?

Javascript Solutions


Solution 1 - Javascript

You can use location.reload() without the forceReload flag. This is just deprecated because it is not in the spec: https://www.w3.org/TR/html50/browsers.html#dom-location-reload

Solution 2 - Javascript

If you look at interface definition for Location you will see the following:

/**
  * Reloads the current page.
  */
reload(): void;
/** @deprecated */
reload(forcedReload: boolean): void;

Using location.reload() is the valid syntax. It is only the reload with forcedReload which is deprecated.

In your case changing location.reload(true) to location.reload() to resolve your issue.

Solution 3 - Javascript

Since the forceReload parameter is deprecated, as 3 people already mentioned, it is also ignored by some browsers already.

Using only location.reload(); is not a solution if you want to perform a force-reload (as done with e.g. Ctrl + F5) in order to reload all resources from the server and not from the browser cache.

The solution to this issue is, to execute a POST request to the current location as this always makes the browser to reload everything.

location.reload() is only executing a GET request.

So I came to the workaround to place an empty form on the Angular app with method="POST" and action="https://example.org/" and then submitting that form from code-behind when wanted.

Template:

<form [action]="myAppURL" method="POST" #refreshForm></form>

Code-behind / component:

import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  // bla bla bla Mr. Freeman
})
export class FooComponent {
  @ViewChild('refreshForm', { static: false }) refreshForm;

  forceReload() {
    this.refreshForm.nativeElement.submit();
  }
}

I guess this can be also done in a more non-hacky way. I am investigating... (HttpClient, maybe?)

Solution 4 - Javascript

Since window.location.reload(true) has been deprecated. You can use:

window.location.href = window.location.href,

to force-reload and clear the cache.

Solution 5 - Javascript

According to Martin Schneider answer, you can create a function, and then just call it

forceReload() {
    if(environment.production) {
        const form = document.createElement('form');
        form.method = "POST";
        form.action = location.href;
        document.body.appendChild(form);
        form.submit();
    } else {
        window.location.reload();
    }
}

Good luck ! 

Solution 6 - Javascript

Changing location.reload(true) to location.reload() works. forcedReload is the one which is deprecated.

Solution 7 - Javascript

I created a small package in order to have a solution to clean the cache and reload the page if necessary: https://www.npmjs.com/package/clear-cache

clearCache() or clearCache(false) when you only want to clear cache but don't want to reload the page.

Solution 8 - Javascript

I think the best and most effective way to refresh (not just reload) the page is using a form with method="post", an empty action, and a submit button, since "post" requests are never cached. Simple and effective, and it even works with no JavaScript at all!

<form action="" method="post">
    <button type="Submit">Refresh</button>
</form>

If you need to do it programatically, you can just hide the button with CSS and trigger the click action in the button with JavaScript.

Thanks to @Martin Schneider for his idea.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDilshan LiyanageView Question on Stackoverflow
Solution 1 - JavascriptZachary SchroederView Answer on Stackoverflow
Solution 2 - JavascriptMatt SeymourView Answer on Stackoverflow
Solution 3 - JavascriptMartin SchneiderView Answer on Stackoverflow
Solution 4 - JavascriptabadilloView Answer on Stackoverflow
Solution 5 - JavascriptJunior Klaus Mombo-NzahouView Answer on Stackoverflow
Solution 6 - JavascriptCharith JayasankaView Answer on Stackoverflow
Solution 7 - JavascriptDimitri DumontView Answer on Stackoverflow
Solution 8 - JavascriptOMAView Answer on Stackoverflow