简体   繁体   English

客户端路由的缺点?

[英]Downsides of client side routing?

I am building a universal/isomorphic javascript app (Express/Redux/React). 我正在构建一个通用/同构的JavaScript应用程序(Express / Redux / React)。 I am contemplating routing on the client with React Router and/or routing on the server with Express. 我正在考虑使用React Router在客户端上进行路由和/或使用Express在服务器上进行路由。

I know that client side routing has become popular with single page apps because they make user interaction more seamless. 我知道客户端路由已经成为单页应用程序的流行,因为它们使用户交互更加无缝。

However, I am trying to get a better understanding of the client vs server side routing. 但是,我试图更好地了解客户端与服务器端路由。 What are potential downsides to client side routing that someone may encounter when building any application (single page or not)? 构建任何应用程序(单页或不单页)时,有人可能遇到的客户端路由的潜在缺点是什么? And when is it best to consider routing on the server? 什么时候最好考虑在服务器上路由? Do large scale applications route exclusively on one side (client/server) or do they often blend the two? 大规模应用程序是专门在一侧(客户端/服务器)路由还是经常混合两者?

Thank you! 谢谢!

I see no good reason to stay away from client side routing. 我认为没有充分理由远离客户端路由。 If you're using something like react-router, then this is both client and server routing and there is nothing difficult about it. 如果你正在使用类似反应的路由器,那么这是客户端和服务器的路由并没有什么困难了。 Some specific areas some people might tell you will be difficult: 有些人可能会告诉你的一些特定领域很难:

  • SEO. 搜索引擎优化。 That comes for free, whatever URL you hit will be rendered on the server correctly and sent to the client, so Googlebot will see the page correctly. 这是免费的,您点击的任何网址都会在服务器上正确呈现并发送到客户端,因此Googlebot会正确查看该页面。 There is absolutely no truth to the suggestion that SEO is harder with client side routing, provided that you are server-side redering. 如果你是服务器端的redering,那么对于客户端路由来说SEO更难的建议绝对没有道理。
  • Analytics. Analytics(分析)。 Easy, just put ga('send', 'pageview', path) wherever you're handling navigation on the client side (right before you trigger the router to change path). 简单,只需将ga('send', 'pageview', path)放在客户端处理导航的任何地方(在触发路由器改变路径之前)。
  • Resources, if your whole site is quite big, you don't want to send the entire thing to the client when the page first loads. 资源,如果您的整个网站非常大,您不希望在首次加载页面时将整个内容发送到客户端。 This will require a little more complexity (for example defining multiple entry points with webpack). 这将需要更多的复杂性(例如使用webpack定义多个入口点)。 If you have a site with hundreds of pages then client-side routing is going to give you less benefit anyway. 如果您的网站有数百个页面,那么客户端路由将为您带来更少的好处。

This site (my own) uses client side rendering. 这个站点 (我自己的)使用客户端渲染。 You'll note that it works just fine with JavaScript turned off (the best way to be certain that Google sees it correctly). 你会注意到,它只是罚款关闭JavaScript(可以肯定 ,谷歌正确看到它的最佳方式)。 The source is here if you want to see how any particular piece was done. 如果你想知道任何特定的部分是如何完成的,那么源头就在这里

Some of the downsides of client side routing (can be) that: 客户端路由的一些缺点(可能):

  • While server-side routing is a tried and true technique and have many techniques and library options available, client side will probably be less robust and manageable. 虽然服务器端路由是一种经过验证的技术,并且有许多可用的技术和库选项,但客户端可能不那么健壮且易于管理。

  • Monitoring. 监测。 While server-side routed pages could be verified through any basic web scraper software, client-side routed pages would need to be monitored through a more advanced tool that actually renders HTML and fires client-side scripts. 虽然服务器端路由页面可以通过任何基本的Web刮刀软件进行验证,但客户端路由页面需要通过更高级的工具进行监控,该工具实际呈现HTML并触发客户端脚本。

  • Difficulties with serving SEO content. 服务SEO内容的困难。 Though it's possible, it's a lot more difficult. 虽然这是可能的,但它要困难得多。

  • Resources. 资源。 Depending on how you build your application, server-side routing might be more resource-effective, since there'll be less overhead to load client-side for each page. 根据您构建应用程序的方式,服务器端路由可能更具资源效率,因为为每个页面加载客户端的开销会更少。

  • Compatability. 兼容性。 Based on which browsers you're targeting, your preferred method of client-side routing might not be supported. 根据您定位的浏览器,可能不支持您首选的客户端路由方法。

You can still use client-side routing for applications where the routed pages don't need to or shouldn't be indexed by search engines. 对于路由页面不需要或不应由搜索引擎编制索引的应用程序,您仍然可以使用客户端路由。

For pages that are critical to SEO and don't need to be a SPA (for example they just serve informational content), there's little reason not to go server side. 对于对SEO至关重要并且不需要成为SPA的页面(例如它们只是提供信息内容),没有理由不去服务器端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM