Skip to content

Commit 42f2ec2

Browse files
committed
1 parent 1f2fdf6 commit 42f2ec2

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

README.md

Lines changed: 39 additions & 31 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,49 +2053,57 @@ Finished in 0.5 seconds
20532053
27 tests, 0 failures
20542054
```
20552055

2056-
### 11.4 Add Turbolinks
2056+
<br />
2057+
2058+
### 11.4 Add Turbolinks to Eliminate Page Refresh
20572059

2058-
Add [turbolinks package](https://www.npmjs.com/package/turbolinks) to `/assets/package.json`:
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`:
20592082

20602083
```sh
20612084
cd assets && npm install turbolinks --save
20622085
```
20632086

2064-
In `assets/app.js` import turbolinks:
2065-
2066-
```js
2067-
import Turbolinks from "turbolinks"
2068-
```
2087+
e.g:
2088+
[`/assets/package.json#L18`](https://github.com/dwyl/phoenix-todo-list-tutorial/blob/1f2fdf6903c7a3c2f87e4340c12ac59303ce70ae/assets/package.json#L18)
20692089

2070-
and start it by calling the `start` function at the end of the `app.js` file:
2090+
In `assets/app.js` import Turbolinks and start it:
20712091

20722092
```js
2093+
import Turbolinks from "turbolinks"
20732094
Turbolinks.start();
20742095
```
20752096

2076-
Finally move the `app.js` script call from the html body to the header in `lib/app_web/tempaltes/layout/app.html.eex`:
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)
20772099

2078-
```elixir
2079-
<!DOCTYPE html>
2080-
<html lang="en">
2081-
<head>
2082-
<meta charset="utf-8"/>
2083-
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
2084-
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
2085-
<title>Phoenix Todo List</title>
2086-
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
2087-
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
2088-
</head>
2089-
<body>
2090-
<main role="main" class="container">
2091-
<%= @inner_content %>
2092-
</main>
2093-
</body>
2094-
</html>
2095-
```
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.
20962106

2097-
This will avoid the following warning:
2098-
![turbolink-warning](https://user-images.githubusercontent.com/6057298/83739727-e43b3700-a64d-11ea-8977-dbc347fd2fcd.png)
20992107

21002108
<br />
21012109

0 commit comments

Comments
 (0)