2015-11-24 18:12:50 +03:00
|
|
|
// # Templates
|
|
|
|
//
|
|
|
|
// Figure out which template should be used to render a request
|
|
|
|
// based on the templates which are allowed, and what is available in the theme
|
2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const url = require('url');
|
2022-04-05 21:31:49 +03:00
|
|
|
const config = require('../../../shared/config');
|
|
|
|
const themeEngine = require('../theme-engine');
|
2020-04-29 18:44:27 +03:00
|
|
|
const _private = {};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2017-03-14 12:06:42 +03:00
|
|
|
/**
|
2019-04-22 00:55:22 +03:00
|
|
|
* @description Get Error Template Hierarchy
|
2017-03-14 12:06:42 +03:00
|
|
|
*
|
|
|
|
* Fetch the ordered list of templates that can be used to render this error statusCode.
|
|
|
|
*
|
|
|
|
* The default is the
|
|
|
|
*
|
|
|
|
* @param {integer} statusCode
|
|
|
|
* @returns {String[]}
|
|
|
|
*/
|
2017-11-10 15:44:29 +03:00
|
|
|
_private.getErrorTemplateHierarchy = function getErrorTemplateHierarchy(statusCode) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const errorCode = _.toString(statusCode);
|
|
|
|
const templateList = ['error'];
|
2017-03-14 12:06:42 +03:00
|
|
|
|
|
|
|
// Add error class template: E.g. error-4xx.hbs or error-5xx.hbs
|
|
|
|
templateList.unshift('error-' + errorCode[0] + 'xx');
|
|
|
|
|
|
|
|
// Add statusCode specific template: E.g. error-404.hbs
|
|
|
|
templateList.unshift('error-' + errorCode);
|
|
|
|
|
|
|
|
return templateList;
|
2017-11-10 15:44:29 +03:00
|
|
|
};
|
2017-03-14 12:06:42 +03:00
|
|
|
|
2015-11-24 18:12:50 +03:00
|
|
|
/**
|
2019-04-22 00:55:22 +03:00
|
|
|
* @description Get Template Hierarchy
|
2015-11-24 18:12:50 +03:00
|
|
|
*
|
|
|
|
* Fetch the ordered list of templates that can be used to render this request.
|
|
|
|
* 'index' is the default / fallback
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
* For collections with slugs: [:collectionName-:slug, :collectionName, index]
|
|
|
|
* For collections without slugs: [:collectionName, index]
|
|
|
|
* Collections can also have a front page template which is used if this is the first page of the collections, e.g. 'home.hbs'
|
2015-11-24 18:12:50 +03:00
|
|
|
*
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
* @param {Object} routerOptions
|
2015-11-24 18:12:50 +03:00
|
|
|
* @returns {String[]}
|
|
|
|
*/
|
2018-06-24 01:32:08 +03:00
|
|
|
_private.getEntriesTemplateHierarchy = function getEntriesTemplateHierarchy(routerOptions, requestOptions) {
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
const templateList = ['index'];
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2018-06-21 21:18:29 +03:00
|
|
|
// CASE: author, tag, custom collection name
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
if (routerOptions.name && routerOptions.name !== 'index') {
|
|
|
|
templateList.unshift(routerOptions.name);
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2018-06-11 23:06:47 +03:00
|
|
|
if (routerOptions.slugTemplate && requestOptions.slugParam) {
|
|
|
|
templateList.unshift(routerOptions.name + '-' + requestOptions.slugParam);
|
2015-11-24 18:12:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 01:32:08 +03:00
|
|
|
// CASE: collections/channels can define a template list
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
if (routerOptions.templates && routerOptions.templates.length) {
|
|
|
|
routerOptions.templates.forEach((template) => {
|
|
|
|
templateList.unshift(template);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routerOptions.frontPageTemplate && (requestOptions.path === '/' || requestOptions.path === '/' && requestOptions.page === 1)) {
|
|
|
|
templateList.unshift(routerOptions.frontPageTemplate);
|
2015-11-24 18:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return templateList;
|
2017-11-10 15:44:29 +03:00
|
|
|
};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
|
|
|
/**
|
2019-04-22 00:55:22 +03:00
|
|
|
* @description Get Entry Template Hierarchy
|
2015-11-24 18:12:50 +03:00
|
|
|
*
|
|
|
|
* Fetch the ordered list of templates that can be used to render this request.
|
|
|
|
* 'post' is the default / fallback
|
2017-10-10 15:36:35 +03:00
|
|
|
* For posts: [post-:slug, custom-*, post]
|
|
|
|
* For pages: [page-:slug, custom-*, page, post]
|
2015-11-24 18:12:50 +03:00
|
|
|
*
|
2017-10-10 15:36:35 +03:00
|
|
|
* @param {Object} postObject
|
2015-11-24 18:12:50 +03:00
|
|
|
* @returns {String[]}
|
|
|
|
*/
|
2017-11-10 15:44:29 +03:00
|
|
|
_private.getEntryTemplateHierarchy = function getEntryTemplateHierarchy(postObject) {
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
const templateList = ['post'];
|
|
|
|
let slugTemplate = 'post-' + postObject.slug;
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2017-10-10 15:36:35 +03:00
|
|
|
if (postObject.page) {
|
2015-11-24 18:12:50 +03:00
|
|
|
templateList.unshift('page');
|
2017-10-10 15:36:35 +03:00
|
|
|
slugTemplate = 'page-' + postObject.slug;
|
2015-11-24 18:12:50 +03:00
|
|
|
}
|
|
|
|
|
2017-10-10 15:36:35 +03:00
|
|
|
if (postObject.custom_template) {
|
|
|
|
templateList.unshift(postObject.custom_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
templateList.unshift(slugTemplate);
|
2015-11-24 18:12:50 +03:00
|
|
|
|
|
|
|
return templateList;
|
2017-11-10 15:44:29 +03:00
|
|
|
};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
|
|
|
/**
|
2019-04-22 00:55:22 +03:00
|
|
|
* @description Pick Template
|
2015-11-24 18:12:50 +03:00
|
|
|
*
|
|
|
|
* Taking the ordered list of allowed templates for this request
|
|
|
|
* Cycle through and find the first one which has a match in the theme
|
|
|
|
*
|
2017-03-14 02:15:50 +03:00
|
|
|
* @param {Array|String} templateList
|
|
|
|
* @param {String} fallback - a fallback template
|
2015-11-24 18:12:50 +03:00
|
|
|
*/
|
2017-11-10 15:44:29 +03:00
|
|
|
_private.pickTemplate = function pickTemplate(templateList, fallback) {
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
let template;
|
2017-03-14 02:15:50 +03:00
|
|
|
|
|
|
|
if (!_.isArray(templateList)) {
|
|
|
|
templateList = [templateList];
|
|
|
|
}
|
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
if (!themeEngine.getActive()) {
|
2017-03-14 02:15:50 +03:00
|
|
|
template = fallback;
|
|
|
|
} else {
|
2020-10-20 02:02:56 +03:00
|
|
|
template = _.find(templateList, function (templateName) {
|
|
|
|
if (!templateName) {
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
return themeEngine.getActive().hasTemplate(templateName);
|
2017-03-14 02:15:50 +03:00
|
|
|
});
|
|
|
|
}
|
2015-11-24 18:12:50 +03:00
|
|
|
|
|
|
|
if (!template) {
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
if (!fallback) {
|
|
|
|
template = 'index';
|
💡Changed static router - throw 400 for missing tpl
fixes #10990
- Changed the static router to throw a 400 error for a missing template file, rather than falling back to using the default.hbs file
- Falling back is weird and hard to understand, but throwing an error makes it clear that the user has to provide the matching template
- The new error reads 'Missing template [filename].hbs for route "[route]".'
Assume you have a route.yaml file something like:
```
routes:
/: home
```
- In Ghost v2, if you don't have a home.hbs template, Ghost falls back to using the default.hbs file if it's available
- Most themes have a default.hbs, however this file is a layout file, depended on by other templates, not a template file itself
- In production mode, using the default.hbs as a template causes weird, intermittent layout issues depending on which order pages are loaded
- This is due to this issue: https://github.com/barc/express-hbs/issues/161
- In Ghost v3, we will throw a 400 error for missing template files instead of having a fallback
- In the example above, navigating to '/' would throw the error 'Missing template home.hbs for route "/".'
2019-08-05 21:34:12 +03:00
|
|
|
} else if (_.isFunction(fallback)) {
|
|
|
|
fallback();
|
✨Dynamic Routing Beta (#9596)
refs #9601
### Dynamic Routing
This is the beta version of dynamic routing.
- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
- yaml file + validation
- routing + routers
- url service
- sitemaps
- url access
- deeper implementation of yaml validations
- e.g. hard require slashes
- ensure routing hierarchy/order
- e.g. you enable the subscriber app
- you have a custom static page, which lives under the same slug /subscribe
- static pages are stronger than apps
- e.g. the first collection owns the post it has filtered
- a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
- e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
- and you change the permalink in the admin to dated permalink
- the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
- unmount without server restart, yey
- ensure we are backwards compatible
- e.g. render home.hbs for collection index if collection route is /
- ensure you can access your configured permalink from the settings table with {globals.permalink}
### Render 503 if url service did not finish
- return 503 if the url service has not finished generating the resource urls
### Rewrite sitemaps
- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
- the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)
### Re-work usage of Url utility
- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
- only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 20:02:20 +03:00
|
|
|
} else {
|
|
|
|
template = fallback;
|
|
|
|
}
|
2015-11-24 18:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return template;
|
2017-11-10 15:44:29 +03:00
|
|
|
};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2017-11-10 15:44:29 +03:00
|
|
|
_private.getTemplateForEntry = function getTemplateForEntry(postObject) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const templateList = _private.getEntryTemplateHierarchy(postObject);
|
|
|
|
const fallback = templateList[templateList.length - 1];
|
2017-11-10 15:44:29 +03:00
|
|
|
return _private.pickTemplate(templateList, fallback);
|
|
|
|
};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2018-06-24 01:32:08 +03:00
|
|
|
_private.getTemplateForEntries = function getTemplateForEntries(routerOptions, requestOptions) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const templateList = _private.getEntriesTemplateHierarchy(routerOptions, requestOptions);
|
|
|
|
const fallback = templateList[templateList.length - 1];
|
2017-11-10 15:44:29 +03:00
|
|
|
return _private.pickTemplate(templateList, fallback);
|
|
|
|
};
|
2015-11-24 18:12:50 +03:00
|
|
|
|
2017-11-10 15:44:29 +03:00
|
|
|
_private.getTemplateForError = function getTemplateForError(statusCode) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const templateList = _private.getErrorTemplateHierarchy(statusCode);
|
|
|
|
const fallback = path.resolve(config.get('paths').defaultViews, 'error.hbs');
|
2017-11-10 15:44:29 +03:00
|
|
|
return _private.pickTemplate(templateList, fallback);
|
|
|
|
};
|
|
|
|
|
2019-04-22 00:55:22 +03:00
|
|
|
/**
|
|
|
|
* @description Set template for express. Express will render the template you set here.
|
|
|
|
* @param {Object} req
|
|
|
|
* @param {Object} res
|
|
|
|
* @param {Object} data
|
|
|
|
*/
|
2017-11-10 15:44:29 +03:00
|
|
|
module.exports.setTemplate = function setTemplate(req, res, data) {
|
2017-11-28 15:00:43 +03:00
|
|
|
if (res._template && !req.err) {
|
2017-11-10 15:44:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.err) {
|
|
|
|
res._template = _private.getTemplateForError(res.statusCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
if (['channel', 'collection'].indexOf(res.routerOptions.type) !== -1) {
|
|
|
|
res._template = _private.getTemplateForEntries(res.routerOptions, {
|
2018-06-24 01:32:08 +03:00
|
|
|
path: url.parse(req.url).pathname,
|
|
|
|
page: req.params.page,
|
|
|
|
slugParam: req.params.slug
|
|
|
|
});
|
2018-06-26 02:12:50 +03:00
|
|
|
} else if (res.routerOptions.type === 'custom') {
|
|
|
|
res._template = _private.pickTemplate(res.routerOptions.templates, res.routerOptions.defaultTemplate);
|
|
|
|
} else if (res.routerOptions.type === 'entry') {
|
2018-06-24 01:32:08 +03:00
|
|
|
res._template = _private.getTemplateForEntry(data.post);
|
|
|
|
} else {
|
|
|
|
res._template = 'index';
|
2017-11-10 15:44:29 +03:00
|
|
|
}
|
2015-11-24 18:12:50 +03:00
|
|
|
};
|