Tuesday, 7 August 2018

How to deal with SJR responses that call code the browser doesn't have yet?

I want to know how to mitigate issues that arise when a user is interacting with your site via AJAX, and you deploy a new version of the site at the same time.

Imagine a scenario like the following:


A user is navigating through your posts by clicking a 'Next' button. The 'Next' button submits a form via XHR to the server, which then responds with a Server-generated Javascript Response (SJR).

The SJR renders this Javascript:

// in create.js.erb renderNewPost(<%= @post.to_json %>);

(renderNewPost is defined in your concatenated, minified application.js and is cached by the user's browser.)

Everything is working fine at this point.

Now, as part of a new feature, you change the SJR for the endpoint to first log something:

// in create.js.erb logSomething("hi!"); renderNewPost(<%= @post.to_json %>);

and add the function logSomething to your application.js.

The user clicks the 'Next' button again. This time, the SJR responds with the logSomething('hi') line... but the browser doesn't have logSomething defined anywhere, because the application.js the browser has loaded is the first version.

Now the user won't be able to use your site until the page is refreshed.


How can we solve this problem? Is there a name for the issue at hand?



from How to deal with SJR responses that call code the browser doesn't have yet?

No comments:

Post a Comment