Skip to content

Commit e4c712d

Browse files
Fix html content access (#1317)
* fix. http get always returning html * fix. styles and javascript not loading * format
1 parent 04e027b commit e4c712d

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

src/lib/run.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ async function run(
155155
}
156156

157157
function startServer() {
158+
//isFallback = true;
158159
webServer?.stop();
159160
webServer = CreateServer(port, openBrowser, onError);
160161
webServer.setOnRequestHandler(handleRequest);
@@ -179,10 +180,15 @@ async function run(
179180
const reqId = req.requestId;
180181
let reqPath = req.path.substring(1);
181182

183+
console.log(`XREQPATH ${reqPath}`);
184+
console.log(req);
185+
182186
if (!reqPath || (reqPath.endsWith("/") && reqPath.length === 1)) {
183187
reqPath = getRelativePath();
184188
}
185189

190+
console.log(`XREQPATH1 ${reqPath}`);
191+
186192
const ext = Url.extname(reqPath);
187193
let url = null;
188194

@@ -253,31 +259,41 @@ async function run(
253259

254260
let file = activeFile.SAFMode === "single" ? activeFile : null;
255261

256-
if (pathName && isFallback) {
262+
if (pathName) {
257263
const projectFolder = addedFolder[0];
264+
const query = url.split("?")[1];
265+
let rootFolder = "";
258266

259-
//set the root folder to the file parent if no project folder is set
260-
let rootFolder = pathName;
261-
if (projectFolder !== undefined) {
267+
if (
268+
projectFolder !== undefined &&
269+
pathName.includes(projectFolder.url)
270+
) {
262271
rootFolder = projectFolder.url;
272+
} else {
273+
rootFolder = pathName;
263274
}
264-
const query = url.split("?")[1];
265275

266-
//remove the query string if present this is needs to be removed because the url is not valid
267-
if (rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) {
268-
if (rootFolder.includes("?")) {
269-
rootFolder = rootFolder.split("?")[0];
270-
}
276+
if (
277+
(rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) &&
278+
rootFolder.includes("?")
279+
) {
280+
rootFolder = rootFolder.split("?")[0];
271281
}
272282

273-
url = Url.join(rootFolder, reqPath);
283+
rootFolder = rootFolder.replace(/\/+$/, ""); // remove trailing slash
284+
reqPath = reqPath.replace(/^\/+/, ""); // remove leading slash
285+
286+
const rootParts = rootFolder.split("/");
287+
const pathParts = reqPath.split("/");
274288

275-
//attach the ftp query string to the url
276-
if (query) {
277-
url = `${url}?${query}`;
289+
if (pathParts[0] === rootParts[rootParts.length - 1]) {
290+
pathParts.shift();
278291
}
279292

280-
console.log("url", url);
293+
const fullPath = Url.join(rootFolder, pathParts.join("/"));
294+
295+
// Add back the query if present
296+
url = query ? `${fullPath}?${query}` : fullPath;
281297

282298
file = editorManager.getFile(url, "uri");
283299
} else if (!activeFile.uri) {
@@ -544,17 +560,15 @@ async function run(
544560

545561
// Set the root folder to the file parent if no project folder is set
546562
let rootFolder = pathName;
547-
if (projectFolder !== undefined) {
563+
if (projectFolder !== undefined && pathName.includes(projectFolder)) {
548564
rootFolder = projectFolder.url;
565+
} else {
566+
rootFolder = pathName;
549567
}
550568

551569
//make the uri absolute if necessary
552570
rootFolder = makeUriAbsoluteIfNeeded(rootFolder);
553571

554-
console.log("rootFolder", rootFolder);
555-
console.log("pathName", pathName);
556-
console.log("filename", filename);
557-
558572
// Parent of the file
559573
let filePath = pathName;
560574

0 commit comments

Comments
 (0)