更新时间:2022-12-25 13:29:01
tl;dr:
想象一下用户点击了一个简单的链接:Hello!</a>
在使用服务器端路由的网络应用上:
href
标签中的 URL 发出 HTTP GET 请求如果网络应用使用客户端路由:
客户端路由听起来更复杂,因为它是.但是现在有些图书馆真的让这件事变得很容易.
客户端路由有几个好处:你下载更少的数据来显示新内容,你可以重用 DOM 元素,向用户显示加载通知等.但是,在服务器端生成 DOM 的 web 应用程序更容易抓取(通过搜索引擎),从而使 SEO 优化更容易.结合这两种方法也是可能的,优秀的 Flow Router s-s-r 就是一个很好的例子.>
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:
Imagine the user clicking on a simple link: <a href="/hello">Hello!</a>
On a webapp that uses server side routing:
href
tagIf the webapp uses client side routing:
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 s-s-r is a good example for that.