@@ -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,6 +2053,58 @@ Finished in 0.5 seconds
2053
2053
27 tests, 0 failures
2054
2054
```
2055
2055
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
+
2056
2108
<br />
2057
2109
2058
2110
### Deploy!
0 commit comments