英文:
ApostropheCMS - how to serve different page in middleware
问题
I have ApostropheCMS v3 project.
I want to add some middleware inside modules/@apostrophecms/page/index.js.
I believe it should look like this:
module.exports = {
...
handlers(self, options) {
return {
"@apostrophecms/page:serve": {
handleCustomLocales(req) {
try {
// code will go here
} catch (_err) {}
},
Let's say that the user opened "/en/contact" page. The contact page from EN locale was served by default.
I want to catch a few exceptions. If the user opened "/en-cz/contact" ("/en-**/contact") page - I want to still serve the contact page from the EN locale. For now, it serves a 404 page.
I don't want to change the URL or redirect the user to "/en/contact". How can I do it?
英文:
I have ApostropheCMS v3 project.
I want to add some middleware inside modules/@apostrophecms/page/index.js.
I believe it sould look like this:
module.exports = {
...
handlers(self, options) {
return {
"@apostrophecms/page:serve": {
handleCustomLocales(req) {
try {
// code will go here
} catch (_err) {}
},
Let's say that the user opened "/en/contact" page. The contact page from EN locale was served by default.
I want to catch a few exceptions. If user opened "/en-cz/contact" ("/en-**/contact") page - I want to still serve contact page from EN locale. For now, it serves 404 page.
I don't want to change the url or redirect user to "/en/contact". How can I do it?
答案1
得分: 1
首先,只是一个警告:在没有使用规范标签的情况下为两个不同的URL提供相同的内容可能会影响您的SEO。如果您不能使用重定向(我建议使用),考虑在这种情况下输出规范链接标签。
好的,说了这些:
要添加真正的Express中间件,这是您在这里需要的,请在任何模块中使用Apostrophe的middleware
功能,就像这样:
module.exports = {
...
middleware(self, options) {
return {
altLocales: {
before: '@apostrophecms/i18n',
middleware(req, res, next) {
req.path = req.path.replace('/en-cz', '/en');
req.url = req.url.replace('/en-cz', '/en');
return next();
}
}
};
}
};
before
属性将此中间件安排在@apostrophecms/i18n
模块安装的中间件之前,这确保在您的中间件有机会运行之前不会决定req.locale
。
当然,您可以在此处使用options
根据配置做出决策,并更加小心确保/en-cz
仅位于路径的开头。这是一个演示该技术的简单示例。
英文:
First, just a warning: serving identical content for two different URLs without a canonical tag can affect your SEO. If you can't use a redirect (which I would recommend), consider outputting a canonical link tag in this situation.
OK, that being said:
To add true Express middleware, which is what you need here, you want to use Apostrophe's middleware
feature in any module, like this:
module.exports = {
...
middleware(self, options) {
return {
altLocales: {
before: '@apostrophecms/i18n',
middleware(req, res, next) {
req.path = req.path.replace('/en-cz', '/en');
req.url = req.url.replace('/en-cz', '/en');
return next();
}
}
};
}
};
The before
property schedules this middleware before the middleware installed by the @apostrophecms/i18n
module, which ensures that req.locale
is not decided before your middleware has a chance to run.
Of course you can use options
here to make decisions about what to do based on configuration, and be more careful about ensuring /en-cz
is only at the start of the path. This is a simple example demonstrating the technique.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论