404 rendering at custom route without redirection

(Arjen) #1

Dear Discourse community,

I have an issue I’m trying to resolve, but having little problems with it.
The behaviour I’m trying to achieve with a custom route, is basically the behaviour you see when linking in a topic to another non-existent topic. When clicking a link to a non-existent topic, you are presented with a 404 page. When you click your browser’s Back button, you are redirected back to the topic you came from.

Now for my custom route, I notice different behaviour. So when I’m at my custom route, a JSON is retrieved from the backend, but when this JSON is not present, the controller returns a 404 page. EmberJS picks up on this, and redirects to www.my-discourse-server.com/404. So far so good. But if I hit the back button, I go back to the non-existent page, which returns a 404 and I once again find myself at the /404 page.

Let me try to give a short indication of what the code looks like. My route:

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

When this returns a 404, it is caught by Discourse and transitioned to ‘exception-unknown’ by /discourse/app/assets/javascripts/discourse/routes/application.js.es6.

So basically what I want is to present the user with a 404 page, without the URL redirecting to /404.

Any suggestions?


(Bas van Leeuwen) #2

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

(Joe Buhlig) #3

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:

(Arjen) #4

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.


(Joe Buhlig) #5

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.

(Arjen) #6

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?