routing in spa
TRANSCRIPT
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Dima KuzmichExpert Web Developer
E4D Solutions LTDsite: www.e4d.co.il
Routing in SPA
Israel JavaScript Conference
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
What’s all about?
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Traditional apps
Browser Request
Index.html
Server
Traditional Request / Response for ALL rendered content and assets
Client
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
HTML 5 )Changes The Rules of The
Game(
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Web Application (SPA)
Initial Request
Application.htm RequireJS Loader
Page1 Partial.htm
IndexViewModel.js
Application.js (bootstrap)
ViewModel (#index)
ViewModel (#login)
Model (LoginModel)JSON Requests
HTML5 localstorage
Handling disconnection
Server Client
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
What have we gained?
Interactivity
Speed
Great User Experience
Hybrid apps
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Navigation Challenges
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
History and Back Button
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Deep Linking
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Maintain Navigation
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
SEO Friendly
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Cross-Browser
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Choosing Your Way
Choosing frameworks that provides you with routing and navigation built-in
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Choosing Your Way
Choosing stand-alone navigation libraries
History.js jQuery BBQ
Crossroads.js
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Choosing Your Way
Writing your own solution
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Choosing Your WayMy personal opinion is that we don’t need to reinvent the wheel, so the easiest way is to use ready solutions.
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Old Way
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Why?
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Problem
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Inability to track hash changes
Request
Response
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Method - Polling
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Solution
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Polling hash
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
onhashchange event
3.6 (1.9.2)
5.0
5.0
8.0
10.6
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
How to
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Solution
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
onhashchange
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Back button support
Remember those?
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Solution
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Back button support
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The New Way
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
One day I will change Url
without hash and without full
refresh.
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Dreams come true!
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The API
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
window.onpushstate?
Unfortunately, HTML5 doesn't provide us with a native event that will fire on pushing new state.
Thus, if we want to provide response to that kind of change, we need to implement it by ourselves.
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
HTML5 History API
4.0 (2.0)
5.0
5.0
10.0
11.5
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
HTML5 History API
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
SEO
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
SEO
The basis of SEO is that every page with different content is in fact a different document, therefore it has a different location (URL).
Since AJAX uses JavaScript to generate the page with new content search engine robots cannot crawl these altered pages.
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Page Backbone
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Page after JS execution
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
What the User see
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
What the Crawler see
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Hijax
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Hijax motto
Plan for Ajax from the start
Implement Ajax at the end
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Hijax way
Create an app using traditional page refreshes
Data requested from server via regular links and forms
Intercept (hijack) your links and forms using JavaScript
Send that data via XMLHttpRequest to the server
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Hijax
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Google !
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Making Ajax Apps Crawlable
1. Indicate to the crawler that your site supports the AJAX crawling scheme
Change your URL from this:
www.app.com/index.html#my-hash-valueTo this new one:
www.app.com/index.html#!my-hash-value
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Making Ajax Apps Crawlable
2. Set up your server to handle requests for URLs that contain
Instead this URL:
www.app.com/index.html#!my-hash-valueCrawler will go to tis one:
www.app.com/index.html?_escaped_fragment_=my-hash-value
http://crawljax.com – Javahttps://github.com/gregorypratt/Ajax-Crawling - ASP.NET MVC
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Making Ajax Apps Crawlable
3. Handle pages without hash fragments
* Full explanation and step-by-step guide you can find here
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
My name is Sammy, and I’m Tiny (5.2K when I’m compressed and
gzipped)
Built on top of jQuery
Using hash for navigation
Supporting back/forward buttons
Bookmarkable
RESTful
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Beginning
Use standard JavaScript definition:
Or use a jQuery like syntax
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Routes
Define routes directly:
Or use a verb shortcuts:
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Don’t forget to Run() your app
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
The Beginning
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Route With Parameters
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Who is ‘this'?
this == context == Sammy.EventContext
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
EventContextEventContext support some useful args and methods:
app $element params path redirect() trigger()
* Full list of arguments and events check out here
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Form Submitting
'post', 'put' and 'delete' can be invoked only by submitting form
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Routes and Forms
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Events
You can bind custom event to the app.Inside events is EventContext, same as in routes
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Trigger Events
You can trigger events on the app context and EventContext.
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Passing Data To Event
You can pass custom data object as third parameter
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Sammy.js Events
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Resources
http://singlepageappbook.com/index.html
http://
www.informit.com/articles/article.aspx?p=789762
https://developers.google.com/webmasters/ajax-cr
awling/docs/getting-started
http://net.tutsplus.com/tutorials/javascript-ajax/qui
ck-tip-an-introduction-to-sammy-js
/
http://sammyjs.org/
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
www.js-il.com
18.6.13
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |
Thanks
Dima KuzmichExpert Web Developer
E4D Solutions LTDsite: www.e4d.co.il