“Single-page” JS websites and SEO

后端 未结 9 1745
春和景丽
春和景丽 2020-11-30 16:13

There are a lot of cool tools for making powerful \"single-page\" JavaScript websites nowadays. In my opinion, this is done right by letting the server act as an API (and no

相关标签:
9条回答
  • 2020-11-30 16:47

    So, it seem that the main concern is being DRY

    • If you're using pushState have your server send the same exact code for all urls (that don't contain a file extension to serve images, etc.) "/mydir/myfile", "/myotherdir/myotherfile" or root "/" -- all requests receive the same exact code. You need to have some kind url rewrite engine. You can also serve a tiny bit of html and the rest can come from your CDN (using require.js to manage dependencies -- see https://stackoverflow.com/a/13813102/1595913).
    • (test the link's validity by converting the link to your url scheme and testing against existence of content by querying a static or a dynamic source. if it's not valid send a 404 response.)
    • When the request is not from a google bot, you just process normally.
    • If the request is from a google bot, you use phantom.js -- headless webkit browser ("A headless browser is simply a full-featured web browser with no visual interface.") to render html and javascript on the server and send the google bot the resulting html. As the bot parses the html it can hit your other "pushState" links /somepage on the server <a href="/someotherpage">mylink</a>, the server rewrites url to your application file, loads it in phantom.js and the resulting html is sent to the bot, and so on...
    • For your html I'm assuming you're using normal links with some kind of hijacking (e.g. using with backbone.js https://stackoverflow.com/a/9331734/1595913)
    • To avoid confusion with any links separate your api code that serves json into a separate subdomain, e.g. api.mysite.com
    • To improve performance you can pre-process your site pages for search engines ahead of time during off hours by creating static versions of the pages using the same mechanism with phantom.js and consequently serve the static pages to google bots. Preprocessing can be done with some simple app that can parse <a> tags. In this case handling 404 is easier since you can simply check for the existence of the static file with a name that contains url path.
    • If you use #! hash bang syntax for your site links a similar scenario applies, except that the rewrite url server engine would look out for _escaped_fragment_ in the url and would format the url to your url scheme.
    • There are a couple of integrations of node.js with phantom.js on github and you can use node.js as the web server to produce html output.

    Here are a couple of examples using phantom.js for seo:

    http://backbonetutorials.com/seo-for-single-page-apps/

    http://thedigitalself.com/blog/seo-and-javascript-with-phantomjs-server-side-rendering

    0 讨论(0)
  • 2020-11-30 16:49

    I think you need this: http://code.google.com/web/ajaxcrawling/

    You can also install a special backend that "renders" your page by running javascript on the server, and then serves that to google.

    Combine both things and you have a solution without programming things twice. (As long as your app is fully controllable via anchor fragments.)

    0 讨论(0)
  • 2020-11-30 16:50

    If you're using Rails, try poirot. It's a gem that makes it dead simple to reuse mustache or handlebars templates client and server side.

    Create a file in your views like _some_thingy.html.mustache.

    Render server side:

    <%= render :partial => 'some_thingy', object: my_model %>
    

    Put the template your head for client side use:

    <%= template_include_tag 'some_thingy' %>
    

    Rendre client side:

    html = poirot.someThingy(my_model)
    
    0 讨论(0)
  • 2020-11-30 16:50

    This might help you : https://github.com/sharjeel619/SPA-SEO

    Logic

    • A browser requests your single page application from the server, which is going to be loaded from a single index.html file.
    • You program some intermediary server code which intercepts the client request and differentiates whether the request came from a browser or some social crawler bot.
    • If the request came from some crawler bot, make an API call to your back-end server, gather the data you need, fill in that data to html meta tags and return those tags in string format back to the client.
    • If the request didn't come from some crawler bot, then simply return the index.html file from the build or dist folder of your single page application.
    0 讨论(0)
  • 2020-11-30 16:53

    Use NodeJS on the serverside, browserify your clientside code and route each http-request's(except for static http resources) uri through a serverside client to provide the first 'bootsnap'(a snapshot of the page it's state). Use something like jsdom to handle jquery dom-ops on the server. After the bootsnap returned, setup the websocket connection. Probably best to differentiate between a websocket client and a serverside client by making some kind of a wrapper connection on the clientside(serverside client can directly communicate with the server). I've been working on something like this: https://github.com/jvanveen/rnet/

    0 讨论(0)
  • 2020-11-30 17:02

    To take a slightly different angle, your second solution would be the correct one in terms of accessibility...you would be providing alternative content to users who cannot use javascript (those with screen readers, etc.).

    This would automatically add the benefits of SEO and, in my opinion, would not be seen as a 'naughty' technique by Google.

    0 讨论(0)
提交回复
热议问题