自定义路由未重定向时渲染 404

亲爱的 Discourse 社区:

我遇到了一个问题,正试图解决,但进展不大。

我希望通过自定义路由实现的行为,类似于链接到另一个不存在的主题时的表现:点击指向不存在主题的链接时,会显示 404 页面;点击浏览器的“返回”按钮后,会重定向回你之前所在的主题页面。

然而,对于我的自定义路由,我观察到了不同的行为。当我访问自定义路由时,系统会从后端获取 JSON 数据;如果该 JSON 不存在,控制器会返回 404 页面。EmberJS 检测到这一点后,会重定向到 www.my-discourse-server.com/404。到目前为止一切正常。但如果我点击“返回”按钮,我会回到那个不存在的页面,该页面再次返回 404,结果我又被带回了 /404 页面。

让我简要说明一下代码的大致样子。我的路由如下:

export default Discourse.Route.extend({
  model(opts, router, model) {
    return Ember.$.getJSON(`/custom-route/${opts.uniqueID}`);
  }
})

当该请求返回 404 时,Discourse 会捕获它,并通过 /discourse/app/assets/javascripts/discourse/routes/application.js.es6 将其转换到 exception-unknown 状态。

因此,我想要的效果是:向用户展示 404 页面,但 URL 不要重定向到 /404。

有任何建议吗?

谢谢!

2 个赞

@joebuhlig is this something that you encountered when developing the Static Pages plugin?

Not exactly. The Static Pages plugin doesn’t try to maintain the URL without redirecting. It’s all done on the server side as seen here:

https://github.com/procourse/procourse-static-pages/blob/master/app/controllers/dl_static_pages/pages_controller.rb

1 个赞

Hey @joebuhlig,

I actually created PRs for the procourse-static-pages plugin recently. Glad you removed the Licensing, since I think more people can benefit from this plugin.

When I make a post, linking to a non-existing procourse-static-page, I see the same behaviour as I described. So when you click the link in the post, Discourse opens that URL and receives a 404 from the controller (as intended). However, the frontend then redirects you to /404. Meaning that if you go back, you again hit the non-existing route which again redirects you to the /404.

We’re still dealing with this problem, and I think your plugin can also benefit from a solution.

Arjen

1 个赞

The Static Pages plugin simply follows the core Discourse process. I try to stick to the “the Discourse way” as much as I can.

Since this seems to be the way it works in core, you would need to write an acceptable PR for the team. But I can’t speak to whether or not they would accept it. There may be a valid reason for redirecting to a 404 page as opposed to leaving the URL.

I agree that it seems to be “the Discourse way”. However, when I link to a non-existing topic/post, Discourse does not redirect to a 404 page, but simply renders one without redirecting. So there seem to be multiple ways of handling this.

Any chance @codinghorror can weigh in on this?

1 个赞