1. Do I have to generate slug once and store it in a PostModel scheme or generate on every posted post?
Both methods are valid and have pros:
- Database: faster because we don’t need to generate it every time we need it. Slimes are generated only once.
- On the fly: we don’t need to restore all the information about the table or database if you decide to change your template / algorithm (which should be avoided anyway). Less space used in the database and less data transferred between your database and your application. You should not drag out too long unless your algorithm for creating a bullet does not work, but in this case the generation time should not be a problem.
In both cases, you will need to select a template and determine the algorithm for generating your slug that matches the template you selected.
I personally almost always prefer to store slug in a database that allows you to specify a pool for a specific record. You may never need it, but if it comes, you are ready. For example, if for a specific record the generated pool is awesome-post , and you want it to be best-awesome-post , you can easily do this if the pool is stored in a database, otherwise you will have to configure your own algorithm for each " special "case, which will become a nightmare with such cases.
Another point that I think is in favor of preserving it: as soon as you post, the slime is part of a permanent link to this post, and it should be considered unchanged. I am not a big fan of generating several times immutable data if I can avoid this in this case.
2. How to generate a header-based slug (which existing node modules allow this task) for non-ASCII characters?
As you said, there are several node modules for generating the slugs database in one or more fields, such as a header, some of them are even integrated with MongoDB / Mongoose, for example mongoose-url-slugs .
In most slugs, accented characters will be converted to their non-addressed copies, everything will be converted to lowercase, punctuation removed, the place replaced with - , for example, etc.
Regarding the ASCII part of your question, please see the mongoose-url-slugs code, for example, when creating a bullet, they call the removeDiacritics function, which will remove these special characters and replace them with the equivalent to slug.
One example that I can think of requires special treatment in order to correctly process this word "road" in German: "Straße".
The function identifies the Eszett character ( \u00DF ) and replaces it with the letter '.'.
If you want to take a step forward, you should use the slug module that processes unicode and utf-8, such as slug , for example, which conforms to RFC 3986 with respect to Uniform Resource Identifier (URI).
It converts a title like i ♥ my title to i-love-my-title , etc.
3. What place should I use to redirect requests from http://www.example.local/posts/571f78d077b4454bafcfcced to http://www.example.local/posts/571f78d077b4454bafcfcced/how-to-make-and-store-slug-for-title (nodejs, nginx, client side).
If you save the pool in your database for the reasons stated above, the pool should be generated only once, and then stored in the database. Currently, regeneration no longer occurs on the server side or on the client side.
When displaying links on the client side, you will always safely use the previously created slug, for example, http://www.example.local/posts/571f78d077b4454bafcfcced/how-to-make-and-store-slug-for-title , to display the link, corresponding to the desired template.
In the case of a client using a URL without a slug or partial slug, e.g. http://www.example.local/posts/571f78d077b4454bafcfcced/how-to-make , to redirect to the correct URL with a full pool, Stack Overflow question in a good example, they just send a 301 redirect to the correct URL.

They deal with these special cases on the server, as it should be, since your application on the server is the only one (if you save the pool in the database) that has authority on this issue. Your application knows the correct pool for a particular entry, like in a database, so if slug is not specified or only partially, which is easy to find in your application, you can safely initiate a 301 redirect to the correct URL with the correct slug, for example http://www.example.local/posts/571f78d077b4454bafcfcced/how-to-make-and-store-slug-for-title .
You should handle these cases in your node application (I assume that you are using node, as you mentioned in the question), and redirect to the correct URL if necessary.
For instance:
res.writeHead(301, { "Location": `http://www.example.local/posts/${postId}/${postSlug}` });
Because similar content is available through multiple URLs, you should also use the canonical link element to specify the “canonical” URL that search engines should use, for example, to avoid duplicating content problems.
<link rel="canonical" href="http://www.example.local/posts/571f78d077b4454bafcfcced/how-to-make-and-store-slug-for-title">
Regarding your change about the Stack Exchange Data Explorer , I think they omit the field from the results because it is not that important. According to a comment by Nick Craver, software developer and system administrator for Stack Exchange , they really check if the abandoned header matches that they have in the database, a request, and if not, they are redirected.
Change for russian characters in urls:
If you want to keep Russian characters, for example, no problem, if you are not behind utf-8, for example. In your example links, Russian characters are displayed, but behind the scene the URL is “encoded in percent” or “encoded in url”, you can check it yourself by right-clicking on the link in your browser, selecting “Inspect”, and you will see that the url is actually something like http://ru.stackoverflow.com/questions/456697/genymotion-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0-%D0%BF%D1%80%D0%B8-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B8-%D0%B2%D0%B8%D1%80%D1%82%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B3%D0%BE-%D1%83%D1%81%D1%82%D1%80%D0%BE%D0%B9%D1%81%D1%82%D0%B2%D0%B0 . Your browser knows that it is encoded in url and displays it correctly with Russian characters.
You have, of course, Node.js modules or even your own Javascript methods for URL-coding any URLs you want.
If you are interested in what SEO and search engines are, Google, for example: “we, as a rule, do not lag behind UTF-8 encoded URLs and usually show them to users in our search results (but refer to your server using URL addresses are properly shielded), so there is no problem.
Most slugifier modules remove these characters, so if you really want to keep them, you will need to use something more specific, like arSlugify :
var ars = require('arslugify'); var title = 'genymotion '; var slug = ars(title); var url = 'www.example.local/posts/571f78d077b4454bafcfcced/' + slug; var encodedUrl = encodeURIComponent(url); console.log(url); // www.example.local/posts/571f78d077b4454bafcfcced/genymotion----- console.log(encodedUrl); // www.example.local%2Fposts%2F571f78d077b4454bafcfcced%2Fgenymotion-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0-%D0%BF%D1%80%D0%B8-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B8-%D0%B2%D0%B8%D1%80%D1%82%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B3%D0%BE-%D1%83%D1%81%D1%82%D1%80%D0%BE%D0%B9%D1%81%D1%82%D0%B2%D0%B0