When to use "client-side routing" or "server-side routing"?
I'm a little bit confused about this, and I feel slightly stupid asking this question, but I want to understand it.
So, say I'm working with a client side web framework, like Backbone, Angular or Durandal. This framework includes routing.
But I of course still have a server for database stuff, and so on, which also has routing.
My question now is:
When to use "client-side routing" or "server-side routing"?
How is it "decided" whether routing is already performed on the client side or whether the request is first sent to the web server?
I have a particularly hard time imagining this because the client side could do routing before the server ever gets to know about that request.
I'd be very thankful if someone could explain how these two routing systems work together.
P.S.: I have not included code samples because I'm not looking for an answer concerning a particular framework, but concerning the routing process in general.
tl;dr:
- with server-side routing you download an entire new webpage whenever you click on a link,
- with client-side routing the webapp downloads, processes and displays new data for you.
Imagine the user clicking on a simple link: <a href="/hello">Hello!</a>
On a webapp that uses server side routing:
- The browser detects that the user has clicked on an anchor element.
- It makes an HTTP GET request to the URL found in the
href
tag - The server processes the request, and sends a new document (usually HTML) as a response.
- The browser discards the old webpage altogether, and displays the newly downloaded one.
If the webapp uses client side routing:
- The browser detects that the user has clicked on an anchor element, just like before.
- A client side code (usually the routing library) catches this event, detects that the URL is not an external link, and then prevents the browser from making the HTTP GET request.
- The routing library then manually changes the URL displayed in the browser (using the HTML5 history API, or maybe URL hashbangs on older browsers)
- The routing library then changes the state of the client app. For example, it can change the root React/Angular/etc component according to the route rules.
- The app (particularly the MVC library, like React) then processes state changes. It renders the new components, and if necessary, it requests new data from the server. But this time the response isn't necessarily an entire webpage, it may also be "raw" data, in which case the client-side code turns it into HTML elements.
Client-side routing sound more complicated, because it is. But some libraries really make it easy these days.
There are several upsides of client-side routing: you download less data to display new content, you can reuse DOM elements, display loading notifications to user etc. However, webapps that generate the DOM on server side are much easier to crawl (by search engines), thereby making SEO optimization easier. Combining these two approaches is also possible, the excellent Flow Router SSR is a good example for that.
I think client side routing is used by single page applications, where the actual site is never left.
Routing works by attaching to the current page, where client-side routing frameworks react on.
index.html#/mysubpage
Server side routing is similar to what apache does by default when calling a subsite by it´s url, but node.js does that by using routes because the html files need to be rendered first.
If you have a SPA with a client side routing framework, and you are using Node.js, you still need server side routing to switch between sites
Modern applications often use both client-side and server-side routing in a "mixed" or "hybrid" way so it is quite hard to draw a line between these two techniques.
To better understand when and how to use server-side routing and client-side routing, you probably have to figure out what it happens when you have a large application that is used to manage a large manufactoring company (this does NOT happen very often in the real world. It is just a useful example).
In these cases, you will likely have different people (with different roles) who see different parts of this complex environment (different aspects or domains). For example, an engineer would see a file server with a lot of digital documents while the people working in the company canteen would see the menu to be prepared, the working schedule and the store. These are totally different application "domains" that requires totally different UIs so it makes sense to serve different SPAs to each type of user.
In this case, you could use server-side routing to serve a specific UI (SPA) to a specific user while you could use client-side routing to navigate inside this UI (and to load data). Think to these SPAs as "dashboards" or "control panels" devoted to specific "tasks" and used by specific types of "professionals".
For example, you could have a /myapp/engineering route for all of you engineers and a /myapp/canteen for all of your canteen staff. Each of these URLs would represent a specific domain and would serve a specific dashboard to a specific type of user. These URLs would be managed server-side.
Instead, you would use client-side routing to navigate inside each of these dashboard, loading data and re-configuring the UI as needed.
Of course, your app would probably also have a RESTful API used by your SPAs to fetch the data they need. The URLs belonging to the REST API must be managed server-side to perform their job (even if they are NOT associated to real HTML pages) and are invoked only by your SPAs "behind the scenes". They usually are kept in a separated "domain" like /myapp/api .
The same happens with static web page (like the "contacts" page and "about" page) that are usually kept in a /myapp/static folder (or "domain") and managed server-side (this folder or "domain" can be - and often is - hosted on a different server).
So, you should probably use server-side routing to separate applications domains from each other and client-side routing to navigate inside each domain.