treefrogframework/treefrog-framework

tflash() object not always visible on redirect page

Closed this issue · 3 comments

In the code below the BlogController::show() action will not show the "Created successfully." message when redirected from BlogController::create(), if the ajax request /blog/getStatus arrives before /blog/show.

create.erb:

// Add this in HTML body: 

<p id="status"></p>

<script type="text/javascript">
function updateStatus() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById('status').innerHTML = xhttp.responseText;
        }
    };
    xhttp.open('GET', '/blog/getStatus');
    xhttp.send();
}
setInterval(updateStatus, 2000);
</script>

blogcontroller.cpp:

void BlogController::show(const QString &id)
{
    auto blog = Blog::get(id.toInt());
    texport(blog);
    render();
}

void BlogController::create() {
    // ...
    case Tf::Post: {
        // ...
       QString notice = "Created successfully.";
        tflash(notice);
        redirect(urla("show", model.id()));   // Wait at breakpoint for 10 seconds here, then run
     }
}

void BlogController::getStatus()
{
    renderText("OK");
}

In real world example this almost newer happen, but it can be observed when debugging if you wait at breakpoint in BlogController::create() for 10 seconds, to let the browser send some /blog/getStatus requests.

I have tried it. However, it doesn't reproduce.
After created a entry, it showed "Created successfully."

This happens very rarely if the requests come to server in the following order (check the access.log file):

/blog/create - stores notice in the session and redirects to /blog/show
/blog/getStatus - unexpected AJAX request, here the notice is "Created successfully."
/blog/show - the notice doesn't exist here, the flash object was consumed by AJAX request

The flash object is not related in any way to the redirect url.

It is a specification.