diff --git a/core/engine/index.js b/core/engine/index.js index a7b9e66a..56018ecf 100644 --- a/core/engine/index.js +++ b/core/engine/index.js @@ -53,14 +53,18 @@ const engine = (app, pagesDirectoryPath, opt = defaultOptions) => { } else { filePath = filePath.replace(pagesDirectoryPath, ''); const template = `${filePath.replace('.html', '')}`.slice(1); + let tempPath = ''; if (filePath.endsWith('index.html')) { - pagesRoutes[filePath.replace('index.html', '')] = template; + tempPath = filePath.replace('/index.html', ''); + pagesRoutes[tempPath || '/'] = template; + pagesRoutes[`${tempPath}/`] = template; + pagesRoutes[`${tempPath}/index.html`] = template; } else { - pagesRoutes[filePath.replace('.html', '')] = template; + tempPath = filePath.replace('.html', ''); + pagesRoutes[tempPath] = template; + pagesRoutes[`${tempPath}/`] = template; } - - pagesRoutes[filePath.replace(/\/$/, '')] = template; } } diff --git a/core/engine/page-and-resources-middleware.js b/core/engine/page-and-resources-middleware.js index d7681089..9b7615e7 100644 --- a/core/engine/page-and-resources-middleware.js +++ b/core/engine/page-and-resources-middleware.js @@ -19,82 +19,84 @@ const cache = {}; function pageAndResourcesMiddleware(pagesRoutes, pagesDirectoryPath, {env, onPageRequest}) { return async (req, res, next) => { - const ext = path.extname(req.path); - - if (ext && sourcesExtensions.has(ext)) { - let content = ''; - let contentType = 'text/css'; - let resourcePath = ''; - let file = null; - - if (/node_modules/.test(req.path)) { - resourcePath = path.join(process.cwd(), req.path); - } else { - resourcePath = path.join(pagesDirectoryPath, req.path); - } - - if (env === 'production' && cache[resourcePath]) { - res.setHeader('Content-Type', cache[resourcePath].contentType); - return res.send(cache[resourcePath].content); - } + if (req.method === 'GET') { + const ext = path.extname(req.path); - file = new File(resourcePath, pagesDirectoryPath); - - try { - switch (ext) { - case '.scss': - case '.sass': - content = await transformResource.sass({file}); - content = (await transformResource.css(content, {file, env})).content; - break; - case '.less': - content = await transformResource.less({file}); - content = (await transformResource.css(content, {file, env})).content; - break; - case '.styl': - content = await transformResource.stylus({file}); - content = (await transformResource.css(content, {file, env})).content; - break; - case '.css': - content = (await transformResource.css({file, env})).content; - break; - case '.js': - case '.jsx': - case '.ts': - case '.tsx': - case '.mjs': - case '.cjs': - const result = await transformResource.js({file, env}); - content = result.content; - contentType = 'application/javascript'; - break; + if (ext && sourcesExtensions.has(ext)) { + let content = ''; + let contentType = 'text/css'; + let resourcePath = ''; + let file = null; + + if (/node_modules/.test(req.path)) { + resourcePath = path.join(process.cwd(), req.path); + } else { + resourcePath = path.join(pagesDirectoryPath, req.path); } - - if (env === 'production') { - cache[resourcePath] = {content, contentType} + + if (env === 'production' && cache[resourcePath]) { + res.setHeader('Content-Type', cache[resourcePath].contentType); + return res.send(cache[resourcePath].content); } - - res.setHeader('Content-Type', contentType); - - return res.send(content); - } catch(e) { - console.error(`Failed to load style/script content "${req.path}"`, e); - return res.sendStatus(404); - } - } else if(!ext || ext === '.html') { - const template = pagesRoutes[req.path] ?? pagesRoutes[`${req.path}/`]; + + file = new File(resourcePath, pagesDirectoryPath); + + try { + switch (ext) { + case '.scss': + case '.sass': + content = await transformResource.sass({file}); + content = (await transformResource.css(content, {file, env})).content; + break; + case '.less': + content = await transformResource.less({file}); + content = (await transformResource.css(content, {file, env})).content; + break; + case '.styl': + content = await transformResource.stylus({file}); + content = (await transformResource.css(content, {file, env})).content; + break; + case '.css': + content = (await transformResource.css({file, env})).content; + break; + case '.js': + case '.jsx': + case '.ts': + case '.tsx': + case '.mjs': + case '.cjs': + const result = await transformResource.js({file, env}); + content = result.content; + contentType = 'application/javascript'; + break; + } + + if (env === 'production') { + cache[resourcePath] = {content, contentType} + } + + res.setHeader('Content-Type', contentType); - if (template) { - return res.render(template, onPageRequest(req) || {}) + return res.send(content); + } catch(e) { + console.error(`Failed to load style/script content "${req.path}"`, e); + return res.sendStatus(404); + } } else if(!ext || ext === '.html') { - - if (req.path.startsWith('/404')) { + const template = pagesRoutes[req.path] ?? pagesRoutes[`${req.path}/`]; + + if (template) { + return res.render(template, onPageRequest(req) || {}) + } else if(!ext || ext === '.html') { + + if (req.path.startsWith('/404')) { return pagesRoutes['/404'] ? res.render(pagesRoutes['/404']) : res.send('
If you await on the engine and have some dynamic routes -you may need to set them up before otherwise any matching static route will override.
+you may need to set them up before otherwise any matching static route will override them. Prefer the engine +call as the last thing before you call the listen method for the server.If you are not awaiting the engine, you can just set the routes after. The effect is the same but not awaiting on the engine may throw an error if you get to the page before the engine is done processing your pages directory.
@@ -89,10 +88,6 @@The engine returns undefined.
HTML+ engine will setup your Routes for your pages and all handlers for CSS(including preprocessors), Javascript and Typescript files.
diff --git a/website/pages/index.html b/website/pages/index.html index 21e4e451..9e07e5a9 100644 --- a/website/pages/index.html +++ b/website/pages/index.html @@ -41,13 +41,17 @@