Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api suggestion: API.App.restart #149

Open
peters opened this issue Nov 5, 2012 · 69 comments
Open

Api suggestion: API.App.restart #149

peters opened this issue Nov 5, 2012 · 69 comments
Assignees

Comments

@peters
Copy link

peters commented Nov 5, 2012

Support for restarting the application. It should restart with the same invocation flags it was started with in the first place. This would be great when it comes to auto-updating.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@Ivshti
Copy link
Contributor

Ivshti commented Nov 5, 2012

Can't you just refresh the page?

document.location.reload(true)

@peters
Copy link
Author

peters commented Nov 5, 2012

Sure, you could do that. But what if the application holds an exclusive lock on a file you are going to update? What if you want to replace the chromium binary blob itself?

@ibdknox
Copy link
Contributor

ibdknox commented Nov 6, 2012

+1

@zcbenz
Copy link
Contributor

zcbenz commented Nov 7, 2012

I'll implement App.restart() after #64 is done.

@twrodriguez
Copy link

+1

@tommoor
Copy link

tommoor commented Jun 10, 2013

Any thoughts / priority on this restart API? Would be great.

@javichi
Copy link

javichi commented Jul 10, 2013

+1 for a clean restart function

@adampetrie
Copy link

Any updates on this feature?

@schme16
Copy link

schme16 commented Nov 6, 2013

+1 for this!

@rhiokim
Copy link

rhiokim commented Nov 6, 2013

+1 It would be nice to auto update applications.

@tommoor
Copy link

tommoor commented Nov 8, 2013

This feature would be helpful when doing updates, as you can provide the user with a button to restart the application instead of asking them to close and reopen.

@semmel
Copy link

semmel commented Nov 8, 2013

It is a real nuisance to do this with shell scripts. Since after you've spawned the script you'll have to wait until the app process terminates.
And it is hard to do right path parameter sanitising (e.g. spaces). Not to mention Unicode text which I believe is not supported on Windows, or is it?
The Windows shell script:

@echo off
rem usage: relaunch_app.bat <seconds to wait> <executable to launch> <arguments>
rem i.e. $>relaunch_app.bat 5 "C:\Program Files\Jujusoft\JujuEdit\JujuEdit.exe" "C:\Windows\system.ini"

echo Restarting in %1 seconds...
title Waiting

rem Windows 7
@timeout /t %1 /nobreak >nul 2>&1

rem Windows XP
if errorlevel 1 ping 192.0.2.2 -n 1 -w %1000 > nul

rem # launch the executable via start command
start /B "" %2 %3 %4

And on Mac:

echo "Please wait for ${1} seconds.."
sleep $1
open -n -a "${2}" --args $3

In your node-webkit app:

child_process.spawn(path.join(process.cwd(), "relaunch_app.bat")
    , [waitSecs.toString()
        , (process.platform == "darwin") ? process.execPath.match(/^([^\0]+?\.app)\//)[1] 
                                        : process.execPath
    ]    
    , {detached: true}
);

@yolio2003
Copy link

现在只能在工具条上点击, 如何在程序中调用 restart 呢

用 document.location.reload(true) 代替是不是不能刷新node的模块?

@f0rmiga
Copy link

f0rmiga commented Feb 4, 2014

What about:
require('nw.gui').Window.get().reload(3);
It will do clean-reload, like the rightmost reload button on toolbar, right?

@jamesmortensen
Copy link

@f0rmiga No, unfortunately that doesn't work. If your app consists of more than one window, only the window from which that command is run is reloaded. Ideally, App.restart or its equivalent would restart the entire app by shutting down all windows associated with the app.

@paulmand3l
Copy link

+1

@michapixel
Copy link

@james: you could send a notification to that window so it can reload
itself too.

micha

2014-02-27 1:11 GMT+01:00 Paul Mandel notifications@github.com:

+1


Reply to this email directly or view it on GitHubhttps://github.com//issues/149#issuecomment-36194978
.

@laggingreflex
Copy link

+1

3 similar comments
@scottgmeadows
Copy link

+1

@nbush
Copy link

nbush commented Mar 17, 2014

+1

@zefhemel
Copy link

zefhemel commented Apr 3, 2014

+1

@dynamite-ready
Copy link

@f0rmiga - You wrote:

require('nw.gui').Window.get().reload(3);

I had no idea the method accepted arguments. It's not in the wiki. It helped me out.
What does it control?

One or two of the 'Window.reload' family methods aren't documented either. Like this one:

Window.prototype.reloadDev()

Which, funnily enough, resolves to a nested call to reload(3)...

@f0rmiga
Copy link

f0rmiga commented May 5, 2014

@dynamite-ready I'm sorry, but I don't remember where I found it and what exactly it controls, but it worked to me pretty well. I made a app that uses just one window and it connects to a server, and all other solutions that I tried did not reloaded the connection to the server for example, but that method did it... maybe in other scenarios, like @jamesmortensen said, it won't work. More specifically when you have more than 1 window.

@gpetrov
Copy link

gpetrov commented Jul 3, 2014

Actually this works perfect for me, a simple respawn of itself:

//Restart node-webkit app
var child_process = require("child_process");

//Start new app
var child = child_process.spawn(process.execPath, [], {detached: true});

//Don't wait for it
child.unref();

//Quit current
GUI.window.hide(); // hide window to prevent black display
GUI.app.quit();  // quit node-webkit app

@tphalp
Copy link

tphalp commented Jul 8, 2014

@gpetrov I can't get this to work on OS X. I did alter the spawn call to use the .app path that is common on OS X, but that didn't work. It does close the app, and does not throw an error, but the spawn doesn't launch the app again.

If I use it as-is (not altering the exePath to point to the actual .app folder), it launches node-webkit again, but does not load any of my specific web assets, just the plain node-webkit window that you see when it can't find your web assets.

Any ideas?

In the meantime, I think I'm going to try @semmel 's method.

@azendal
Copy link

azendal commented Jul 25, 2014

i tested the spawn method the problem (0.10) seems that the .unref on the child is not working i tried it with say and delay. app does not quit until the delay on the spawned process and say command happens

this is my bash test script content

sleep 5
say 'hi there'

but if you run the same thing on plain node works.

@mitselek
Copy link

add my +1 to App.restart, too

@mitselek
Copy link

in my index.html

<script src="./code/app.js"></script>

in my app.js

require('nw.gui').App.restart = function() {
    var child
    var child_process = require("./code/app.js")
    var gui = require('nw.gui')
    var win = gui.Window.get()
    if (process.platform == "darwin")  {
        child = child_process.spawn("open", ["-n", "-a", process.execPath.match(/^([^\0]+?\.app)\//)[1]], {detached:true})
    } else {
        child = child_process.spawn(process.execPath, [], {detached: true})
    }
    child.unref()
    win.hide()
    gui.App.quit()
}
setTimeout(function () {
    require('nw.gui').App.restart()
}, 2*1000)

Application launches fine and crashes after 2 seconds (on App.restart) with output

[11948:1028/112029:ERROR:nw_shell.cc(336)] Error: Cannot find module 'nw.gui'
    at Function.Module._resolveFilename (module.js:327:15)
    at Function.Module._load (module.js:264:25)
    at Module.require (module.js:356:17)
    at require (module.js:375:17)
    at Object.<anonymous> (/Users/michelek/Documents/github/Screenwerk/code/app.js:6:23)
    at Module._compile (module.js:451:26)
    at Object.Module._extensions..js (module.js:469:10)
    at Module.load (module.js:346:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:356:17)
[11948:1028/112029:INFO:CONSOLE(327)] "Uncaught Error: Cannot find module 'nw.gui'", source: module.js (327)

There is a context problem as I'm not reloading my index.html
I can't child_process = require("./index.html")
Any ideas?

@orielz
Copy link

orielz commented Dec 24, 2015

+1

@Ostro
Copy link

Ostro commented Jan 11, 2016

@mitselek i have the same error on fedora 22

@xenohunter
Copy link

A long-awaited feature, indeed. Is there some hope for it to be implemented?

@ghost
Copy link

ghost commented Apr 7, 2016

+1 to this feature from me too!
https://i.gyazo.com/23171745df3aa4ac77ced7901c44b5f8.gif 👍

@Aagsby
Copy link

Aagsby commented Apr 20, 2016

+1

@iteufel
Copy link
Contributor

iteufel commented Apr 20, 2016

chrome.runtime.reload();

@xenohunter
Copy link

@iteufel, it ends up with "Unchecked runtime.lastError while running runtime.restart: Restart is only supported on ChromeOS".

@iteufel
Copy link
Contributor

iteufel commented Apr 20, 2016

@xenohunter Use chrome.runtime.reload not chrome.runtime.restart

@Aagsby
Copy link

Aagsby commented Apr 20, 2016

@iteufel Thank you very much! It works like a charm.

@xenohunter
Copy link

@iteufel, oops. Though now it takes no effect at all. No warning, no error, just nothing.

@jkramp
Copy link

jkramp commented Jun 22, 2016

+1

1 similar comment
@blaremc
Copy link

blaremc commented Jan 18, 2017

+1

@blaremc
Copy link

blaremc commented Jan 18, 2017

if anyone is interested, i've solved it like this

restart() {
    const {execPath, platform} = nw.process;
    const {resolve} = nw.require('path');
    let exePath = '';
    let restartFile = '';
    let command = '';
    let content = '';
    const {tmpdir} = nw.require('os');
    const TEMP_DIR = resolve(tmpdir(), 'YOUR APP NAME');
    if (platform === 'darwin') {
      exePath = resolve(execPath, '../../../../../../../');
      restartFile = resolve(TEMP_DIR, 'restart.sh');
      content = "#!/bin/sh\nAPP=$1\nsleep 1\nopen $APP";
      command = 'sh ';
    } else {
      exePath = execPath;
      restartFile = resolve(TEMP_DIR, 'restart.bat');
      content = "timeout 2\ncall $1";
      command = '';
    }
    const fs = nw.require('fs');
    fs.writeFile(restartFile, content, function (err) {
      child_process.exec(command + restartFile + ' ' + exePath);
      nw.App.quit();
    });
  }

@GendelfLugansk
Copy link

@blaremc did you test it on windows? Doesn't work for me, seems like child_process.exec does nothing on windows.

@GendelfLugansk
Copy link

For me, solution provided by @gpetrov worked on windows (tested in win7 & win10) and solution from @blaremc (slightly modified) worked on linux

@blaremc
Copy link

blaremc commented Apr 17, 2017

@GendelfLugansk this code is working

restart() {
    let exePath = '';
    const {resolve} = nw.require('path');
    const {execPath, platform} = nw.process;
    if (platform === 'darwin') {
      exePath = resolve(execPath, '../../../../../../../');
    } else {
      exePath = execPath;
    }
    this.createRestartFile(exePath, () => {
      setTimeout(() => {
        nw.App.quit();
      }, 300);
    });
  }

  createRestartFile(exePath, callback) {

    const {platform} = nw.process;
    const {resolve} = nw.require('path');
    let restartFile = '';
    let command = '';
    let content = '';
    const TEMP_DIR = this.getTempDir();
    if (platform === 'darwin') {
      restartFile = resolve(TEMP_DIR, 'restart.sh');
      content = "#!/bin/sh\nAPP=$1\nsleep 3\nopen $APP";
      command = 'sh ';
    } else {
      restartFile = resolve(TEMP_DIR, 'restart.bat');
      content = "timeout 3\nstart %1\nexit\n";
      command = 'start /b ';
    }
    const fs = nw.require('fs');
    fs.writeFile(restartFile, content, function (err) {
      child_process.exec(command + restartFile + ' ' + exePath);
      callback();
    });
  }

@surinder83singh
Copy link

try chrome.runtime.reload();

@mscreenie
Copy link
Contributor

As mentioned by surinder: chrome.runtime.reload(); should work I believe it is the code underlying Reload app context menu in sdk versions.

@GendelfLugansk
Copy link

GendelfLugansk commented Aug 30, 2017

@mscreenie As was mentioned before by few users, chrome.runtime.reload(); does not fully restart webkit, which is required for tasks like application update or ffmpeg update. Old versions of ddl/so will be used until app fully restarted (meaning - closed and started again)

@f3l1x
Copy link

f3l1x commented Sep 14, 2017

Works

chrome.runtime.reload() works for me.

@IssueHuntBot
Copy link

@loadbalance-sudachi-kun funded this issue with $256. Visit this issue on Issuehunt

@jssuttles
Copy link

chrome.runtime.reload() does not work at this time. seems like localStorage doesn't get loaded the same way afterwards

@jonlepage
Copy link
Contributor

jonlepage commented Jun 30, 2019

any new?, document.location.reload(true); seem buggy with vscode debugger
I lost break point

             if(event.keyCode === 116){ // F5 refresh
                 document.location.reload(true);
             };
        "version": "0.1.0",
        "configurations": [
            {
                "type": "nwjs",
                "request": "launch",
                "name": "NWJS DEBUG",
                "runtimeExecutable": "${workspaceRoot}\\_NW\\sdk\\nw.exe",
                "runtimeArgs": [
                    "--remote-debugging-port=9222",
                    "--enable-gpu-rasterization",
                    "--enable-gpu-memory-buffer-video-frames",
                    "--enable-native-gpu-memory-buffers",
                ],
                "webRoot": "${workspaceRoot}",
                "port": 9222,
                "sourceMaps": false,
                "reloadAfterAttached": true,
            },
            {
                "type": "nwjs",
                "request": "attach",
                "name": "Attach to NWjs",
                "port": 9222,
                "webRoot": "${workspaceFolder}",
                "reloadAfterAttached": true,
            }

        ]

@konijn
Copy link

konijn commented Oct 24, 2019

+1 It seems there are enough uses cases to just expose either the old win.reloadDev() or API.App.restart. Also, the website still mentions win.reloadDev()..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests