Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a single page application to update

For single page apps, what methods are there for forcing the page and/or JavaScript files to be reloaded when there have been updates deployed to the server?

One obvious way would be to poll some server resource to see if the current version of the app is running in the browser and if not then load the updated resources.

I'm wondering if there are more generally accepted methods that make use of specific HTTP or DOM features.

update

I'm reading about the HTML5 Appcache. This seems to be geared more towards applications that can run without requiring a server connection. I don't think this could be relevant for updating a SPA's resources (including the page itself) from the server. Am I correct?

like image 469
Ronnie Overby Avatar asked Jul 23 '13 18:07

Ronnie Overby


People also ask

How do I force refresh react app?

If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page. import React from 'react'; function App() { function refreshPage() { window. location. reload(false); } return ( <div> <button onClick={refreshPage}>Click to reload!

How do you know if an application is single page?

If you see the spinner on your browser start loading, and a new page opens it means it's a multi page application. If the content changes instantly without the opening on any new web page, the website is a single-page application.

What is reactive single page application?

The single page application is a web application or website that interacts with the user by dynamically rewriting the current page, rather than loading entire new pages from the server.

What is SPA framework?

An SPA (Single-page application) is a web app implementation that loads only a single web document, and then updates the body content of that single document via JavaScript APIs such as XMLHttpRequest and Fetch when different content is to be shown.


1 Answers

You could use a long polling request to know when there are updates without having to do requests in certain intervals, and when the long polling request returns, you could update the whole page, since old javascript files and events would be bound to DOM elements, so it'd be easier to just reload the page.

EDIT

I read your comment about letting the user continue to use the page, while things are updated, but I think this is very dangerous... would it be too bad to just show a notification to tell the user that there are updates, and recommend that he/she reloads the page?

Otherwise you would have to undo previous scripts, such as unbinding events from DOM elements.

You can unbind events quite easily using jQuery: how to unbind all event using jquery This works for one DOM element, you'd have to do this for each DOM element in the page.

like image 110
Miguel Angelo Avatar answered Oct 27 '22 18:10

Miguel Angelo