Erreur 404 lors du rendu sur une route personnalisée sans redirection

Chère communauté Discourse,

Je rencontre un problème que j’essaie de résoudre, mais je bute sur quelques difficultés. Le comportement que je souhaite obtenir avec une route personnalisée est essentiellement celui que vous observez lorsque vous cliquez sur un lien vers un sujet inexistant. En cliquant sur un lien vers un sujet inexistant, vous êtes redirigé vers une page 404. Lorsque vous cliquez sur le bouton « Retour » de votre navigateur, vous êtes renvoyé vers le sujet d’où vous veniez.

Or, pour ma route personnalisée, je constate un comportement différent. Ainsi, lorsque je me trouve sur ma route personnalisée, un JSON est récupéré depuis le backend, mais si ce JSON n’existe pas, le contrôleur renvoie une page 404. EmberJS intercepte cela et redirige vers www.my-discourse-server.com/404. Jusqu’ici, tout va bien. Mais si j’appuie sur le bouton « Retour », je reviens à la page inexistante, qui renvoie une erreur 404, et je me retrouve une nouvelle fois sur la page /404.

Laissez-moi essayer de donner une brève indication de l’apparence du code. Ma route :

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

Lorsque cela renvoie une erreur 404, elle est interceptée par Discourse et la transition se fait vers ‘exception-unknown’ via /discourse/app/assets/javascripts/discourse/routes/application.js.es6.

Donc, ce que je veux fondamentalement, c’est présenter à l’utilisateur une page 404 sans que l’URL ne soit redirigée vers /404.

Des suggestions ?

Merci !

2 « J'aime »

@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 « J'aime »

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 « J'aime »

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 « J'aime »