Skip to content

Commit 22a8a0c

Browse files
authored
Merge pull request #42 from dwyl/turbolinks-#40
add turbolinks to the application
2 parents 0d45317 + 42f2ec2 commit 22a8a0c

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,12 @@ and replace the contents with the following code:
534534
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
535535
<title>Phoenix Todo List</title>
536536
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
537+
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
537538
</head>
538539
<body>
539540
<main role="main" class="container">
540541
<%= @inner_content %>
541542
</main>
542-
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
543543
</body>
544544
</html>
545545
```
@@ -548,7 +548,7 @@ and replace the contents with the following code:
548548
> Before:
549549
[`/lib/app_web/templates/layout/app.html.eex`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/bddacda93ecd892fe0907210bab335e6b6e5e489/lib/app_web/templates/layout/app.html.eex) <br />
550550
> After:
551-
[`/lib/app_web/templates/layout/app.html.eex#L12`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/4d9e2031687d07494f98fad407dc5cc2be795b24/lib/app_web/templates/layout/app.html.eex#L12)
551+
[`/lib/app_web/templates/layout/app.html.eex#L12`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/1f2fdf6903c7a3c2f87e4340c12ac59303ce70ae/lib/app_web/templates/layout/app.html.eex#L12)
552552

553553
`<%= @inner_content %>` is where the Todo App will be rendered.
554554

@@ -2053,6 +2053,58 @@ Finished in 0.5 seconds
20532053
27 tests, 0 failures
20542054
```
20552055

2056+
<br />
2057+
2058+
### 11.4 Add Turbolinks to Eliminate Page Refresh
2059+
2060+
Given that our Phoenix Todo List App is 100% server rendered,
2061+
older browsers will perform a full page refresh
2062+
when an action (create/edit/toggle/delete) is performed.
2063+
This will feel like a "blink" in the page
2064+
and on **_really_ slow connections**
2065+
it will result in a temporary **_blank_ page**!
2066+
Obviously, that's _horrible_ UX and is a big part of why
2067+
Single Page Apps (SPAs) became popular;
2068+
to avoid page refresh, use
2069+
**[Turbolinks](https://github.com/turbolinks/turbolinks)®**!
2070+
2071+
Get the performance benefits of an SPA
2072+
without the added complexity
2073+
of a client-side JavaScript framework.
2074+
When a link is clicked/tapped,
2075+
Turbolinks automatically fetches the page,
2076+
swaps in its `<body>`, and merges its `<head>`,
2077+
all without incurring the cost of a full page load.
2078+
2079+
2080+
Add [turbolinks package](https://www.npmjs.com/package/turbolinks)
2081+
to `/assets/package.json`:
2082+
2083+
```sh
2084+
cd assets && npm install turbolinks --save
2085+
```
2086+
2087+
e.g:
2088+
[`/assets/package.json#L18`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/1f2fdf6903c7a3c2f87e4340c12ac59303ce70ae/assets/package.json#L18)
2089+
2090+
In `assets/app.js` import Turbolinks and start it:
2091+
2092+
```js
2093+
import Turbolinks from "turbolinks"
2094+
Turbolinks.start();
2095+
```
2096+
2097+
e.g:
2098+
[`assets/js/app.js#L16-L17`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/1f2fdf6903c7a3c2f87e4340c12ac59303ce70ae/assets/js/app.js#L16-L17)
2099+
2100+
That's it!
2101+
Now when you deploy your server rendered Phoenix App,
2102+
it will _feel_ like an SPA!
2103+
Seriously, try the Heroku demo again:
2104+
[phxtodo.herokuapp.com](https://phxtodo.herokuapp.com/)
2105+
Feel that buttery-smooth page transition.
2106+
2107+
20562108
<br />
20572109

20582110
### Deploy!

assets/js/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ import "../css/app.scss"
1212
// import {Socket} from "phoenix"
1313
// import socket from "./socket"
1414
//
15-
import "phoenix_html"
15+
import "phoenix_html"
16+
import Turbolinks from "turbolinks"
17+
Turbolinks.start();

assets/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"dependencies": {
1616
"phoenix": "file:../deps/phoenix",
17-
"phoenix_html": "file:../deps/phoenix_html"
17+
"phoenix_html": "file:../deps/phoenix_html",
18+
"turbolinks": "^5.2.0"
1819
},
1920
"devDependencies": {
2021
"@babel/core": "^7.0.0",

lib/app_web/templates/layout/app.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
77
<title>Phoenix Todo List</title>
88
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
9+
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
910
</head>
1011
<body>
1112
<main role="main" class="container">
1213
<%= @inner_content %>
1314
</main>
14-
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
1515
</body>
1616
</html>

0 commit comments

Comments
 (0)