@@ -534,12 +534,12 @@ and replace the contents with the following code:
534
534
<meta name =" viewport" content =" width=device-width, initial-scale=1.0" />
535
535
<title >Phoenix Todo List</title >
536
536
<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 >
537
538
</head >
538
539
<body >
539
540
<main role =" main" class =" container" >
540
541
<%= @inner_content %>
541
542
</main >
542
- <script defer type =" text/javascript" src =" <%= Routes.static_path(@conn, " /js /app.js " ) %>" ></script >
543
543
</body >
544
544
</html >
545
545
```
@@ -548,7 +548,7 @@ and replace the contents with the following code:
548
548
> Before:
549
549
[`/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 />
550
550
> 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 )
552
552
553
553
` <%= @inner_content %> ` is where the Todo App will be rendered.
554
554
@@ -2053,49 +2053,57 @@ Finished in 0.5 seconds
2053
2053
27 tests, 0 failures
2054
2054
```
2055
2055
2056
- ### 11.4 Add Turbolinks
2056
+ <br />
2057
+
2058
+ ### 11.4 Add Turbolinks to Eliminate Page Refresh
2057
2059
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 ` :
2059
2082
2060
2083
``` sh
2061
2084
cd assets && npm install turbolinks --save
2062
2085
```
2063
2086
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 )
2069
2089
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 :
2071
2091
2072
2092
``` js
2093
+ import Turbolinks from " turbolinks"
2073
2094
Turbolinks .start ();
2074
2095
```
2075
2096
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 )
2077
2099
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.
2096
2106
2097
- This will avoid the following warning:
2098
- ![ turbolink-warning] ( https://user-images.githubusercontent.com/6057298/83739727-e43b3700-a64d-11ea-8977-dbc347fd2fcd.png )
2099
2107
2100
2108
<br />
2101
2109
0 commit comments