Skip to content

Commit 1567d25

Browse files
committed
chore: live preview ouside of custom server root will display an error page
1 parent cfd05ab commit 1567d25

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

src/assets/default-project/en/Newly_added_features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ new server settings dialog.
2020

2121
`Added on April, 2024`
2222

23-
CSS class hints are now shown within the HTML files class attribute.
23+
CSS class hints are now shown within the HTML file's class attribute.
2424

2525
![image](https://github.com/phcode-dev/phoenix/assets/5336369/112ad909-8fd0-4fc4-8042-041ecade9481)
2626

src/extensionsIntegrated/Phoenix-live-preview/BrowserStaticServer.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ define(function (require, exports, module) {
3636
EventDispatcher = require("utils/EventDispatcher"),
3737
CommandManager = require("command/CommandManager"),
3838
Commands = require("command/Commands"),
39+
StringUtils = require("utils/StringUtils"),
3940
EventManager = require("utils/EventManager"),
4041
LivePreviewSettings = require("./LivePreviewSettings"),
4142
ProjectManager = require("project/ProjectManager"),
@@ -112,10 +113,13 @@ define(function (require, exports, module) {
112113
encodeURIComponent(`${Strings.DESCRIPTION_LIVEDEV_MAIN_SPAN}`);
113114
}
114115

115-
function getNoPreviewURL(){
116+
function getNoPreviewURL(
117+
heading = Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW,
118+
message = Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_DETAILS
119+
){
116120
return `${window.Phoenix.baseURL}assets/phoenix-splash/no-preview.html?jsonInput=`+
117-
encodeURIComponent(`{"heading":"${Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW}",`
118-
+`"details":"${Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_DETAILS}"}`);
121+
encodeURIComponent(`{"heading":"${heading}",`
122+
+`"details":"${message}"}`);
119123
}
120124

121125
function _isLivePreviewSupported() {
@@ -153,6 +157,7 @@ define(function (require, exports, module) {
153157
httpFilePath = fullPath;
154158
}
155159
const customServeURL = LivePreviewSettings.getCustomServerConfig(fullPath);
160+
const shouldUseInbuiltPreview = utils.isMarkdownFile(fullPath) || utils.isSVG(fullPath);
156161
if(customServeURL){
157162
const relativePath = path.relative(projectRoot, fullPath);
158163
resolve({
@@ -165,7 +170,17 @@ define(function (require, exports, module) {
165170
serverSupportsHotReload: LivePreviewSettings.serverSupportsHotReload()
166171
});
167172
return;
168-
} else if(utils.isPreviewableFile(fullPath)){
173+
} else if(LivePreviewSettings.isUsingCustomServer() && !customServeURL && !shouldUseInbuiltPreview){
174+
// this is the case where the file is outside of a custom configured server root (E. `www/`)
175+
// like `notServed/Path.html`. For markdown and SVG, we will still use the inbuilt live preview.
176+
resolve({
177+
URL: getNoPreviewURL(Strings.DESCRIPTION_LIVEDEV_EXCLUDED,
178+
StringUtils.format(Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_EXCLUDED,
179+
LivePreviewSettings.getCustomServeRoot())),
180+
isNoPreview: true
181+
});
182+
return;
183+
} else if(utils.isPreviewableFile(fullPath)){
169184
const filePath = httpFilePath || path.relative(projectRoot, fullPath);
170185
let URL = httpFilePath || `${projectRootUrl}${filePath}`;
171186
resolve({

src/extensionsIntegrated/Phoenix-live-preview/LivePreviewSettings.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ define(function (require, exports, module) {
207207
};
208208
}
209209

210+
function getCustomServeRoot() {
211+
return PreferencesManager.get(PREFERENCE_PROJECT_SERVER_PATH) || "/";
212+
}
213+
210214
function getCustomServerConfig(fullPath) {
211215
const customServer = _resolveServer();
212216
if(!customServer || !ProjectManager.isWithinProject(fullPath)) {
@@ -227,7 +231,8 @@ define(function (require, exports, module) {
227231
// for docusaurus, we do not have a reliable way to parse the config file and deduce paths, so we always
228232
// return the root url for now.
229233
pathRelativeToServeRoot = "";
230-
} else if(!isServerRenderedURL){
234+
} else if(!isServerRenderedURL || (customServer.pathInProject !== "" &&
235+
!relativePath.startsWith(customServer.pathInProject))){ // someNonServePath/design/index.html -> cannot serve this!
231236
return null;
232237
}
233238
// www/design/index.html -> http://localhost:8000/design/index.html
@@ -255,6 +260,7 @@ define(function (require, exports, module) {
255260

256261
exports.showSettingsDialog = showSettingsDialog;
257262
exports.getCustomServerConfig = getCustomServerConfig;
263+
exports.getCustomServeRoot = getCustomServeRoot;
258264
exports.isUsingCustomServer = isUsingCustomServer;
259265
exports.getCustomServerFramework = getCustomServerFramework;
260266
exports.serverSupportsHotReload = serverSupportsHotReload;

src/extensionsIntegrated/Phoenix-live-preview/NodeStaticServer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ define(function (require, exports, module) {
648648
if(fullPath.startsWith("http://") || fullPath.startsWith("https://")){
649649
httpFilePath = fullPath;
650650
}
651+
const shouldUseInbuiltPreview = utils.isMarkdownFile(fullPath) || utils.isSVG(fullPath);
651652
const customServeURL = LivePreviewSettings.getCustomServerConfig(fullPath);
652653
if(customServeURL){
653654
const relativePath = path.relative(projectRoot, fullPath);
@@ -661,6 +662,16 @@ define(function (require, exports, module) {
661662
serverSupportsHotReload: LivePreviewSettings.serverSupportsHotReload()
662663
});
663664
return;
665+
} else if(LivePreviewSettings.isUsingCustomServer() && !customServeURL && !shouldUseInbuiltPreview){
666+
// this is the case where the file is outside of a custom configured server root (E. `www/`)
667+
// like `notServed/Path.html`. For markdown and SVG, we will still use the inbuilt live preview.
668+
resolve({
669+
URL: getNoPreviewURL(Strings.DESCRIPTION_LIVEDEV_EXCLUDED,
670+
StringUtils.format(Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_EXCLUDED,
671+
LivePreviewSettings.getCustomServeRoot())),
672+
isNoPreview: true
673+
});
674+
return;
664675
} else if(!_staticServerInstance || !_staticServerInstance.getBaseUrl()){
665676
resolve({
666677
URL: getNoPreviewURL(),

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,14 +625,17 @@ define(function (require, exports, module) {
625625
}
626626

627627
async function _currentFileChanged(_event, changedFile) {
628-
if(changedFile && _shouldShowCustomServerBar(changedFile.fullPath)){
628+
const fullPath = changedFile.fullPath;
629+
if(changedFile && _shouldShowCustomServerBar(fullPath)){
629630
_showCustomServerBar();
630631
}
631-
if(urlPinned){
632+
const shouldUseInbuiltPreview = utils.isMarkdownFile(fullPath) || utils.isSVG(fullPath);
633+
if(urlPinned || (LivePreviewSettings.isUsingCustomServer() &&
634+
!LivePreviewSettings.getCustomServerConfig(fullPath) && !shouldUseInbuiltPreview)){
632635
return;
633636
}
634-
if(changedFile && (utils.isPreviewableFile(changedFile.fullPath) ||
635-
utils.isServerRenderedFile(changedFile.fullPath))){
637+
if(changedFile && (utils.isPreviewableFile(fullPath) ||
638+
utils.isServerRenderedFile(fullPath))){
636639
if(!panelShownAtStartup){
637640
_setPanelVisibility(true);
638641
}

src/htmlContent/about-dialog.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ <h2>{{APP_NAME_ABOUT_BOX}}</h2>
1313
{{/BUILD_TIMESTAMP}}
1414
</p>
1515
<p class="dialog-message">{{{Strings.ABOUT_RELEASE_CREDITS}}}
16-
<a href="https://github.com/abose" target="_blank">Arun Bose</a><span>,&nbsp;
17-
<a href="https://github.com/charlypa" target="_blank">charly p abraham</a>,&nbsp;
18-
<a href="https://github.com/ayoung5555" target="_blank">Andrew Young</a>,&nbsp;
19-
<a href="https://github.com/faizanbhagat" target="_blank">Faizan Bhagat</a>
16+
<a href="https://github.com/abose" target="_blank" rel="noopener" >Arun Bose</a><span>,&nbsp;
17+
<a href="https://github.com/charlypa" target="_blank" rel="noopener" >charly p abraham</a>,&nbsp;
18+
<a href="https://github.com/ayoung5555" target="_blank" rel="noopener" >Andrew Young</a>,&nbsp;
19+
<a href="https://github.com/faizanbhagat" target="_blank" rel="noopener" >Faizan Bhagat</a>
2020
</p>
2121

2222
<p class="dialog-message"><!-- $NON-NLS$ -->Copyright 2021 - present <a href="https://core.ai">Core.ai</a> and its licensors. All rights reserved.</p>

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ define({
10031003
"DESCRIPTION_LIVE_DEV_HIGHLIGHT_SETTINGS": "Live Preview Highlight settings",
10041004
"DESCRIPTION_LIVEDEV_ENABLE_REVERSE_INSPECT": "false to disable live preview reverse inspect",
10051005
"DESCRIPTION_LIVEDEV_NO_PREVIEW": "Nothing to preview!",
1006+
"DESCRIPTION_LIVEDEV_EXCLUDED": "Custom Server Cannot Serve This file",
1007+
"DESCRIPTION_LIVEDEV_NO_PREVIEW_EXCLUDED": "Live preview settings is configured to only serve files from folder '{0}'",
10061008
"DESCRIPTION_LIVEDEV_NO_PREVIEW_DETAILS": "Please select an HTML file to preview",
10071009
"DESCRIPTION_LIVEDEV_PREVIEW_RESTRICTED": "Preview Unavailable!",
10081010
"DESCRIPTION_LIVEDEV_PREVIEW_RESTRICTED_DETAILS": "This HTML file is not part of the current project. For security reasons, only project files can be live-previewed. To preview this file, open its containing folder as a separate project.",

0 commit comments

Comments
 (0)