From b33c55474d592f7b51f94d2b731ec345bbf4caa5 Mon Sep 17 00:00:00 2001 From: Ze Yu Date: Mon, 9 Mar 2020 21:56:39 +0800 Subject: [PATCH] Fix lazy reload for urls without index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lazy live reloading uses the file extension of the request url to determine whether the request is for a html page, and not from a dynamic include. Hence, requests for index.html with urls that do not end with index.html fail to be detected. Let’s add extra processing to detect such requests. --- index.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 03d279b750..19010fb091 100755 --- a/index.js +++ b/index.js @@ -179,21 +179,38 @@ program if (onePagePath) { const lazyReloadMiddleware = function (req, res, next) { - const isHtmlFile = req.url.endsWith('.html'); + const urlExtension = path.posix.extname(req.url); - if (isHtmlFile) { - const isDynamicIncludeHtmlFile = req.url.endsWith('._include_.html'); + const hasEndingSlash = req.url.endsWith('/'); + const hasNoExtension = urlExtension === ''; + const isHtmlFileRequest = urlExtension === '.html' || hasEndingSlash || hasNoExtension; - if (!isDynamicIncludeHtmlFile) { - const urlWithoutBaseUrl = req.url.replace(config.baseUrl, ''); - const urlWithoutExtension = fsUtil.removeExtension(urlWithoutBaseUrl); + if (!isHtmlFileRequest || req.url.endsWith('._include_.html')) { + next(); + return; + } - const didInitiateRebuild = site.changeCurrentPage(urlWithoutExtension); - if (didInitiateRebuild) { - req.url = ensurePosix(path.join(config.baseUrl || '/', LAZY_LOADING_SITE_FILE_NAME)); - } + if (hasNoExtension && !hasEndingSlash) { + // Urls of type 'host/userGuide' - check if 'userGuide' is a raw file or does not exist + const diskFilePath = path.resolve(rootFolder, req.url); + if (!fs.existsSync(diskFilePath) || !fs.isDirectorySync(diskFilePath)) { + // Request for a raw file + next(); + return; } } + + const urlWithoutBaseUrl = req.url.replace(config.baseUrl, ''); + // Map 'hostname/userGuide/' and 'hostname/userGuide' to hostname/userGuide/index. + const urlWithIndex = (hasNoExtension || hasEndingSlash) + ? path.posix.join(urlWithoutBaseUrl, 'index') + : urlWithoutBaseUrl; + const urlWithoutExtension = fsUtil.removeExtension(urlWithIndex); + + const didInitiateRebuild = site.changeCurrentPage(urlWithoutExtension); + if (didInitiateRebuild) { + req.url = ensurePosix(path.join(config.baseUrl || '/', LAZY_LOADING_SITE_FILE_NAME)); + } next(); };