app.relaunch([options]) is not working in electron - javascript

I want to change userData path with path defined by user. So, I'm fetching the path from UI, storing it into a file. So that next time app launches, it changes the path.
I wanted to restart the app as soon as user has selected the path. I tried app.relaunch() function. But it didn't work, neither it returned error.
I used exact same example mentioned in documentation. http://electron.atom.io/docs/api/app/#apprelaunchoptions

Calling app.relaunch() will not actually quit the app, you need to follow it by a call to app.quit() or app.exit().

app.relaunch();
app.quit();
This code must work, but Please Note that while debugging (i.e. in Visual studio code) after app.quit() debugger disconnects and kills whole application, therefore app will not restart. You might want to test it on application that is already installed or run through npm.

Related

window.dataLayer.push is working in local machine but not in the production environment

This is the logout logic, here the key logoutoverlay, is set in the local storage.
<div className="logout">
<NavLink href={propsData.userDataTopNavLinkList.logout} onClick={() => {
localStorage.removeItem("manage");
localStorage.removeItem("token");
localStorage.removeItem("auth");
localStorage.removeItem("AccountInfo");
localStorage.removeItem("successfullySet");
localStorage.removeItem("userImpression");
localStorage.removeItem("cartCnt");
localStorage.removeItem("callDuration");
localStorage.removeItem("customGeoIPInfo");
localStorage.removeItem("geoipInfo");
localStorage.setItem("logoutoverlay","form overlay")
}}> {dictionary.t("Logout", "LOGOUT")}</NavLink>
</div>
Here inside Main.js the local localStorage is checked for the key logoutoverlay
if(localStorage.getItem("logoutoverlay"))
{
logoutDataLayer(localStorage.getItem("logoutoverlay"));
localStorage.removeItem("logoutoverlay");
}
and inside dataLayerAnalytics.js the item is being pushed to the data layer
export const logoutDataLayer = (logoutplace) => {
let logoutDatalayer = {
"event": "loyaltyLogout",
"logoutContext": logoutplace
}
window.dataLayer.push(logoutDatalayer);
}
All of this is working fine on my local machine. But after I deployed it to the production environment the localStorage.removeItem("logoutPlace") inside the Main.js is not getting executed and the code can't reach localStorage.removeItem("logoutPlace") as well. That's why the item "logoutoverlay" doesn't get removed from the local storage.
And since I am unable to replicate this issue on my local system, I can't even debug the code.
Introduction
Your problem statement is that
localStorage.removeItem("logoutoverlay");
is not executed on prod, but it is successfully executed on your dev env. Since we do not have your source-code we cannot tell you for sure what the problem is, but we can tell you how you can find out what the problem is. This answer focuses on the method of finding out what the issue is.
How to debug the client-side of prod
If your source-code is available on prod, then you can debug your client-side code, written in Javascript via the Sources tab of your browser's dev tools. You can reach it via:
right-click anywhere on the page -> click on inspect/inspect element (the text varies accross browsers) -> dev tools appear, usually with the Elements tab being shown by default -> click on Sources -> find Main.js and click on that
Now, in your Main.js you can click on the left-most edge of the source-code's lines to put in breakpoints and from there on you can debug.
If the client-side code is minified on prod, then you have several options to proceed. You could download the minified script into your dev env and temporarily use that to debug. Or, you can implement a testing mode on prod, which would allow you to load and use the original, not minified js code, which would allow you to debug your prod's client-side code in the browser.
Make sure you compare apples with apples
It is certainly possible that your server has a different version of your client-side code, which behaves differently. Make sure that you have equivalent source-code on the server as in your dev env, since, if these are different, then it is unsurprising that they behave differently. To do so, you need to:
clear your browser cache or open the page in incognito mode in order to avoid being misled by old file versions being loaded into your browser either on server or client-side
clear your server cache if you have one, since that prevents new code from being used for a while
download Main.js from the server and study it, possibly comparing it to the Main.js you have on the dev environment
if you don't use a version controller, then it is high time you start using one. If you have a version controller, then, using it look at the commit hash of the latest commit and the branch being in use at both, maybe pulling/pushing in order to ensure that the versions are the same
Check user state on client-side and server-side
This is the problematic section of your code:
if(localStorage.getItem("logoutoverlay"))
{
logoutDataLayer(localStorage.getItem("logoutoverlay"));
localStorage.removeItem("logoutoverlay");
}
Maybe on your server's localStorage you do not have logoutoverlay. Maybe you have logged in to your server before that value was tracked and never logged in since. If it's just a matter of user state, then basically the problem is just coping with differences of versions.
Be careful about user states when you deploy session logic changes
You implement some logout logic which assumes some values inside localStorage. What if that value is not inside localStorage, because the user logged in before the change that creates it was deployed. The last thing you want is to have some broken user sessions, so make sure that your work covers the case when users logged in before your deploy and their session still exists. If during deployment all sessions are destroyed, then this is not an actual concern.

NextJS isn't updating when pages/containers/components are changed, HMR always returns success

I'm having an issue with NextJS/webpack.
Whenever I make changes, it won't update anything until a full restart of everything. This takes a really long time to compile, most recently 6.4 seconds for the client and 2.3 for the server. Often it has taken 20-30 seconds in the past.
process.env.NODE_ENV is undefined, webpack-hmr and on-demand-entries both always return empty/success in the browser. My guess would be that Next/Webpack aren't able to watch the files that are being changed.
My next version is 6.0.3 and I'm working in Ubuntu 16.04.4 lts in WSL on Windows 10.0.17134.
The problem persists with a custom server, I'm calling app.getRequestHandler once, assigning it to handler, then routing get paths (/{p*}) to the handler with Hapi v17.5.2.
If you happen to find this "question and solution" and it is still not working...
Try adding a .env with CHOKIDAR_USEPOLLING=true on it, this fixed it for me, because I am not storing the files inside WSL but in a different disk on Windows...
I added
config.node = {
fs: 'empty'
}
and now it updates on file changes.

Meteor: disable hot code push

I'm developing a phonegap app and hot code push is causing a problem for some of my users (after push, app hangs and users have to re-install the new app).
I'd like to disable hot code pushes and found the following snippet but am getting the error, "TypeError: Cannot call method 'onMigrate' of undefined"
Meteor._reload.onMigrate(function() {
return [false];
});
Looks like the _reload object is only available on the client. I moved the snippet of code to a client directory and that solved it.
Leaving this question / answer here for anyone else who might come across it.
You can set the environment variable AUTOUPDATE_VERSION to something static. You need to set this environment variable when you build your app and when you start the server. (Source)

How to identify first call in Cordova

I have a cordova-based app that runs on Android, iOS and Windows Phone. The starting point in my app is the index.html page, which will not only be loaded on app start, but you can redirect to it from inside the app.
I want to execute some code if and only if the app was just started (so when the index.html was displayed the first time) and not if it was redirected to it. I tried to use cookies that expire when the session ends, but cordova does not work with cookies.
Also I do not want to use session storage because some older Androids (as well as Internet Explorer) cannot handle this.
My used cordova version is 4.0.0
EDIT:
I forgot to mention that it is not a single-page app, but I use multiple pages that can be accessed, so the deviceready-event does not work, because it would be fired every time I access the index.html-page
Try smth like this:
function documentReady() {
   document.addEventListener("deviceready", handleDeviceReady, false);
}
 
function handleDeviceReady(event) {
   //cordova api is ready for use
if (!localStorage.getItem('alreadyStarted')) {
// App just started!
localStorage.setItem('alreadyStarted', true);
}
}
UPD. Also you need to set the flag at first startup (i.e. alreadyStarted = true in sessions or LocalStorage) and check it in handleDeviceReady() later.
Write to the database at first run. Everytime the app starts, check the database to see if that string is present. If it it, ignore. Else, create and write.

How can I edit on my server files without restarting nodejs when i want to see the changes?

I'm trying to setup my own nodejs server, but I'm having a problem. I can't figure out how to see changes to my application without restarting it. Is there a way to edit the application and see changes live with node.js?
Nodules is a module loader for Node that handles auto-reloading of modules without restarting the server (since that is what you were asking about):
http://github.com/kriszyp/nodules
Nodules does intelligent dependency tracking so the appropriate module factories are re-executed to preserve correct references when modules are reloaded without requiring a full restart.
Check out Node-Supervisor. You can give it a collection of files to watch for changes, and it restarts your server if any of them change. It also restarts it if it crashes for some other reason.
"Hot-swapping" code is not enabled in NodeJS because it is so easy to accidentally end up with memory leaks or multiple copies of objects that aren't being garbage collected. Node is about making your programs accidentally fast, not accidentally leaky.
EDIT, 7 years after the fact: Disclaimer, I wrote node-supervisor, but had handed the project off to another maintainer before writing this answer.
if you would like to reload a module without restarting the node process, you can do this by the help of the watchFile function in fs module and cache clearing feature of require:
Lets say you loaded a module with a simple require:
var my_module = require('./my_module');
In order to watch that file and reload when updated add the following to a convenient place in your code.
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
my_module = require('./my_module');
});
If your module is required in multiple files this operation will not affect other assignments, so keeping module in a global variable and using it where it is needed from global rather than requiring several times is an option. So the code above will be like this:
global.my_module = require ('./my_module');
//..
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
global.my_module = require('./my_module');
});
Use this:
https://github.com/remy/nodemon
Just run your app like this: nodemon yourApp.js
There should be some emphasis on what's happening, instead of just shotgunning modules at the OP. Also, we don't know that the files he is editing are all JS modules or that they are all using the "require" call. Take the following scenarios with a grain of salt, they are only meant to describe what is happening so you know how to work with it.
Your code has already been loaded and the server is running with it
SOLUTION You need to have a way to tell the server what code has changed so that it can reload it. You could have an endpoint set up to receive a signal, a command on the command line or a request through tcp/http that will tell it what file changed and the endpoint will reload it.
//using Express
var fs = require('fs');
app.get('reload/:file', function (req, res) {
fs.readfile(req.params.file, function (err, buffer) {
//do stuff...
});
});
Your code may have "require" calls in it which loads and caches modules
SOLUTION since these modules are cached by require, following the previous solution, you would need a line in your endpoint to delete that reference
var moduleName = req.params.file;
delete require.cache[moduleName];
require('./' + moduleName);
There's a lot of caveats to get into behind all of this, but hopefully you have a better idea of what's happening and why.
What's “Live Coding”?
In essence, it's a way to alter the program while it runs, without
restarting it. The goal, however, is to end up with a program that
works properly when we (re)start it. To be useful, it helps to have an
editor that can be customized to send code to the server.
Take a look: http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
You can also use the tool PM2. Which is a advanced production process tool for node js.
http://pm2.keymetrics.io/
I think node-inspector is your best bet.
Similar to how you can Live Edit Client side JS code in Chrome Dev tools, this utilizes the Chrome (Blink) Dev Tools Interface to provide live code editing.
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
A simple direct solution with reference to all answers available here:
Node documentation says that fs.watch is more efficient than fs.watchFile & it can watch an entire folder.
(I just started using this, so not really sure whether there are any drawbacks)
fs.watch("lib", (event_type, file_name) => {
console.log("Deleting Require cache for " + file_name);
delete require.cache[ require.resolve("./lib/" + file_name)];
});

Categories

Resources