Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions packages/cli/src/lib/live-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -382,11 +365,18 @@ 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;

// 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;
return;
}

// Insert new entry to the active tabs list
LiveServer.activeTabs.unshift({ url: reqUrl, client: ws });
});

var ignored = [
Expand Down Expand Up @@ -416,10 +406,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');
}
});
Expand Down Expand Up @@ -452,10 +441,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;