42
Turbolinks + Vue.js Pascal Laliberté

Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Embed Size (px)

Citation preview

Page 1: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Turbolinks + Vue.jsPascal Laliberté

Page 2: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Starting small Depth

WHAT I VALUE

Page 3: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Turbolinks Vue.js

WHICH IS WHY I’M ATTRACTED TO THESE TWO TECHNOLOGIES

Page 4: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Turbolinks

Page 5: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

A PAGE HAS A BODY

Page 6: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

AND A HEAD

<head>

A PAGE HAS A BODY

Page 7: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<head>

Replaces the body

with the new content

<head>

WITH TURBOLINKS, CLICKING A LINK…

Page 8: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<head>

Replaces the body

with the new content

<head>

WITH TURBOLINKS, CLICKING A LINK…

Merges the head

Page 9: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<head>

Replaces the body

with the new content

<head>

WITH TURBOLINKS, CLICKING A LINK…

Merges the head

It keeps a Cache of snapshotsFor Back operations, etc

Page 10: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<head>

Replaces the body

with the new content

<head>

WITH TURBOLINKS, CLICKING A LINK…

Merges the head

It keeps a Cache of snapshotsFor Back operations, etc

WHICH MAKES PAGE TRANSITIONS(AND PAGE RESTORES)

SUPER QUICK

Page 11: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Vue.js

Page 12: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

jQuery

In the category of Front-end UX

Dev Tools

Page 13: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

jQuery Vanilla JS

In the category of Front-end UX

Dev Tools

Page 14: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Angular A full-fledge client-side

MVC

jQuery Vanilla JS

In the category of Front-end UX

Dev Tools

Page 15: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

React

Angular A full-fledge client-side

MVC

jQuery Vanilla JS

In the category of Front-end UX

Dev Tools

Page 16: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

React

Angular A full-fledge client-side

MVC

jQuery Vanilla JS

In the category of Front-end UX

Dev Tools

View renderers

Page 17: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

React

Angular A full-fledge client-side

MVC

Vue

jQuery Vanilla JS

In the category of Front-end UX

Dev Tools

View renderers

Page 18: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

AS AN EXAMPLE, LET’S TAKE A FORM FIELD

Page 19: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<div class=“purchase-form”> <input v-model=“quantity”>

AS AN EXAMPLE, LET’S TAKE A FORM FIELDAS PART OF A PURCHASE FORM…

Page 20: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

new Vue({ el: “.purchase-form”, data: { }

quantity: null

})

<div class=“purchase-form”> <input v-model=“quantity”>

WHEN WE SETUP THE VUE INSTANCE, WE SET THE MODEL

Page 21: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

new Vue({ el: “.purchase-form”, data: { }

quantity: 2

}

2

<div class=“purchase-form”> <input v-model=“quantity”>

})

AS THE FORM FIELD’S VALUE CHANGES, SO DOES THE VALUE IN THE MODEL

Page 22: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

new Vue({ el: “.purchase-form”, data: { }

quantity: 23

}

23

<div class=“purchase-form”> <input v-model=“quantity”>

})

AS THE FORM FIELD’S VALUE CHANGES, SO DOES THE VALUE IN THE MODEL

AND VICE VERSA

Page 23: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

, price: 25 }

new Vue({ el: “.purchase-form”, data: {

quantity: 23

})

23

WE CAN ADD OTHER PROPERTIES TO THE MODEL

Page 24: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

, price: 25 }

new Vue({ el: “.purchase-form”, data: {

quantity: 23

})

23 , computed: { total () { return this.quantity * this.price } }

AND COMPUTED PROPERTIES TOO, WHICH UPDATE ON THE FLY

Page 25: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

, price: 25 }

new Vue({ el: “.purchase-form”, data: {

quantity: 23

})

23

methods events components

, computed: { total () { return this.quantity * this.price } }

YOU CAN ALSO DEFINE

Page 26: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<map-popover :lat=“lat” :long=“long” :zoom=“zoom” ></map-popover>

DEFINING SUB-COMPONENTS MAKES VUE SCALABLE

Page 27: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

React

Angular

Vue

jQuery Vanilla JS

SPA

Front-end UX

vue-routervue-resource SPA

YOU CAN GO ALL OUT AND BUILD A SPA…

Page 28: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Approachable

SmallFast

DelightfulScalable

BUT VUE’S BEST QUALITIES ARE THAT IT IS

Page 29: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

Now available: Vue.js 2.0

Page 30: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

IF YOU WANT

mostly server-generated content

in places: more front-end reactivity

Turbolinks + Vuejs

mostly jQuery / Vanilla js, Unobtrusive JS

Page 31: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

<head>

Replaces the body

with the new content

<head>

WITH TURBOLINKS, CLICKING A LINK…

Merges the head

It keeps a Cache of snapshotsFor Back operations, etc

THE PROBLEM MERGING THEM TOGETHER IS THE BODY REPLACEMENT

CAUSES VUE INSTANCES TO LOSE REACTIVITY ON RESTORES

Turbolinks + Vuejs

Page 32: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

new Vue({ el: “.purchase-form”, data: { }

quantity: 23

}

23

REACTIVITY = DOM EVENTS, OBSERVERS

DESTROYED WHEN CLONING THE BODY FOR THE RESTORE CACHE

Page 33: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

FOR THE SOLUTION LET’S LOOK AT THE TIMELINE OF EVENTS

Page 34: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load

FOR THE SOLUTION LET’S LOOK AT THE TIMELINE OF EVENTS

Page 35: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

FOR THE SOLUTION LET’S LOOK AT THE TIMELINE OF EVENTS

Page 36: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy(Vue.js events)

FOR THE SOLUTION LET’S LOOK AT THE TIMELINE OF EVENTS

Page 37: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy

re-inject in turbolinks snapshot the original DOM

element with end data (serialized) injected

(Vue.js events)

Page 38: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy

save initial DOM element before it’s

walked

re-inject in turbolinks snapshot the original DOM

element with end data (serialized) injected

(Vue.js events)

Page 39: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy

save initial DOM element before it’s

walked

re-inject in turbolinks snapshot the original DOM

element with end data (serialized) injected

detect serialized start data attached to initial DOM element

(Vue.js events)

Page 40: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy

save initial DOM element before it’s

walked

re-inject in turbolinks snapshot the original DOM

element with end data (serialized) injected

detect serialized start data attached to initial DOM element

Vue.js Mixin

(Vue.js events)

Page 41: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

turbolinks:load turbolinks:before-cache

init destroy

save initial DOM element before it’s

walked

re-inject in turbolinks snapshot the original DOM

element with end data (serialized) injected

detect serialized start data attached to initial DOM element

Vue.js Mixin …for each Vue component on the page

(Vue.js events)

Page 42: Making Turbolinks work with Vue.js: Fast server-generated pages with reactive front-end components

@pascallalibertepascallaliberte.me