Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it slow to use a redirect because it causes an additional request?

In a Rails app sometimes you use a redirect in an action...

redirect_to :controller => 'sessions', :action => 'new'

I wonder if that's bad though because it sends back a 302 status to the browser and then the browser makes a whole new request. It's an additional back-and-forth.

Would it be better to just render a template?

render :template => 'users/new'
like image 945
Ethan Avatar asked Dec 09 '22 21:12

Ethan


2 Answers

The main reason for using redirects instead of renderings is to ensure the idempotent invariant. This basically mean that if you modify something from a POST or DELETE, then you should redirect to the next page. Otherwise, if someone tries to update, they might redo the mutating operation. It also makes it easier for the user since they can always bookmark a specific page. That is not necessarily true if you have used a POST to get to the current place.

But yes, it will be mildly less efficient - although in this case I would care more about usability of the application.

like image 63
Ola Bini Avatar answered May 04 '23 01:05

Ola Bini


In addition, it won't be that much less efficient, because HTTP 1.1 keep-alives should ensure the browser can re-use the same connection to make the second request, rather than having to start again.

like image 23
bobince Avatar answered May 04 '23 00:05

bobince