404 renderizado en ruta personalizada sin redirección

Estimada comunidad de Discourse,

Tengo un problema que estoy tratando de resolver, pero estoy teniendo algunas dificultades con ello.

El comportamiento que intento lograr con una ruta personalizada es básicamente el que se observa al enlazar un tema a otro tema que no existe. Al hacer clic en un enlace a un tema inexistente, se muestra una página 404. Cuando haces clic en el botón Atrás del navegador, te redirige de vuelta al tema desde el que venías.

Ahora, para mi ruta personalizada, noto un comportamiento diferente. Cuando estoy en mi ruta personalizada, se recupera un JSON desde el backend, pero cuando este JSON no está presente, el controlador devuelve una página 404. EmberJS lo detecta y redirige a www.my-discourse-server.com/404. Hasta aquí todo bien. Pero si presiono el botón Atrás, vuelvo a la página inexistente, que devuelve un 404 y una vez más me encuentro en la página /404.

Déjame intentar dar una breve indicación de cómo se ve el código. Mi ruta:

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

Cuando esto devuelve un 404, es capturado por Discourse y se transiciona a ‘exception-unknown’ por /discourse/app/assets/javascripts/discourse/routes/application.js.es6.

Básicamente, lo que quiero es presentar al usuario una página 404, sin que la URL se redirija a /404.

¿Alguna sugerencia?

¡Gracias!

2 Me gusta

@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 me gusta

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 me gusta

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 me gusta