phlex-sinatra 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile +2 -0
- data/README.md +51 -2
- data/lib/phlex/sinatra/version.rb +1 -1
- data/lib/phlex-sinatra.rb +8 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6281c981ddfb5118f6a6f0a38297dee0152e7052dc29aab21284de40814acfbb
|
4
|
+
data.tar.gz: 4d56528737f86832f8113ae8da2c05b0b41ef0643d7dc8f8a36d435e0ec7f00a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c818ef9cdee0f1bc70e2a8cdb98956c7e83313e2774d09330c6487a9c1f3f2b504c7aae5e57d5fefa7693311b609a3f94c16aa6caa63f18b76135f3c304d7309
|
7
|
+
data.tar.gz: c67a8d2d7d4b3b279fa812eb173b6c854af7c87d587894a6907059238e7c3df21daec28d2d0b14a2a600961b68f39deffe98d150bc7a2f0dce5386779e081893
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## Version 0.3.0 - 2023-12-13
|
2
|
+
|
3
|
+
- Add support for streaming a view. Pass `stream: true` to the `#phlex` helper so Phlex will use Sinatra's streaming capability.
|
4
|
+
|
1
5
|
## Version 0.2.0 - 2023-04-24
|
2
6
|
|
3
7
|
- Allow passing a `content_type:` kwarg to the `#phlex` helper so it behaves like Sinatra's other template helpers (defaults to `:html` – or `:svg` for a `Phlex::SVG` instance).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -42,7 +42,56 @@ get '/foo' do
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
-
##
|
45
|
+
## Streaming
|
46
|
+
|
47
|
+
Streaming a Phlex view can be enabled by passing `stream: true` which will cause Phlex to automatically write to the response after the closing `</head>` and buffer the remaining content:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
get '/foo' do
|
51
|
+
phlex MyView.new, stream: true
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Even with no further intervention this small change means that the browser will receive the complete `<head>` as quickly as possible and can start fetching and processing its external resources while waiting for the rest of the page to download.
|
56
|
+
|
57
|
+
You can also manually flush the contents of the buffer at any point using Phlex's `#flush` method:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class Layout < Phlex::HTML
|
61
|
+
def template(&)
|
62
|
+
doctype
|
63
|
+
html {
|
64
|
+
head {
|
65
|
+
# All the usual stuff: links to external stylesheets and JavaScript etc.
|
66
|
+
}
|
67
|
+
# Phlex will automatically flush to the response at this point which will
|
68
|
+
# benefit all pages that opt in to streaming.
|
69
|
+
body {
|
70
|
+
# Standard site header and navigation.
|
71
|
+
render Header.new
|
72
|
+
|
73
|
+
yield_content(&)
|
74
|
+
}
|
75
|
+
}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class MyView < Phlex::HTML
|
80
|
+
def template
|
81
|
+
render Layout.new {
|
82
|
+
# Knowing that this page can take a while to generate we can choose to
|
83
|
+
# flush here so the browser can render the site header while downloading
|
84
|
+
# the rest of the page - which should help minimise the First Contentful
|
85
|
+
# Paint metric.
|
86
|
+
flush
|
87
|
+
|
88
|
+
# The rest of the big long page...
|
89
|
+
}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
## Why do I need Sinatra's `url()` helper?
|
46
95
|
|
47
96
|
It might not seem obvious at first why you'd use `url()` at all given that you mostly just pass the string you want to output and then probably `false` so the scheme/host isn't included.
|
48
97
|
|
@@ -54,7 +103,7 @@ There are a couple of reasons:
|
|
54
103
|
|
55
104
|
2. **Awareness that the app is being served from a subdirectory**
|
56
105
|
|
57
|
-
This isn't something you encounter very often in a standard Sinatra app but you hit it quite quickly if you're using [Parklife](https://github.com/benpickles/parklife) to generate a static build
|
106
|
+
This isn't something you encounter very often in a standard Sinatra app but you hit it quite quickly if you're using [Parklife](https://github.com/benpickles/parklife) to generate a static build hosted on GitHub Pages – which is exactly what prompted me to write this integration.
|
58
107
|
|
59
108
|
In this case by using the `url()` helper you won’t have to change anything when switching between serving the app from `/` in development and hosting it at `/my-repository/` in production – internal links to other pages/stylesheets/etc will always be correct regardless.
|
60
109
|
|
data/lib/phlex-sinatra.rb
CHANGED
@@ -37,13 +37,19 @@ end
|
|
37
37
|
|
38
38
|
module Sinatra
|
39
39
|
module Templates
|
40
|
-
def phlex(obj, content_type: nil)
|
40
|
+
def phlex(obj, content_type: nil, stream: false)
|
41
41
|
raise Phlex::Sinatra::TypeError.new(obj) unless obj.is_a?(Phlex::SGML)
|
42
42
|
|
43
43
|
content_type ||= :svg if obj.is_a?(Phlex::SVG)
|
44
44
|
self.content_type(content_type) if content_type
|
45
45
|
|
46
|
-
|
46
|
+
if stream
|
47
|
+
self.stream do |out|
|
48
|
+
obj.call(out, view_context: self)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
obj.call(view_context: self)
|
52
|
+
end
|
47
53
|
end
|
48
54
|
end
|
49
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phlex-sinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Pickles
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phlex
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.7.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.7.0
|
27
27
|
description: A Phlex adapter for Sinatra
|
28
28
|
email:
|
29
29
|
- spideryoung@gmail.com
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
- !ruby/object:Gem::Version
|
65
65
|
version: '0'
|
66
66
|
requirements: []
|
67
|
-
rubygems_version: 3.4.
|
67
|
+
rubygems_version: 3.4.10
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: A Phlex adapter for Sinatra
|