miguel-perez/smoothState.js

Doesn't honor redirects

Open

#147 opened on Mar 28, 2015

View on GitHub
 (5 comments) (0 reactions) (0 assignees)CSS (4,433 stars) (515 forks)batch import
bughelp wanted

Description

I'm using node.js / Express on the server, but I assume any backend stack would yield similar results.
After I preform a save, or update on my website, I redirect users to the next page, this is standard so that if the user hits the back button it doesn't perform the action twice. The issue is that smoothState doesn't honor these redirects and stays on the current url.

So for example, from the /widgets/list page I click the the delete widget button which takes me too the url /widgets/delete/123, the backend does it's thing, deletes the user and then sends a redirect response back to the frontend to go to the url /list/widgets, but smoothState doesn't update the url. This can lead to weird behavior and actions being performed twice. For instance in my application if the user hit refresh while on the /widgets/delete/123 url (and the widget had already been deleted) they would get a 404, because the widget can no longer be found.

I believe you can check for redirects via the jQuery $.ajax done method with something like

$.ajax({
    type: "POST",
    url: reqUrl,
    data: reqBody,
    dataType: "json",
    success: function(data, textStatus) {
        if (data.redirect) {
            // data.redirect contains the string URL to redirect to
            window.location.href = data.redirect;
        }
        else {
            ...
        }
    }
});

example from this url: http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call

Contributor guide