Slide 1

Slide 1 text

PushState @danwrong or Bust

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

http://storify.com/timhaines/hashbang-conversation

Slide 4

Slide 4 text

“We MUST stop page refreshes!” - JavaScript Nerds

Slide 5

Slide 5 text

Why? ‣ Do snazzy transitions between pages ‣ Your client-side MVC app gives you no choice ‣ Reduce amount of bytes over the wire ‣ Reduce burden on the server

Slide 6

Slide 6 text

Breaking the web ‣ The back button ‣ Bookmarking and link sharing ‣ Search engine crawling ‣ Redirects, 404 and other HTTP level stuff ‣ Works (at least minimally) across browsers

Slide 7

Slide 7 text

So what are our options?

Slide 8

Slide 8 text

Hashbangs

Slide 9

Slide 9 text

twitter.com/#!/mentions

Slide 10

Slide 10 text

// Link to fragment identifier @danwrong // Listen for change, do change state window.addEventListener('hashchange', function() { var path = location.hash.replace(/^#!/, ''); $.get(path, function(state) { render(state); }); });

Slide 11

Slide 11 text

http://danwebb.net/2011/5/28/it-is-about-the-hashbangs

Slide 12

Slide 12 text

TL;DR !=

Slide 13

Slide 13 text

Have we killed the web? ‣ The back button ‣ Bookmarking and link sharing ‣ Search engine crawling ‣ Redirects, 404 and other HTTP level stuff ‣ Works (at least minimally) across browsers Meh. Meh. Meh.

Slide 14

Slide 14 text

PushState

Slide 15

Slide 15 text

// fetch new state $.get('/danwrong', function(state) { history.pushState(state, "Twitter / danwrong", "/danwrong"); render(state); } // Handle back / forward button window.addEventListener('popstate', function(e) { render(e.state); });

Slide 16

Slide 16 text

Have we killed the web? ‣ The back button ‣ Bookmarking and link sharing ‣ Search engine crawling ‣ Redirects, 404 and other HTTP level stuff ‣ Works (at least minimally) across browsers Meh.

Slide 17

Slide 17 text

PushState falling back to Hashbang

Slide 18

Slide 18 text

Have we killed the web? ‣ The back button ‣ Bookmarking and link sharing ‣ Search engine crawling ‣ Redirects, 404 and other HTTP level stuff ‣ Works (at least minimally) across browsers

Slide 19

Slide 19 text

PushState or bust

Slide 20

Slide 20 text

If pushState is supported use it...

Slide 21

Slide 21 text

otherwise...

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Page refresh!

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Mentions if (history.pushState) { document.addEventListener('click', function(e) { if (e.target.className == 'ps') { $.get(e.target.href, function(state) { render(state); history.pushState(state, state.title, e.target.href); }); e.preventDefault(); } }); }

Slide 26

Slide 26 text

Meanwhile, on the server...

Slide 27

Slide 27 text

class ConnectController def mentions @mentions = get_mentions if request.xhr? render :json => { :title => 'Twitter / mentions', :section => 'connect', # render just the page content but not the global "chrome" :page_html => MentionsView.new(@mentions).render } end # otherwise render the whole page as normal end end

Slide 28

Slide 28 text

Have we killed the web? ‣ The back button ‣ Bookmarking and link sharing ‣ Search engine crawling ‣ Redirects, 404 and other HTTP level stuff ‣ Works (at least minimally) across browsers

Slide 29

Slide 29 text

PushState or bust

Slide 30

Slide 30 text

But what about my client- side MVC app?

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Questions?