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('

404 - Page Not Found

') + } + + return res.redirect('/404') } - - return res.redirect('/404') } } diff --git a/package.json b/package.json index ff03e7b2..608fe623 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@beforesemicolon/html-plus", - "version": "0.4.2", + "version": "0.4.3", "description": "HTML Template Engine/Language", "main": "dist/index.js", "files": [ diff --git a/website/pages/_partials/docs/_engine-function.html b/website/pages/_partials/docs/_engine-function.html index 90075ee3..914236f8 100644 --- a/website/pages/_partials/docs/_engine-function.html +++ b/website/pages/_partials/docs/_engine-function.html @@ -15,9 +15,7 @@

Asynchronous function

const app = express(); (async () => { - // setup all custom dynamic routes here - // to override the site routes - // when you await on the engine + // setup all middlewares and any route overrides before the engine await engine(app, path.resolve(__dirname, './pages'), { staticData: { site: { @@ -31,7 +29,8 @@

Asynchronous function

})();

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 @@

Return

The engine returns undefined.

Usage Examples

-const {engine} = require('@beforesemicolon/html-plus'); - -const app = express(); - engine(app, path.resolve(__dirname, './pages'), { staticData: { site: { @@ -103,10 +98,6 @@

Usage Examples

});
-const {engine} = require('@beforesemicolon/html-plus'); - -const app = express(); - engine(app, path.resolve(__dirname, './pages'), { onPageRequest: (req) => { return { diff --git a/website/pages/_partials/docs/_getting-started.html b/website/pages/_partials/docs/_getting-started.html index 802856cf..2a55db97 100644 --- a/website/pages/_partials/docs/_getting-started.html +++ b/website/pages/_partials/docs/_getting-started.html @@ -53,11 +53,13 @@

Server Setup

const app = express(); -engine(app, path.resolve(__dirname, './pages')); +(async () => { + await engine(app, path.resolve(__dirname, './pages')); -const server = http.createServer(app); + const server = http.createServer(app); -server.listen(3000) + server.listen(3000); +})()

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 @@

{page.quick_start.title}

const app = express(); -engine(app, path.resolve(__dirname, './pages')); +(async () => { + // wait for engine to finish setting upt the routes + // and all files + await engine(app, path.resolve(__dirname, './pages')); -const server = http.createServer(app); + const server = http.createServer(app); -server.listen(3000, () => { - console.log('server live on port 3000') -}) + server.listen(3000, () => { + console.log('server live on port 3000') + }) +})() {page.quick_start.link.title}