单页面应用程序的版本控制和路由?
背景
我正在开发一个教育性 JavaScript 应用程序/网站 (SPA),最终将拥有 1000 个我希望可抓取的动态 URL。
我现在正在研究如何实现版本控制、路由和 seo(以及 i18n)。
我的总体想法是使用 hashbangs 并拥有如下资源:
example.com/#!/v1?page=story1&country=denmark&year=1950
这里的“page”参数决定需要加载哪些控制器/视图,后续参数可以指示控制器加载相应的内容。
然后,只需替换 url 的“v1”部分即可处理参数的版本控制,并且有一个特定的路由处理程序来映射每个版本的已弃用参数。
通过让 Node.js 或其他后端提供内容的“转义片段”版本,可以改进 SEO。
i18n 也应该由 node.js 处理吗?这样,传递给爬虫的内容就已经被翻译了吗?
- 这是使单页应用程序版本化且可爬网的可行方法吗?
- 我现在使用 Backbone.js,您会添加什么来帮助解决上述问题?
Background
I'm working on a an educational JavaScript application/site (SPA) that will eventually have 1000s of dynamic urls that I'd like to make crawlable.
I'm now researching how to implement versioning, routing and seo (and i18n).
My general idea is to use hashbangs and have resources like:
example.com/#!/v1?page=story1&country=denmark&year=1950
The "page" parameter here decides which controllers/views that need to be loaded and the subsequent parameters could instruct controllers to load corresponding content.
Versioning of parameters could then be handled by just replacing the "v1" part of the url - and have a specific route handler mapping deprecated parameters for each version.
SEO would be improved by having node.js or other backend delivering an "escaped fragment" version of the content.
i18n should probably be handled by node.js as well? This way, what gets delivered to the crawler is already translated?
- Is this a viable approach to making a Single-page-application versioned and crawlable?
- I'm using Backbone.js now, what would you add to the mix to help out with the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)天哪,不。 (好吧,它可以工作,但是使用 hashbangs 从头开始设计应用程序是一个坏主意)
2)node.js 和backbone 是一个很好的组合。就我个人而言,我喜欢在服务器上使用 Express 进行路由/模板化。
--反对 hashbangs 的论点:网络上有很多好的信息,我会尊重它们。
此处:http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs
此处: http://www.pixelflips.com/blog/do-clean-urls-still-matter/
和这个精彩的库:https://github.com/browserstate/History.js/
以及该库中的此 wiki 页面:https://github.com/browserstate/history.js/wiki/Intelligent-State -Handling
看看这个 chrome 扩展,它将使用该库 ajax StackOverflow(或具有正常 url 的任何其他网站): https://chrome.google.com/webstore/detail/oikegcanmmpmcmbkdopcfdlbiepmcebg
15 个参数是绝对必要的吗?将内容参数(页面、国家/地区)放在 url 中,将表示参数(即:sortby=author)放在查询字符串中。
为了回应“你仍然被哈希标签序列化所困扰”,我给出了这样的观点:
每条路由都应该指向一个有效的资源位置。即: /v1/page/denmark/some-slug-for-post 应该是一个资源位置,当您将其更改为新的帖子/页面时,它也应该是一个资源位置。我的意思是,如果您无法使用 url 为页面添加书签,那么实现就会被破坏。
另外,您是否打算断开每个版本的所有链接?我不确定您为什么要在网址中包含版本。
我希望这有帮助。
1) Hell no. (well it could work, but designing your application from the ground up with hashbangs is a bad idea)
2) node.js and backbone are a good combination. Personally I like express for routing/templating on the server.
--The argument against hashbangs:There is so much good information on the web that I will defer to them.
here: http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs
here: http://www.pixelflips.com/blog/do-clean-urls-still-matter/
and this wonderful library: https://github.com/browserstate/History.js/
and this wiki page from that library: https://github.com/browserstate/history.js/wiki/Intelligent-State-Handling
Than check out this chrome extension that will ajax StackOverflow(or any other site with normal urls) using that library: https://chrome.google.com/webstore/detail/oikegcanmmpmcmbkdopcfdlbiepmcebg
Are 15 parameters absolutely necessary? Put the content parameters(page, country) in the url and the presentational (ie: sortby=author) in the query string.
in response to "You are still stuck with hash tag serialization" I give this:
every route should point to a valid resource location. ie: /v1/page/denmark/some-slug-for-post should be a resource location and when you change it to a new post/page, that should be a resource location too. What I'm saying is that if you can't use the url to bookmark the page, than the implementation is broken.
Also, are you planning on breaking all your links with every version? I'm not sure why you're including the version in the url.
I hope this helps.
对于第 1 点,要求是所有“页面”都有唯一的 URL,并且无需 JavaScript 即可找到和查看。
你必须创建一个 robots.txt 来列出你所有的唯一 URL,或者在某个地方有一个站点地图,以便爬虫可以找到所有 URL
我不确定你在这种情况下所说的 SEO 到底是什么意思。您似乎建议您为爬虫提供与浏览器不同的内容。通常这不是一个好主意,除非您的网站非常动态,没有其他办法。
In answer to number 1, the requirement is that all "pages" have a unique URL and can be found and viewed without JavaScript.
You MUST make a robots.txt that lists all your unique URLs or have a site map somewhere so the crawlers can find all URLs
I'm not sure exactly what yo mean by SEO in this context. It seems like you are suggesting you will give different content to the crawlers than to the browsers. Typically not a great idea unless your site is so dynamic there is no other way.