location.reload() and location.replace(url) not work in Electron with Angular 6

Viewed 4451

I use Electron to rebuild a ready web app. the electron main.js is simple:

    // Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
  // Create the browser window.
   mainWindow = new BrowserWindow({
        width: 1600, 
        height: 900
  })

  // and load the index.html of the app.
  mainWindow.loadFile('dist/mysite/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

when i use 'electron .' run the app, it works good except the line with location. The app code below is the login submit form action. After login there is one line 'location.reload()' not work:

this.http.post(this.url + ':8000/' + 'auth/jwt/create/', this.validateForm.value)
  .subscribe((res: any) => {
    localStorage.setItem('token', res.token);

// This LINE NOT WORK 
location.reload();
// //////

    // this.router.navigate([this.router.url]);
    // this.router.navigate(['./']);
    console.log(location);
  }, (err: any) => {
    if (err.status === 400) {
      this.msg = err.error.non_field_errors[0];
      console.log(err);
    } else {
      this.msg = err;
      console.log(err);
    }
    this.isVisible = true;
  });

I try lots of ways to make the web go to the target url but failed. location.reload() location.replace router.navigate all not work. When I use 'ng serve' and open site with chrome, 'location.reload()' has no problem. So what should I do ? Thank you.

EDIT: after location.reload() executed , the page go to blank. and when console.log(location), the href is chrome-error://chromewebdata/

console.log(location)  
Location {replace: ƒ, assign: ƒ, href: "chrome-error://chromewebdata/", ancestorOrigins: DOMStringList, origin: "null", …}
3 Answers

It stopped working for me when I introduced a Listener to my browserView.webContents.on('will-navigate', ...); and I was stopping some events, page reloading was blocked by that one.

In Angular, you should use window.location.reload(), because window is a share object, and can access by anywhere such as localStorage.

window.location.reload()

You can also console.log(location) and console.log(window.location) to know the difference.

So this has nothing to do with how you're calling window.location.reload(). By default, angular and other frameworks replace index.html with #/my-app-home-page. So the page loads correctly the first time, but if you try to reload the page, either programmatically or by using keyboard/menu, the page fails to load and you see the blank screen.

The fix is to ensure that /index.html stays in the url, which you can easily accomplish by changing the base tag:

<base href="./index.html" />
Related