如何防止注销时显示404错误?
我有一个关于注销路线的问题。当您在用户注销时查看演示页面时,控制台中会显示 404 错误,因为 CMSPageGuard 尝试从后端获取不存在的注销页面。这是一个小问题,但似乎不是故意的?
更重要的是,如果注销路径不存在,注销守卫中的逻辑会重定向到“home”或“login”。
即使它从未显示过,在后端添加注销页面也不会出现 404 错误的唯一解决方法是什么?
回答
简而言之,这是故意的。它是一项功能的一部分,可让需要可选注销页面的店面轻松地将其添加到 CMS 中,并且 Spartacus 注销逻辑将立即将其取出。
你对引擎盖下发生的事情是正确的。如果您查看doc 中对 LogoutGuard的描述,其整体逻辑描述如下:
负责将用户路由到注销页面(如果可用)或重定向到主页。如果主页受到保护,则会将用户重定向到登录路径。
为了验证注销页面是否可用,它LogoutGuard会发出请求。如果注销页面不可用,该请求将返回 404,这就是浏览器开发工具中显示的内容。
至于防止这些错误显示在日志中,目前没有配置可以关闭此功能。您可能会探索使用自定义LogoutGuard并覆盖该canActivate功能,但我不能 100% 确定这是可能的。
- Indeed, I see there are 2 requests made to the logout page upon logout. One seems to come from LogoutGuard, the other from CMSPAgeGuard. If you manage to override the LogoutGuard so it doesn't attempt to redirect to a `logout` cms page, you could also remove `CMSPageGuard` from the overridden logout route config. I think `CMSPageGuard` might not be used in the scenario where you never want to redirect to a logout cms page. Hopefully, that should eliminate the 404 errors.
- So far, I don't think this is possible. I did some attempts to override the route configuration but I had no success.