How do I use Backbone.js for a multi-page web app?

时间秒杀一切 提交于 2019-12-10 13:09:45

问题


I've always used Backbone's routes (#!/blah)

But I realize that in order to get indexed by Google, you need to provide a non-javascript version of the site.

I don't like to write code twice. So, I would rather build a multi-page app instead of a single page web app.

Backbone.js provides me with the "structure" so my javascript does not turn into sphagetti mess. But , I'm completely clueless on how to build multi-page apps using Backbone.

Do you include the main.js file on every page render? What about routes? How do you deal with that? How do you deal with modularizing, etc?

I wish there was a tutorial on how to build multi-page javascript apps using Backbone.


回答1:


There are a lot of considerations for doing something like this, but here are two keys one:

Serving the page

You probably want to have your web server route everything to the serve the same static page (assuming it is static assets). This means that everything in http://yourdomain.com/* will serve /var/www/yourdomain.com/index.html. Once the static page is loaded, the JS on that page will decide what to do given the url.

Push State

To do your routing, you can still use backbone routing, but don't use hashbangs (the #!/blah style urls). See http://backbonejs.org/#History for instance. This will allow you to navigate to real URLs without actually needing a page refresh. If the browser doesn't support a pushState, everything will still work, but it will reload the page.




回答2:


The answer by @Jamie Wong gets you part of the way. By using pushState instead hash-based URL's Google should treat each route as a different page. However since I'm assuming the content for each route is generated dynamically it's unclear to me whether Google will grab all the content. Google definitely has the capability of accessing dynamic content, as @Domenic mentioned above, but the questions are:

1) How much can they access?

2) Do they attempt to access as much as possible from ALL sites or perhaps only large data sites like Facebook?

You could try providing default data to each view and layering in the dynamic, user-behavior based stuff later. Backbone calls this bootstrapping. http://backbonejs.org/#FAQ-bootstrap



来源:https://stackoverflow.com/questions/14718806/how-do-i-use-backbone-js-for-a-multi-page-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!