routing in spa

70
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com | Dima Kuzmic Expert Web Develop E4D Solutions LT site: www.e4d.co. Routing in SPA Israel JavaScript Conference

Upload: dima-kuzmich

Post on 17-May-2015

2.905 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Routing in SPA

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

Page 2: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What’s all about?

Page 3: Routing in SPA

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

Page 4: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

HTML 5 )Changes The Rules of The

Game(

Page 5: Routing in SPA

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

Page 6: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What have we gained?

Interactivity

Speed

Great User Experience

Hybrid apps

Page 7: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Navigation Challenges

Page 8: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

History and Back Button

Page 9: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Deep Linking

Page 10: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Maintain Navigation

Page 11: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

SEO Friendly

Page 12: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Cross-Browser

Page 13: Routing in SPA

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

Page 15: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Choosing Your Way

Writing your own solution

Page 16: Routing in SPA

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.

Page 17: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Old Way

Page 19: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Problem

Page 20: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Inability to track hash changes

Request

Response

Page 21: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Method - Polling

Page 22: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Solution

Page 23: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Polling hash

Page 24: Routing in SPA

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

Page 25: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

How to

Page 26: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Solution

Page 27: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

onhashchange

Page 28: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Back button support

Remember those?

Page 29: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Solution

Page 30: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Back button support

Page 31: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The New Way

Page 32: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

One day I will change Url

without hash and without full

refresh.

Page 33: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Dreams come true!

Page 34: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The API

Page 35: Routing in SPA

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. 

Page 36: Routing in SPA

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

Page 37: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

HTML5 History API

Page 38: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

SEO

Page 39: Routing in SPA

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.

Page 40: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Page Backbone

Page 41: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Page after JS execution

Page 42: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What the User see

Page 43: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What the Crawler see

Page 44: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Hijax

Page 45: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Hijax motto

Plan for Ajax from the start

Implement Ajax at the end

Page 46: Routing in SPA

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

Page 47: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Page 48: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Hijax

Page 49: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Google !

Page 50: Routing in SPA

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

Page 51: Routing in SPA

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

Page 52: Routing in SPA

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

Page 53: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Page 54: Routing in SPA

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

Page 55: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Beginning

Use standard JavaScript definition:

Or use a jQuery like syntax

Page 56: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Routes

Define routes directly:

Or use a verb shortcuts:

Page 57: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Don’t forget to Run() your app

Page 58: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

The Beginning

Page 59: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Route With Parameters

Page 60: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Who is ‘this'?

this == context == Sammy.EventContext

Page 61: Routing in SPA

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

Page 62: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Form Submitting

'post', 'put' and 'delete' can be invoked only by submitting form

Page 63: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Routes and Forms

Page 64: Routing in SPA

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

Page 65: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Trigger Events

You can trigger events on the app context and EventContext.

Page 66: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Passing Data To Event

You can pass custom data object as third parameter

Page 67: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Sammy.js Events

Page 69: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

www.js-il.com

18.6.13

Page 70: Routing in SPA

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Thanks

Dima KuzmichExpert Web Developer

E4D Solutions LTDsite: www.e4d.co.il