ApostropheCMS – 如何在中间件中提供不同的页面

huangapple go评论52阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月27日 20:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定