danish danish - 6 months ago 40
Node.js Question

Electron not using updated css file

I am using electron for making an app . Till Now I was able to load my css file (updated version) . But suddenly I have found that if I make any change in my css file , the changes are no longer reflected in electron . Similar thing was happening in case wjen I used to run same thing in Chrome browser but I got it working by clearing cache .
I think clearing cache in electron might help me as well but I dont know how can I do that .
I got this piece of code from somewhere , but I dont know how to use this :(

var remote = require('remote');
var win = remote.getCurrentWindow();
win.webContents.session.clearCache(function(){
//some callback.
}


Can some one guide me on how can I rectify my problem .

UPDATED :

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var mainWindow = null;

app.on('window-all-closed', function() {
app.quit();
});


app.on('ready', function() {
var subpy = require('child_process').spawn('python', ['./index.py']);

var rq = require('request-promise');
var mainAddr = 'http://localhost:5000';

var openWindow = function(){
mainWindow = new BrowserWindow({width:1200, height: 700});

mainWindow.loadURL('http://localhost:5000');
mainWindow.webContents.openDevTools();
mainWindow.on('closed', function() {
mainWindow = null;
subpy.kill('SIGINT');
});
};

var startUp = function(){
rq(mainAddr)
.then(function(htmlString){
console.log('server started!');
openWindow();
})
.catch(function(err){
startUp();
});
};

startUp();
});

Answer

From the documentation site:

const {BrowserWindow} = require('electron');

let win = new BrowserWindow({width: 800, height: 600});
win.loadURL('http://github.com');

const ses = win.webContents.session.clearCache(function() {
});

So, the following function will return the window you have and save it on the win variable.

var remote = require('remote'); 
var win = remote.getCurrentWindow();

And then, using the same function that you have like on the doc site:

win.webContents.session.clearCache(function(){
//some callback.
}

You will clean the cache from the current window you have. If you have stored the value of your windows before on some variable, you don't need to get it with the "getCurrentWindow() function