Skip to content

Commit 59dbe2c

Browse files
committed
fix: live previewed pages bloated with live preview scripts
1 parent 2f978ef commit 59dbe2c

8 files changed

Lines changed: 141 additions & 149 deletions

File tree

src/LiveDevelopment/LiveDevMultiBrowser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ define(function (require, exports, module) {
498498
doc._ensureMasterEditor();
499499
_liveDocument = _createLiveDocument(doc, doc._masterEditor);
500500
_server.add(_liveDocument);
501+
_server.addVirtualContentAtPath(
502+
`${_liveDocument.doc.file.parentPath}${LiveDevProtocol.LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME}`,
503+
_protocol.getRemoteScriptContents());
501504
}
502505

503506

src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@
3939
define(function (require, exports, module) {
4040

4141

42-
var EventDispatcher = require("utils/EventDispatcher");
42+
const EventDispatcher = require("utils/EventDispatcher");
4343

4444
// Text of the script we'll inject into the browser that handles protocol requests.
45-
var LiveDevProtocolRemote = require("text!LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js"),
45+
const LiveDevProtocolRemote = require("text!LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js"),
4646
DocumentObserver = require("text!LiveDevelopment/BrowserScripts/DocumentObserver.js"),
4747
RemoteFunctions = require("text!LiveDevelopment/BrowserScripts/RemoteFunctions.js"),
4848
EditorManager = require("editor/EditorManager"),
4949
LiveDevMultiBrowser = require("LiveDevelopment/LiveDevMultiBrowser"),
5050
HTMLInstrumentation = require("LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation"),
5151
FileViewController = require("project/FileViewController");
5252

53+
const LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = "phoenix_live_preview_scripts_instrumented_345Tt96G4.js";
54+
5355
/**
5456
* @private
5557
* Active connections.
@@ -232,13 +234,13 @@ define(function (require, exports, module) {
232234
* Includes the <script> tags.
233235
* @return {string}
234236
*/
235-
function getRemoteFunctionsScript() {
236-
var script = "";
237+
function _getRemoteFunctionsScript() {
238+
let script = "";
237239
// Inject DocumentObserver into the browser (tracks related documents)
238240
script += DocumentObserver;
239241
// Inject remote functions into the browser.
240-
script += "window._LD=(" + RemoteFunctions + "(" + JSON.stringify(LiveDevMultiBrowser.config) + "))";
241-
return "<script>\n" + script + "</script>\n";
242+
script += "\nwindow._LD=(" + RemoteFunctions + "(" + JSON.stringify(LiveDevMultiBrowser.config) + "))";
243+
return "\n" + script + "\n";
242244
}
243245

244246
/**
@@ -247,14 +249,25 @@ define(function (require, exports, module) {
247249
* This script will also include the script required by the transport, if any.
248250
* @return {string}
249251
*/
250-
function getRemoteScript() {
251-
var transportScript = _transport.getRemoteScript() || "";
252-
var remoteFunctionsScript = getRemoteFunctionsScript() || "";
252+
function getRemoteScriptContents() {
253+
const transportScript = _transport.getRemoteScript() || "";
254+
const remoteFunctionsScript = _getRemoteFunctionsScript() || "";
253255
return transportScript +
254-
"<script>\n" + LiveDevProtocolRemote + "</script>\n" +
256+
"\n" + LiveDevProtocolRemote + "\n" +
255257
remoteFunctionsScript;
256258
}
257259

260+
/**
261+
* Returns a script that should be injected into the HTML that's launched in the
262+
* browser in order to handle protocol requests. Includes the <script> tags.
263+
* This script will also include the script required by the transport, if any.
264+
* @return {string}
265+
*/
266+
function getRemoteScript() {
267+
// give a wrong random file name that wont have a possibility of an actual file name
268+
return `\n\t\t<script src="${LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME}"></script>`;
269+
}
270+
258271
/**
259272
* Protocol method. Evaluates the given script in the browser (in global context), and returns a promise
260273
* that will be fulfilled with the result of the script, if any.
@@ -374,6 +387,7 @@ define(function (require, exports, module) {
374387
// public API
375388
exports.setTransport = setTransport;
376389
exports.getRemoteScript = getRemoteScript;
390+
exports.getRemoteScriptContents = getRemoteScriptContents;
377391
exports.evaluate = evaluate;
378392
exports.setStylesheetText = setStylesheetText;
379393
exports.getStylesheetText = getStylesheetText;
@@ -382,4 +396,5 @@ define(function (require, exports, module) {
382396
exports.close = close;
383397
exports.getConnectionIds = getConnectionIds;
384398
exports.closeAllConnections = closeAllConnections;
399+
exports.LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME;
385400
});

src/LiveDevelopment/MultiBrowserImpl/transports/ServiceWorkerTransport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ define(function (require, exports, module) {
6767
* @return {string}
6868
*/
6969
function getRemoteScript() {
70-
return "<script>\n" +
70+
return "\n" +
7171
`window.LIVE_PREVIEW_BROADCAST_CHANNEL_ID = "${BROADCAST_CHANNEL_ID}";\n` +
7272
`window.LIVE_PREVIEW_DEBIG_ENABLED = ${window.loggingOptions.logLivePreview};\n` +
7373
ServiceWorkerTransportRemote +
74-
"</script>\n";
74+
"\n";
7575
}
7676

7777
EventDispatcher.makeEventDispatcher(exports);

src/LiveDevelopment/Servers/BaseServer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ define(function (require, exports, module) {
4040
this._root = config.root; // ProjectManager.getProjectRoot().fullPath
4141
this._pathResolver = config.pathResolver; // ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath)
4242
this._liveDocuments = {};
43+
this._virtualServingDocuments = {};
4344
}
4445

4546
/**
@@ -176,6 +177,15 @@ define(function (require, exports, module) {
176177
this._liveDocuments[key] = liveDocument;
177178
};
178179

180+
/**
181+
* This will add the given text to be served when the path is hit in server. You can use this to either
182+
* serve a file that doesn't exist in project, or to override a given path to the contents you give.
183+
*/
184+
BaseServer.prototype.addVirtualContentAtPath = function (fullPath, docText) {
185+
let key = this._documentKey(fullPath);
186+
this._virtualServingDocuments[key] = docText;
187+
};
188+
179189
/**
180190
* Removes a live document from the server
181191
* @param {Object} liveDocument
@@ -192,6 +202,16 @@ define(function (require, exports, module) {
192202
}
193203
};
194204

205+
/**
206+
* removes path added by addVirtualContentAtPath()
207+
*/
208+
BaseServer.prototype.removeVirtualContentAtPath = function (fullPath) {
209+
let key = this._documentKey(fullPath);
210+
if(this._virtualServingDocuments[key]) {
211+
delete this._virtualServingDocuments[key];
212+
}
213+
};
214+
195215
/**
196216
* Lookup a live document using it's full path key
197217
* @param {string} path Absolute path to covert to a URL
@@ -207,6 +227,7 @@ define(function (require, exports, module) {
207227
*/
208228
BaseServer.prototype.clear = function () {
209229
this._liveDocuments = {};
230+
this._virtualServingDocuments = {};
210231
};
211232

212233
/**

src/extensions/default/StaticServer/StaticServer.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ define(function (require, exports, module) {
140140
* @return {jQuery.Promise} Resolved by the StaticServer domain when the message is acknowledged.
141141
*/
142142
StaticServer.prototype._updateInstrumentedURLSInWorker = function () {
143-
let paths = Object.keys(this._liveDocuments);
143+
let paths = Object.keys(this._liveDocuments)
144+
.concat(Object.keys(this._virtualServingDocuments));
144145
console.log(`Static server _updateInstrumentedURLSInWorker: `, this._root, paths);
145146

146147
window.messageSW({
@@ -166,6 +167,17 @@ define(function (require, exports, module) {
166167
return $.Deferred().resolve().promise(); // virtual server is always assumed present in phoenix
167168
};
168169

170+
/**
171+
* This will add the given text to be served when the path is hit in server. use this to either serve a file
172+
* that doesn't exist in project, or to override a given path to the contents you give.
173+
*/
174+
StaticServer.prototype.addVirtualContentAtPath = function (path, docText) {
175+
BaseServer.prototype.addVirtualContentAtPath.call(this, path, docText);
176+
177+
// update the paths to watch
178+
this._updateInstrumentedURLSInWorker();
179+
};
180+
169181
/**
170182
* See BaseServer#add. StaticServer ignores documents that do not have
171183
* a setInstrumentationEnabled method. Updates request filters.
@@ -191,6 +203,16 @@ define(function (require, exports, module) {
191203
this._updateInstrumentedURLSInWorker();
192204
};
193205

206+
/**
207+
* removes path added by addVirtualContentAtPath()
208+
*/
209+
StaticServer.prototype.removeVirtualContentAtPath = function (path) {
210+
BaseServer.prototype.removeVirtualContentAtPath.call(this, path);
211+
212+
// update the paths to watch
213+
this._updateInstrumentedURLSInWorker();
214+
};
215+
194216
/**
195217
* See BaseServer#clear. Updates request filters.
196218
*/
@@ -258,17 +280,27 @@ define(function (require, exports, module) {
258280
}
259281
let path = this._documentKey(data.path),
260282
requestID = data.requestID,
261-
liveDocument = this._liveDocuments[path];
262-
let response = {};
263-
if (liveDocument && liveDocument.getResponseData) {
283+
liveDocument = this._liveDocuments[path],
284+
virtualDocument = this._virtualServingDocuments[path];
285+
let response = {
286+
body: "instrumented document not found at static server"
287+
};
288+
289+
if (virtualDocument) {
290+
// virtual document overrides takes precedence over live preview docs
291+
response = {
292+
body: virtualDocument
293+
};
294+
} else if (liveDocument && liveDocument.getResponseData) {
264295
response = liveDocument.getResponseData();
265-
_serverBroadcastChannel.postMessage({
266-
type: 'REQUEST_RESPONSE',
267-
requestID, //pass along the requestID so that the appropriate callback will be hit at the service worker
268-
path,
269-
contents: response.body
270-
});
271296
}
297+
298+
_serverBroadcastChannel.postMessage({
299+
type: 'REQUEST_RESPONSE',
300+
requestID, //pass along the requestID so that the appropriate callback will be hit at the service worker
301+
path,
302+
contents: response.body
303+
});
272304
};
273305

274306
_serverBroadcastChannel.onmessage = (event) => {

src/phoenix/virtualServer/json-formatter.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)