diff --git a/src/lib/run.js b/src/lib/run.js index 1934d2e93..3f5e9ec36 100644 --- a/src/lib/run.js +++ b/src/lib/run.js @@ -155,6 +155,7 @@ async function run( } function startServer() { + //isFallback = true; webServer?.stop(); webServer = CreateServer(port, openBrowser, onError); webServer.setOnRequestHandler(handleRequest); @@ -179,10 +180,15 @@ async function run( const reqId = req.requestId; let reqPath = req.path.substring(1); + console.log(`XREQPATH ${reqPath}`); + console.log(req); + if (!reqPath || (reqPath.endsWith("/") && reqPath.length === 1)) { reqPath = getRelativePath(); } + console.log(`XREQPATH1 ${reqPath}`); + const ext = Url.extname(reqPath); let url = null; @@ -253,31 +259,41 @@ async function run( let file = activeFile.SAFMode === "single" ? activeFile : null; - if (pathName && isFallback) { + if (pathName) { const projectFolder = addedFolder[0]; + const query = url.split("?")[1]; + let rootFolder = ""; - //set the root folder to the file parent if no project folder is set - let rootFolder = pathName; - if (projectFolder !== undefined) { + if ( + projectFolder !== undefined && + pathName.includes(projectFolder.url) + ) { rootFolder = projectFolder.url; + } else { + rootFolder = pathName; } - const query = url.split("?")[1]; - //remove the query string if present this is needs to be removed because the url is not valid - if (rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) { - if (rootFolder.includes("?")) { - rootFolder = rootFolder.split("?")[0]; - } + if ( + (rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) && + rootFolder.includes("?") + ) { + rootFolder = rootFolder.split("?")[0]; } - url = Url.join(rootFolder, reqPath); + rootFolder = rootFolder.replace(/\/+$/, ""); // remove trailing slash + reqPath = reqPath.replace(/^\/+/, ""); // remove leading slash + + const rootParts = rootFolder.split("/"); + const pathParts = reqPath.split("/"); - //attach the ftp query string to the url - if (query) { - url = `${url}?${query}`; + if (pathParts[0] === rootParts[rootParts.length - 1]) { + pathParts.shift(); } - console.log("url", url); + const fullPath = Url.join(rootFolder, pathParts.join("/")); + + // Add back the query if present + url = query ? `${fullPath}?${query}` : fullPath; file = editorManager.getFile(url, "uri"); } else if (!activeFile.uri) { @@ -544,17 +560,15 @@ async function run( // Set the root folder to the file parent if no project folder is set let rootFolder = pathName; - if (projectFolder !== undefined) { + if (projectFolder !== undefined && pathName.includes(projectFolder)) { rootFolder = projectFolder.url; + } else { + rootFolder = pathName; } //make the uri absolute if necessary rootFolder = makeUriAbsoluteIfNeeded(rootFolder); - console.log("rootFolder", rootFolder); - console.log("pathName", pathName); - console.log("filename", filename); - // Parent of the file let filePath = pathName;