Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Page Application Routing

Modern single page applications use routing mechanisms which don't have to rely on fragments or additional url parameters, but simply leverage the url path. How does the browser know when to ask the server for a resource and when to ask the single page application for a spa-page controlled by a router? Is there a browser API which makes it possible to take over the control of url handing which is then taken over by e.g. the vue-router or another routing spa library?

like image 321
paweloque Avatar asked Sep 30 '18 13:09

paweloque


People also ask

Do single page applications have routes?

Single Page ApplicationsA SPA can have multiple routes and URLs, but if you watch closely, you will notice that the browser does not do a full refresh (no loading icon on the tab) when navigating between routes.

WHAT IS routing in the context of a single-page application?

Routers are typically used to power route matching within a single-page application. Route matching is done when the location changes, determines which route matches the new location, and then triggers a re-render of the application. In-app navigation is performed using the History API.

How does a SPA use routing?

A critical feature in a SPA is navigation between "pages" within the application. Of course they are not real pages, since it's a Single Page Application, but from the user point of view it looks like that. A typical example of a SPA is Gmail where the URL in the browser reflects the state of the application.

What is single-page application example?

You'll easily recognize some popular examples of single page applications like Gmail, Google Maps, Airbnb, Netflix, Pinterest, Paypal, and many more. Companies all over the internet are using SPAs to build a fluid, scalable experience.


2 Answers

How does the browser know when to ask the server for a resource and when to ask the single page application for a spa-page controlled by a router?

SPA Frameworks use routing libraries.

Suppose your javascript app is already loaded in the browser. When you navigate to a route that is defined in your routes array, the library prevents an http call to the server and handles it internally in your javascript code. Otherwise the call is forwarded to the server as a GET Http request.

here is an answer that discribes this behaviour with a clear scenario

like image 123
mehdi Ichkarrane Avatar answered Sep 17 '22 15:09

mehdi Ichkarrane


In Vue Router (and I assume other libraries/frameworks are the same) this is achieved through the HTML5 history API (pushState(), replaceState(), and popstate) which allows you to manipulate the browser's history but won't cause the browser to reload the page or look for a resource, keeping the UI in sync with the URL.

For example, observe what happens to the address bar when you enter this command in your browser's console

history.pushState({urlPath:'/some/page/on/stackoverflow'},"",'/some/page/on/stackoverflow')

The new URL is even added to your browser's history so if you navigate away from the page and come back to it you'll be directed to the new URL.

Of course all these URLs are non-existent on the server. So to avoid the problem of 404 errors when a user tries to directly access a non-existent resource you'd have to add a fallback route that redirects to your index.html page where your app lives.

Vue Router's HTML5 History Mode

React Router's <BrowserRouter>

like image 8
Husam Ibrahim Avatar answered Oct 10 '22 09:10

Husam Ibrahim