I have some pages that I need to render via the server side to make them search engine friendly. For search engines, it should function as a classic website. For users, I want to make the interface more interactive. My thought is to render the page server side, then use knockout and jquery to fetch the data again via ajax and bind it to the page.
I'm concerned about having flashes of no content, or duplicated content. Is there a best practice/pattern for cases like this?
The process would look like this:
- Render page using server side, including a long list of html elements.
- Use jQuery to fetch the same data already rendered to the page.
- Clear server side content with jquery.
- Bind ajax to knockout template, which effectively renders the page as it was previously.
- Subsequent clicks to page through data by a normal user uses ajax and knockout to render the data.
- Search engine can follow standard links to see the same data as the user.
The part I'm hung up on is how to pre-render, clear, and re-render with knockout/jquery.
Maybe my thought process is off a bit, I'd love to hear feedback.
Its doable, basically populate your data from server side, but add the "data-bind" attributes to your inputs, from knockout part, set your observables by fetching the field values.
here is a sample for a simple form:
MVC Part:
And your View:
And finally your knockout part:
Hope that works for your case.
EDIT: Fixing typo of data_bind to data-bind
You can render knockout using Node and a simple DOM implementation like domino:
This uses Durandal in addition to Knockout, but I had the same issue and this was the solution I came up with:
https://github.com/bestguy/durandal-delayed-composition
You could try the knockout pre-rendered binding handler. It basically does what Tamim suggested, but it handles foreach and works for any data-bind.
https://www.npmjs.com/package/knockout-pre-rendered
You could also consider using React, which is "universal" - you could render the page on the server and on the client with the same code. Since React uses a diff-engine there would be no flashing when the server rendered content is replaced by the client rendered content.
http://facebook.github.io/react/
http://reactjs.net/
It is also possible to use peTemplate binding. For tempate and foreach binding, it doesn't regenerate HTML and it instead uses the server side generated HTML. The only thing you need to do is annotating the HTML in server by few attributes that specify the key of data.