From 1924843b8ce38eaafa6f23d9e0edda4e755398c6 Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Sun, 28 Mar 2021 22:18:45 +0800 Subject: [PATCH 1/6] Move active tab entry addition to socket establishment phase --- packages/cli/src/lib/live-server/index.js | 47 +++++++++-------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/packages/cli/src/lib/live-server/index.js b/packages/cli/src/lib/live-server/index.js index 5ab2237c13..facdafe75b 100644 --- a/packages/cli/src/lib/live-server/index.js +++ b/packages/cli/src/lib/live-server/index.js @@ -9,7 +9,7 @@ * clients list is stored internally. * * This patch allows us to gain access to the information that can be gathered with the client - * websockets, which in turn can enables the support for multiple-tab development. + * websockets, which in turn enables the support for multiple-tab development. * * Patch is written against live-server v1.2.1 * The **only** changes are prefaced with a CHANGED comment @@ -88,23 +88,6 @@ function staticServer(root) { } } - // CHANGED: Added line to create new entry on non-include and non-live-reload requests - const reqUrl = req.originalUrl; - if (!reqUrl.endsWith('._include_.html') && !LiveServer.isLiveReloadRequest(reqUrl)) { - /* - * TODO: Find a way to handle the edge case of a tab that is immediately closed before socket - * establishment happens. Current behaviour is that the tab will remain forever in the list. - * Context: https://github.com/MarkBind/markbind/pull/1513#issuecomment-803025676 - */ - const tabEntry = { - url: reqUrl, - client: undefined, - prevClient: undefined, - isReloading: false, - } - LiveServer.activeTabs.unshift(tabEntry); - } - if (injectTag === null && LiveServer.logLevel >= 3) { console.warn("Failed to inject refresh script!".yellow, "Couldn't find any of the tags ", injectCandidates, "from", filepath); @@ -382,11 +365,22 @@ LiveServer.start = function(options) { LiveServer.activeTabs = LiveServer.activeTabs.filter(tab => tab.client !== ws); }; - // CHANGED: Added line to record tab client socket on creation + // CHANGED: Enhanced client websocket addition process to record the client as an active tab entry const reqUrl = path.dirname(request.url); - const tab = LiveServer.activeTabs.find(tab => tab.url === reqUrl && !tab.client); - tab.client = ws; - tab.isReloading = false; + // Guard clause for MarkBind's _include_ files, no need to recognize as an active tab + if (reqUrl.endsWith('._include_.html')) { + return; + } + + // If present an entry with empty client, reuse existing entry to maintain order from pre-reload + const existingTab = LiveServer.activeTabs.find(tab => tab.url === reqUrl && !tab.client); + if (existingTab) { + existingTab.client = ws; + return; + } + + // Insert new entry to the active tabs list + LiveServer.activeTabs.unshift({ url: reqUrl, client: ws}); }); var ignored = [ @@ -416,10 +410,9 @@ LiveServer.start = function(options) { // CHANGED: Prepare tab entry data before issuing reload LiveServer.activeTabs.forEach(tab => { if (tab.client) { + // Clear the client from the entry to be refilled in the socket establishment phase after reload const client = tab.client; tab.client = undefined; - tab.prevClient = client; - tab.isReloading = true; client.send(cssChange ? 'refreshcss' : 'reload'); } }); @@ -452,10 +445,6 @@ LiveServer.shutdown = function() { }; // CHANGED: Added method to retrieve current active urls -LiveServer.getActiveUrls = () => LiveServer.activeTabs.filter(tab => !tab.isReloading).map(tab => tab.url); - -// CHANGED: Added method to check whether a request is a product of the live reload mechanism -LiveServer.isLiveReloadRequest = (reqUrl) => - LiveServer.activeTabs.some(tab => tab.url === reqUrl && tab.isReloading); +LiveServer.getActiveUrls = () => LiveServer.activeTabs.filter(tab => tab.client).map(tab => tab.url); module.exports = LiveServer; From ec521da032ee7a1ac004750150b16d7163b33c1c Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Mon, 29 Mar 2021 01:51:49 +0800 Subject: [PATCH 2/6] Add extra guard for opened pages sync on fs events --- packages/cli/index.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index a8ff64ebf9..61bb483a18 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -140,9 +140,11 @@ program const addHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); + if (onePagePath) { + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); + } Promise.resolve('').then(() => { if (site.isFilepathAPage(filePath) || site.isDependencyOfPage(filePath)) { return site.rebuildSourceFiles(filePath); @@ -155,9 +157,11 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); + if (onePagePath) { + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); + } Promise.resolve('').then(() => { if (path.basename(filePath) === SITE_CONFIG_NAME) { return site.reloadSiteConfig(); @@ -173,9 +177,11 @@ program const removeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file deletion: ${filePath}`); - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); + if (onePagePath) { + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); + } Promise.resolve('').then(() => { if (site.isFilepathAPage(filePath) || site.isDependencyOfPage(filePath)) { return site.rebuildSourceFiles(filePath); From ab9662e18bdea3f10ce125c67cf6c1d1894eb8fc Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Wed, 31 Mar 2021 22:46:23 +0800 Subject: [PATCH 3/6] Correctly process incomplete active urls to refer to index files --- packages/cli/index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index 61bb483a18..20a9da1161 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -142,7 +142,10 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { + const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; + return fsUtil.removeExtension(completeUrl); + }); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { @@ -159,7 +162,10 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { + const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; + return fsUtil.removeExtension(completeUrl); + }); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { @@ -179,7 +185,10 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file deletion: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { + const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; + return fsUtil.removeExtension(completeUrl); + }); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { From 083d869c7ead47ffcd92a09ae13ead58b5962537 Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Fri, 2 Apr 2021 00:07:38 +0800 Subject: [PATCH 4/6] Revert "Correctly process incomplete active urls to refer to index files" This reverts commit ab9662e18bdea3f10ce125c67cf6c1d1894eb8fc. --- packages/cli/index.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index 20a9da1161..61bb483a18 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -142,10 +142,7 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { - const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; - return fsUtil.removeExtension(completeUrl); - }); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { @@ -162,10 +159,7 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { - const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; - return fsUtil.removeExtension(completeUrl); - }); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { @@ -185,10 +179,7 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file deletion: ${filePath}`); if (onePagePath) { logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map((url) => { - const completeUrl = path.extname(url) === '' ? path.join(url, 'index') : url; - return fsUtil.removeExtension(completeUrl); - }); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); site.changeCurrentOpenedPages(normalizedActiveUrls); } Promise.resolve('').then(() => { From fd9a1a9377cafefb9dd82ce75dc03a5f2360e8b7 Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Fri, 2 Apr 2021 00:09:18 +0800 Subject: [PATCH 5/6] Address reviews --- packages/cli/src/lib/live-server/index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/lib/live-server/index.js b/packages/cli/src/lib/live-server/index.js index facdafe75b..5b2d04ccfe 100644 --- a/packages/cli/src/lib/live-server/index.js +++ b/packages/cli/src/lib/live-server/index.js @@ -367,12 +367,8 @@ LiveServer.start = function(options) { // CHANGED: Enhanced client websocket addition process to record the client as an active tab entry const reqUrl = path.dirname(request.url); - // Guard clause for MarkBind's _include_ files, no need to recognize as an active tab - if (reqUrl.endsWith('._include_.html')) { - return; - } - // If present an entry with empty client, reuse existing entry to maintain order from pre-reload + // If an entry with empty client is present, reuse existing entry to maintain order from pre-reload const existingTab = LiveServer.activeTabs.find(tab => tab.url === reqUrl && !tab.client); if (existingTab) { existingTab.client = ws; @@ -380,7 +376,7 @@ LiveServer.start = function(options) { } // Insert new entry to the active tabs list - LiveServer.activeTabs.unshift({ url: reqUrl, client: ws}); + LiveServer.activeTabs.unshift({ url: reqUrl, client: ws }); }); var ignored = [ From 0eccf8bffc2eb0a1cfc4a48452e5ac3370a306aa Mon Sep 17 00:00:00 2001 From: Ryo Armanda Date: Fri, 2 Apr 2021 00:11:21 +0800 Subject: [PATCH 6/6] Revert "Add extra guard for opened pages sync on fs events" This reverts commit ec521da032ee7a1ac004750150b16d7163b33c1c. --- packages/cli/index.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index 61bb483a18..a8ff64ebf9 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -140,11 +140,9 @@ program const addHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); - if (onePagePath) { - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); - } + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); Promise.resolve('').then(() => { if (site.isFilepathAPage(filePath) || site.isDependencyOfPage(filePath)) { return site.rebuildSourceFiles(filePath); @@ -157,11 +155,9 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); - if (onePagePath) { - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); - } + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); Promise.resolve('').then(() => { if (path.basename(filePath) === SITE_CONFIG_NAME) { return site.reloadSiteConfig(); @@ -177,11 +173,9 @@ program const removeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file deletion: ${filePath}`); - if (onePagePath) { - logger.info('Synchronizing opened pages list before reload'); - const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); - site.changeCurrentOpenedPages(normalizedActiveUrls); - } + logger.info('Synchronizing opened pages list before reload'); + const normalizedActiveUrls = liveServer.getActiveUrls().map(url => fsUtil.removeExtension(url)); + site.changeCurrentOpenedPages(normalizedActiveUrls); Promise.resolve('').then(() => { if (site.isFilepathAPage(filePath) || site.isDependencyOfPage(filePath)) { return site.rebuildSourceFiles(filePath);