Index.js #
Here is documentation about the main js file on server - index.js. And here you can find a list of all functions from this file:
- Import variables
- /generate-pages
- Caching.
- Image sources preparing.
- Sitemap generation.
- Redirects.
- Regeneration.
Import variables #
Firstly get data from config.js
| Variable | Description |
|---|---|
| PATH_TO_WEBSITES_FOLDER | Path to folder with websites. Get from .env |
| SERVER_PORT | Port of your server. Get from .env. By default - 7777 |
| MODE | Mode of this session. Get from .env. Can be development or production. By default development |
| HOST | Host of your website. Its your domain. If you develop locally you should to write in .env your ip address. Get from .env. By default 127.0.0.1 |
| LOCAL_WEBSITE | Bollean variable. Set autonatically from values in .env |
| PATH_TO_CACHE_FOLDER | Path to folder with cache of your website or websites |
/generate-pages #
It's a post request. Using for multi-regeneration of pages.
This route call regenerate method from WebsitesManager class with arguments:
| Argument | Description | Example |
|---|---|---|
| website | Domain of your website (without protocol) | atlasiko.com |
| pages | Here you write part of slug, which must contains pages for regenerate | /news/ - for regenerate all pages with /news/ in slug; / - for regenerate all pages |
/version #
It's a get request. Using for getting version of server.
/sitemap.xml #
It's a get request. Using for creating sitemaps.
If config.mjs exist in folder with your site call generateIndexSitemap method from SitemapGenerator, else call generateSimpleSitemap method from SitemapGenerator. In both cases this request call mathods with arguments: req and res , but if config.mjs exist in folder with your site it's mean that config.mjs contains object with name chapters and every chapter it's a name for your sitemaps.
/:type-sitemap.xml #
It's a get request. Using for creating certain type of sitemap.
This request calling from method generateIndexSitemap in class SitemapGenerator, where :type it's a variable or name of certain sitemap, this name is name of appropriate chapter.
/feed/google-news/:type #
It's a get request. Using for generation and sending news to Google News. This request server recieve from Google
* #
It's a get request. Using for routing all pages, images, assets etc.
This a largest part of index.js, includes:
- Seting data from
config.mjs - Redirects
- Prepare for generation
- Finding template or asset
- Generate
Seting data from config.mjs
#
First at all we need to write root path to project's folder. Then using this path chacking if config.mjs is exist.
If config.mjs exist - get from config next data:
| Variable | Description |
|---|---|
chapters |
Chapters - it's an object where you write subobject with different types, from different application in GudHub (pages, blog etc). This subobjects includes data with id (app_id, slug_field_id, json_field_id etc.) and sitemap (if you want), in sitemap we need to set default values for frequency, priority and sitemapName, also you can add some exclusion, to do this you need add an array cases of objects with properties case(slug or regexp), priority and frequency in every object. Use this for set specific data for pages. At the end we have function filter in this function we set filter to get pages with some value (for example check if field's value is true). |
routes |
Routes - it's an array with objects. Every object has two properties: route - slug and index path to template of page by this slug. In route you can use variables: route: '/blog/page/:page/'. |
redirects |
Redirects - it's an array with objects. Every object has two properties: from and to this a slugs from which page to which page we need redirect. |
auth_key |
Auth_key it's a security key of your account in GudHub. This must be auth_key from acount where all applications with data for website. |
buildFolder |
Path from source project to folder where build version. By default dist. |
Examples #
Chapters
export const chapters = {
pages: {
app_id: 33333,
slug_field_id: 777777,
sitemap: {
frequency: 'weekly',
priority: 0.8,
sitemapName: 'pages',
cases: [
{
case: '/',
priority: 1,
frequency: 'weekly'
},
{
case: '/blog/',
priority: 0.9,
frequency: 'daily'
},
],
filter: (items) => {
return items.filter(item => {
const field = item.fields.find(field => field.field_id == 777777);
if(field) {
return field.field_value == 1;
}
return false;
});
}
}
},
blog: {
app_id: 34444,
slug_field_id: 799999,
sitemap: {
frequency: 'weekly',
priority: 0.6,
cases: [
{
case: /^\/blog\/authors\/[^\/]*\/$/,
sitemapName: 'authors',
frequency: 'weekly',
priority: 0.6
},
],
filter: (items) => {
return items.filter(item => {
const field = item.fields.find(field => field.field_id == 799999);
if(field) {
return field.field_value == 1;
}
return false;
});
}
}
}
}
Routes
export const routes = [
{
route: '/blog/',
index: '/blog/blog.html'
},
{
route: '/blog/page/:page/',
index: '/blog/blog.html'
},
{
route: '/blog/authors/:author/page/:page/',
index: '/blog/author.html'
}
]
Redirects
export const redirects = [
{
from: '/service/it_consulting',
to: '/services/it-consulting-services/'
},
{
from: '/news/page/',
to: '/news/'
},
{
from: '/our_team/team',
to: '/about-us/'
}
]
Redirects #
In this part we call checkRedirects method from redirectsHandler class with original url like a argument. There is compare original with array of objects. If find original url in this array - redirect.
After redirect step check if url has a last symbol - '/', if it's false - add '/' to the end of url.
Prepare for generation #
In this step need to set values of variables from searchParams of original url.
| Variable | Description | Values |
|---|---|---|
| mode | Mode of session can be if MODE (from .env) is development return ssr |
ssr / ssg |
| disableImagesRegeneration | This option to set if images must be regenerated. Set false to improve speed of ssr |
true / false |
| disableBundling | This option to set if css and js bundles must be regenerated. Set false to improve speed of ssr |
true / false |
| preview | Set true to generate page without saving to cache. Useful for example to preview articles | true / false |
Finding template or asset #
In this step check if url to page and hasn't paramin searchParams mode=ssr - get page from cache and response it if page exist.
If page don't exist or it is not a page trying to find template by this url using findSlugTemplate method from routeHandler class with arguments: routes (from config), url (requested url) and buildFolder (from config). This method return object with properties:
| Property | Description |
|---|---|
| found | It's like a status, if template by this route was found - true, else - false. |
| paramsObject | It's an object of urls params (routeObject, path, author (if it's an author page)) |
| queryParams | It's a stringified paramsObject |
Example if not found
{
found: false,
paramsObject: {},
queryParams: '?'
}
Example if found
{
route: '/blog/authors/:author/',
index: '/blog/author.html',
found: true,
paramsObject: {
routeObject: '{"route":"/blog/authors/:author/","index":"/blog/author.html"}',
author: 'author-name',
path: '/blog/authors/author-name/'
},
queryParams: '?routeObject={"route":"/blog/authors/:author/","index":"/blog/author.html"}&author=author-name&path=/blog/authors/author-name/'
}
If this method return found: false (can return false if page don't exist or finded url it's path to asset) call findStaticFile method from routeHandler class, which check if this is path to static file (image, js/css file) and if it is return object with two properties: type and path, else trying to return on client cached page or if this page doesn't exist in cache - return 404 Page.
If findStaticFile return object with properties check type value:
If
type === assets- check is it image. If image - remove header from response to fix bug with CORS and return file, else - just return fileIf
type === html- update data in object which was returned fromfindSlugTemplatemethod before, by seting data in empty propertiesIf
type === error- return 404
Generate #
If url contain mode=ssr in searchParams started this part of code. Call generate method from SSR class with arguments (root, website, route, chapters, auth_key, buildFolder, disableImagesRegeneration, preview, disableBundling)
| Argument | Description |
|---|---|
root |
This is path to folder where is your website |
website |
This is host |
route |
This is object which return findSlugTemplate method |
chapters |
This is chapters from config |
auth_key |
Auth_key it's a security key of your account in GudHub. This must be auth_key from acount where all applications with data for website. |
buildFolder |
Path from source project to folder where build version. By default dist. This option to set if images must be regenerated. Set false to improve speed of ssr |
disableBundling |
This option to set if css and js bundles must be regenerated. Set false to improve speed of ssr |
preview |
Set true to generate page without saving to cache. Useful for example to preview articles |