Skip to content

Commit 7b1eb19

Browse files
committed
Add interactive live view to http caching section
1 parent 63b5d1a commit 7b1eb19

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

apps/components_guide_web/lib/components_guide_web/template_engines/markdown_engine.ex

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ defmodule ComponentsGuideWeb.TemplateEngines.MarkdownEngine do
1414
# regex = ~r{<live-([\w-]+)>(.+)</live-([\w-]+)>}
1515
regex = ~r{<live-([\w-]+)>([^<]+)</live-([^>]+)>}
1616
# regex = ~r{<live-([\w-]+)>}
17-
18-
html = Regex.replace(regex, html, fn whole, name, content -> name end)
17+
18+
html = Regex.replace(regex, html, fn whole, name, content ->
19+
case name do
20+
"render" ->
21+
module = content |> String.trim #|> String.to_existing_atom
22+
"<div><%= live_render(@conn, #{module}, session: %{}) %></div>"
23+
_ ->
24+
"!" <> name <> "!" <> content
25+
end
26+
end)
27+
28+
# html = Regex.replace(regex, html, fn whole, name, content -> "!" <> name <> "!" <> content end)
29+
1930
# html = Regex.replace(regex, html, fn whole, name, content -> "<div><%= live_render(@conn, ComponentsGuideWeb.FakeSearchLive, session: %{}) %></div>" end)
2031

2132
html |> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)

apps/components_guide_web/lib/components_guide_web/templates/web_standards/http-caching.html.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
ComponentsGuideWeb.WebStandards.Live.HttpCaching
55
</live-render>
66

7-
<div>
8-
<%= live_render(@conn, ComponentsGuideWeb.WebStandards.Live.HttpCaching, session: %{}) %>
9-
</div>
10-
117
## Resource that does not want to be cached
128

139
```http
@@ -33,7 +29,7 @@ Host: example.org
3329
```http
3430
HTTP/1.1 200 OK
3531
ETag: "ABC"
36-
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
32+
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
3733
Cache-Control: public, max-age=31536000
3834
Date: Fri, 02 Oct 2020 09:28:50 GMT
3935
Content-Type: image/png
@@ -58,7 +54,7 @@ Date: Fri, 02 Oct 2020 09:28:50 GMT
5854
Content-Type: image/png
5955
Content-Length: 2348
6056
Cache-Control: public, max-age=31536000
61-
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
57+
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
6258
Vary: Accept-Encoding
6359
6460
```
@@ -68,7 +64,7 @@ Vary: Accept-Encoding
6864
```http
6965
GET /assets/logo.png HTTP/1.1
7066
Host: example.org
71-
If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
67+
If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
7268
```
7369

7470
```http
@@ -78,7 +74,7 @@ Content-Type: image/png
7874
Content-Length: 2348
7975
Cache-Control: public, max-age=31536000
8076
ETag: "ABC"
81-
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
77+
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
8278
Vary: Accept-Encoding
8379
8480
```
@@ -98,7 +94,7 @@ Date: Fri, 02 Oct 2020 09:28:50 GMT
9894
Content-Type: image/png
9995
Content-Length: 2348
10096
Cache-Control: public, max-age=31536000
101-
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
97+
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
10298
Vary: Accept-Encoding
10399
104100
```
@@ -108,7 +104,7 @@ Vary: Accept-Encoding
108104
```http
109105
GET /assets/logo.png HTTP/1.1
110106
Host: example.org
111-
If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
107+
If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
112108
```
113109

114110
```http
@@ -118,7 +114,7 @@ Date: Fri, 02 Oct 2020 09:28:50 GMT
118114
Content-Type: image/png
119115
Content-Length: 2348
120116
Cache-Control: public, max-age=31536000
121-
Last-Modified: Thu, 22 Oct 2015 07:28:00 GMT
117+
Last-Modified: Thu, 22 Oct 2015 07:28:00 GMT
122118
Vary: Accept-Encoding
123119
124120
```
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
defmodule ComponentsGuideWeb.WebStandards.Live.HttpCaching do
22
use ComponentsGuideWeb, :live_view
33
alias ComponentsGuideWeb.StylingHelpers
4+
5+
defmodule State do
6+
defstruct local: nil, remote: nil
7+
end
48

59
def mount(_params, _session, socket) do
6-
{:ok, assign(socket, state: %{})}
10+
{:ok, assign(socket, state: %State{})}
711
end
812

913
def render(assigns) do
1014
~L"""
11-
<p>Hello!</p>
15+
<form phx-change=change>
16+
<fieldset class="flex flex-row space-x-4">
17+
<div class="font-bold">Local</div>
18+
<label><input type=radio name=local value=0 class="form-radio"> Blank</label>
19+
<label><input type=radio name=local value=1 class="form-radio"> Loaded previously</label>
20+
</fieldset>
21+
<fieldset class="flex flex-row space-x-4">
22+
<div class="font-bold">Remote</div>
23+
<label><input type=radio name=remote value=a class="form-radio"> ABC</label>
24+
<label><input type=radio name=remote value=z class="form-radio"> XYZ</label>
25+
</fieldset>
26+
</form>
1227
"""
1328
end
29+
30+
def handle_event("change", %{"local" => local}, socket) do
31+
state = socket.assigns.state
32+
state = put_in(state.local, local)
33+
34+
{
35+
:noreply,
36+
socket
37+
|> assign(:state, state)
38+
}
39+
end
40+
41+
def handle_event("change", %{"remote" => remote}, socket) do
42+
state = socket.assigns.state
43+
state = put_in(state.remote, remote)
44+
45+
{
46+
:noreply,
47+
socket
48+
|> assign(:state, state)
49+
}
50+
end
1451
end

0 commit comments

Comments
 (0)