2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 10:38:55 +00:00

Fix/performance issues with FOUC (#214)

Fix/performance issues with FOUC #191
This commit is contained in:
Jia Hao 2016-06-16 15:28:36 +08:00 committed by GitHub
parent 729b5d5749
commit 612da8ce36
3 changed files with 55 additions and 19 deletions

View File

@ -6,7 +6,7 @@ import helpers from './../../helpers/helpers';
import createMenu from './../menu/menu'; import createMenu from './../menu/menu';
import initContextMenu from './../contextMenu/contextMenu'; import initContextMenu from './../contextMenu/contextMenu';
const {isOSX, linkIsInternal, getCssToInject} = helpers; const {isOSX, linkIsInternal, getCssToInject, shouldInjectCss} = helpers;
const ZOOM_INTERVAL = 0.1; const ZOOM_INTERVAL = 0.1;
@ -129,21 +129,9 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.webContents.setUserAgent(options.userAgent); mainWindow.webContents.setUserAgent(options.userAgent);
} }
const injectCss = () => { maybeInjectCss(mainWindow);
mainWindow.webContents.insertCSS(getCssToInject());
};
mainWindow.webContents.on('did-finish-load', () => { mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('params', JSON.stringify(options)); mainWindow.webContents.send('params', JSON.stringify(options));
// remove the injection of css the moment the page is loaded
mainWindow.webContents.removeListener('did-get-response-details', injectCss);
});
// on every page navigation inject the css
mainWindow.webContents.on('did-start-loading', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// will run multiple times
mainWindow.webContents.on('did-get-response-details', injectCss);
}); });
if (options.counter) { if (options.counter) {
@ -210,4 +198,28 @@ function maybeHideWindow(window, event, fastQuit) {
// will close the window on other platforms // will close the window on other platforms
} }
function maybeInjectCss(browserWindow) {
if (!shouldInjectCss()) {
return;
}
const cssToInject = getCssToInject();
const injectCss = () => {
browserWindow.webContents.insertCSS(cssToInject);
};
browserWindow.webContents.on('did-finish-load', () => {
// remove the injection of css the moment the page is loaded
browserWindow.webContents.removeListener('did-get-response-details', injectCss);
});
// on every page navigation inject the css
browserWindow.webContents.on('did-navigate', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// will run multiple times
browserWindow.webContents.on('did-get-response-details', injectCss);
});
}
export default createMainWindow; export default createMainWindow;

View File

@ -23,18 +23,38 @@ function linkIsInternal(currentUrl, newUrl) {
return currentDomain === newDomain; return currentDomain === newDomain;
} }
function getCssToInject() { function shouldInjectCss() {
const needToInject = fs.existsSync(INJECT_CSS_PATH); try {
if (!needToInject) { fs.accessSync(INJECT_CSS_PATH, fs.F_OK);
return ''; return true;
} catch (e) {
return false;
} }
}
function getCssToInject() {
return fs.readFileSync(INJECT_CSS_PATH).toString(); return fs.readFileSync(INJECT_CSS_PATH).toString();
} }
/**
* Helper method to print debug messages from the main process in the browser window
* @param {BrowserWindow} browserWindow
* @param message
*/
function debugLog(browserWindow, message) {
// need the timeout as it takes time for the preload javascript to be loaded in the window
setTimeout(() => {
browserWindow.webContents.send('debug', message);
}, 3000);
console.log(message);
}
export default { export default {
isOSX, isOSX,
isLinux, isLinux,
isWindows, isWindows,
linkIsInternal, linkIsInternal,
getCssToInject getCssToInject,
debugLog,
shouldInjectCss
}; };

View File

@ -37,6 +37,10 @@ ipcRenderer.on('params', (event, message) => {
console.log('nativefier.json', appArgs); console.log('nativefier.json', appArgs);
}); });
ipcRenderer.on('debug', (event, message) => {
console.log('debug:', message);
});
ipcRenderer.on('change-zoom', (event, message) => { ipcRenderer.on('change-zoom', (event, message) => {
webFrame.setZoomFactor(message); webFrame.setZoomFactor(message);
}); });