カスタムルートでのリダイレクトなしの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' へ遷移します。

つまり、URL が /404 にリダイレクトされることなく、ユーザーに 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