phlex-sinatra 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0e0c3d6038f57d3a01b5abb0172d1598a0edb08d46e455dede38da9ad708560
4
- data.tar.gz: 6e5025677940fc22ed1830405646c18d4d441f8125a4c37f3179d8c8e735a354
3
+ metadata.gz: 6281c981ddfb5118f6a6f0a38297dee0152e7052dc29aab21284de40814acfbb
4
+ data.tar.gz: 4d56528737f86832f8113ae8da2c05b0b41ef0643d7dc8f8a36d435e0ec7f00a
5
5
  SHA512:
6
- metadata.gz: e94e93fbb09a9b6043667f8139cc515a4d6b5302f3459b5216d4f900d471579b4621716e1734d4dd38ead39788074c6442ed3f0c46ce63e7aa9faa4868bb4cda
7
- data.tar.gz: 5026ea9e6f0af3ae2b064dc475d441e85b6753e49e37ef1e57f6aca00d3d7e15feb8a6eb188a6939eff70c0dae7ff1fa4db8447f3cd2a6c512a9212273ef4a4e
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
@@ -5,6 +5,8 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in phlex-sinatra.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'capybara'
9
+ gem 'puma'
8
10
  gem 'rack-test'
9
11
  gem 'rake'
10
12
  gem 'rspec'
data/README.md CHANGED
@@ -42,7 +42,56 @@ get '/foo' do
42
42
  end
43
43
  ```
44
44
 
45
- ## Why?
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 which you host on GitHub Pages – which is exactly what prompted me to write this integration.
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Sinatra
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
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
- obj.call(view_context: self)
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.2.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-04-24 00:00:00.000000000 Z
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: '0'
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: '0'
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.6
67
+ rubygems_version: 3.4.10
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: A Phlex adapter for Sinatra