turbo_frame_src 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/Rakefile +12 -0
- data/app/javascript/turbo_frame_src/index.js +18 -0
- data/lib/turbo_frame_src/engine.rb +23 -0
- data/lib/turbo_frame_src/helper.rb +7 -0
- data/lib/turbo_frame_src/middleware.rb +17 -0
- data/lib/turbo_frame_src/railtie.rb +7 -0
- data/lib/turbo_frame_src/version.rb +5 -0
- data/lib/turbo_frame_src.rb +10 -0
- data/package.json +12 -0
- data/sig/turbo_frame_src.rbs +4 -0
- metadata +61 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d98ef71bdc867a5320038121e1e22c6e458515019ddfd8d708ff726cb60e2c7c
|
|
4
|
+
data.tar.gz: '028d11c82757191c3890e0ee5b4165960944bcb0d28e196b9743cba75567d9fd'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ad5d603d63c04211ea15272a2e898f5174a3cbfb09c8e5da9815bd39e0bca155efc6497be52da77ac2f030424e383c26709a773d32e32a42b4c52ab5b4c52eb4
|
|
7
|
+
data.tar.gz: 75b733317f1c3c992be17d0625e08c09aacea395afcfb6ddaca32cc2b2970db38ba6691adb730866c233945bc0bd0cb72d61b48eef18a785f7c1e30a0d509368
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"turbo_frame_src" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["alexis.crozier@spendhq.com"](mailto:"alexis.crozier@spendhq.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexis Crozier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# TurboFrameSrc
|
|
2
|
+
|
|
3
|
+
`TurboFrameSrc` gives your Rails app back a piece of information Turbo doesn't expose natively: **the original `src` of the turbo-frame that triggered the current request.**
|
|
4
|
+
|
|
5
|
+
## The problem
|
|
6
|
+
|
|
7
|
+
Imagine a common Hotwire pattern: a lazy-loaded `turbo-frame` renders a filterable, paginated collection.
|
|
8
|
+
|
|
9
|
+
```erb
|
|
10
|
+
<%= turbo_frame_tag "orders", src: orders_path %>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Filtering is implemented once, generically, through a shared concern (e.g. a `Filterable` concern included in several controllers). That concern also exposes a generic `reset_filters` action so users can clear the filters that apply to whatever controller/action they're currently on — without wiping out unrelated filters living elsewhere on the page.
|
|
14
|
+
|
|
15
|
+
To reset the filters "in place" and re-render the same frame, that generic action needs to know **which URL originally loaded the frame** (`orders_path`, in the example above) so it can redirect or respond with the right content. Turbo does not give you this for free:
|
|
16
|
+
|
|
17
|
+
- The current request's URL is the *action's* URL (e.g. `/orders/reset_filters`), not the frame's original `src`.
|
|
18
|
+
- The `Turbo-Frame` request header only tells you *which frame* is requesting the update, not what URL it was first loaded from.
|
|
19
|
+
|
|
20
|
+
Once your filters are reset, you're stuck without knowing where to send the user back to.
|
|
21
|
+
|
|
22
|
+
## The solution
|
|
23
|
+
|
|
24
|
+
`TurboFrameSrc` tracks each turbo-frame's initial `src` on the client, forwards it as a request header on every subsequent Turbo Frame fetch, and echoes it back so your Rails controllers/views can read it at any point in the request cycle — including from a generic, shared action like `reset_filters`.
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
class FiltersController < ApplicationController
|
|
28
|
+
include Filterable
|
|
29
|
+
|
|
30
|
+
def reset_filters
|
|
31
|
+
reset_filters_for(params[:scope])
|
|
32
|
+
|
|
33
|
+
redirect_to turbo_frame_src
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```erb
|
|
39
|
+
<%= turbo_frame_tag "orders", src: turbo_frame_src || orders_path %>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## How it works
|
|
43
|
+
|
|
44
|
+
1. **JavaScript** listens for Turbo's `turbo:before-fetch-request` event. The first time a given `turbo-frame` fires a request, its current `src` (or the page URL, if it has none yet) is stashed in a `data-initial-src` attribute on the frame element, then sent as the `X-Turbo-Frame-Src` request header on this and every following fetch initiated by that frame.
|
|
45
|
+
2. **Rack middleware** (`TurboFrameSrc::Middleware`) reads the incoming `X-Turbo-Frame-Src` header (only for requests that also carry Turbo's own `Turbo-Frame` header) and re-attaches it to the response headers, so it survives redirects and stays available across the request/response cycle.
|
|
46
|
+
3. **A view/controller helper**, `turbo_frame_src`, reads that header back for you:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
module TurboFrameSrc::Helper
|
|
50
|
+
def turbo_frame_src
|
|
51
|
+
request.headers['X-Turbo-Frame-Src']
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Because the whole flow is generic (frame name agnostic), it works the same way no matter which lazy-loaded frame, controller, or action is involved — which is exactly what a shared/generic `reset_filters` action needs.
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
Add the gem to your `Gemfile`:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
bundle add turbo_frame_src
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Ruby / Rails side
|
|
67
|
+
|
|
68
|
+
The gem ships a Rails engine. Simply requiring it (via Bundler) is enough to:
|
|
69
|
+
|
|
70
|
+
- register `TurboFrameSrc::Middleware` in your middleware stack,
|
|
71
|
+
- include `TurboFrameSrc::Helper` in `ActionController::Base` (available in controllers and views),
|
|
72
|
+
- register the JS package path with **Importmap**, if your app uses it.
|
|
73
|
+
|
|
74
|
+
### JavaScript side
|
|
75
|
+
|
|
76
|
+
The client-side behavior is published to npm as `turbo_frame_src`.
|
|
77
|
+
|
|
78
|
+
#### Importmap
|
|
79
|
+
|
|
80
|
+
If your app uses `importmap-rails`, the engine already added the gem's JS path to the importmap. Just pin it:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
bin/importmap pin turbo_frame_src
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Then register it in your entrypoint:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
import "turbo_frame_src"
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Yarn / npm / Vite
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
yarn add turbo_frame_src
|
|
96
|
+
# or
|
|
97
|
+
npm install turbo_frame_src
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import { registerTurboFrameSrc } from "turbo_frame_src"
|
|
102
|
+
|
|
103
|
+
registerTurboFrameSrc()
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
(Importing the package directly, without calling `registerTurboFrameSrc`, also works — the listener self-registers as soon as the module is loaded in a browser context.)
|
|
107
|
+
|
|
108
|
+
## Usage
|
|
109
|
+
|
|
110
|
+
Once installed, use the `turbo_frame_src` helper anywhere you have access to the current `request` — controllers, views, or a shared concern/action — to retrieve the `src` the enclosing turbo-frame was originally loaded from:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
turbo_frame_src # => "/orders?status=pending"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Typical use case: a generic `reset_filters` action, shared across several controllers via a concern, that needs to redirect back to whichever frame (or page) initiated the request:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
module Filterable
|
|
120
|
+
extend ActiveSupport::Concern
|
|
121
|
+
|
|
122
|
+
def reset_filters
|
|
123
|
+
session.delete(filters_session_key)
|
|
124
|
+
|
|
125
|
+
redirect_to turbo_frame_src || request.referer
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Development
|
|
131
|
+
|
|
132
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
133
|
+
|
|
134
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
135
|
+
|
|
136
|
+
## Contributing
|
|
137
|
+
|
|
138
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/AlexisCro/turbo_frame_src. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/AlexisCro/turbo_frame_src/blob/main/CODE_OF_CONDUCT.md).
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
143
|
+
|
|
144
|
+
## Code of Conduct
|
|
145
|
+
|
|
146
|
+
Everyone interacting in the TurboFrameSrc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/AlexisCro/turbo_frame_src/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function registerTurboFrameSrc() {
|
|
2
|
+
document.addEventListener("turbo:before-fetch-request", (event) => {
|
|
3
|
+
const fetchOptions = event.detail.fetchOptions;
|
|
4
|
+
const targetFrame = event.target.closest("turbo-frame");
|
|
5
|
+
|
|
6
|
+
if (targetFrame) {
|
|
7
|
+
if (!targetFrame.dataset.initialSrc) {
|
|
8
|
+
targetFrame.dataset.initialSrc = targetFrame.getAttribute("src") || window.location.href;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fetchOptions.headers["X-Turbo-Frame-Src"] = targetFrame.dataset.initialSrc;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof window !== "undefined") {
|
|
17
|
+
registerTurboFrameSrc();
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class TurboFrameSrc::Engine < ::Rails::Engine
|
|
4
|
+
isolate_namespace TurboFrameSrc
|
|
5
|
+
|
|
6
|
+
initializer "turbo_frame_src.importmap", before: "importmap" do |app|
|
|
7
|
+
if app.config.respond_to?(:importmap)
|
|
8
|
+
app.config.importmap.paths.add_gem_path "turbo_frame_src", root.join("app/javascript")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
initializer "turbo_frame_src.helpers" do
|
|
13
|
+
ActiveSupport.on_load(:action_controller_base) do
|
|
14
|
+
include TurboFrameSrc::Helper
|
|
15
|
+
|
|
16
|
+
helper TurboFrameSrc::Helper
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
initializer "turbo_frame_src.middleware" do |app|
|
|
21
|
+
app.config.middleware.use TurboFrameSrc::Middleware
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class TurboFrameSrc::Middleware
|
|
4
|
+
def initialize(app)
|
|
5
|
+
@app = app
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def call(env)
|
|
9
|
+
initial_frame_src = env['HTTP_X_TURBO_FRAME_SRC'] if env['HTTP_TURBO_FRAME'].present?
|
|
10
|
+
|
|
11
|
+
status, headers, response = @app.call(env)
|
|
12
|
+
|
|
13
|
+
headers['X-Turbo-Frame-Src'] = initial_frame_src if initial_frame_src&.present?
|
|
14
|
+
|
|
15
|
+
[status, headers, response]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "turbo_frame_src/version"
|
|
4
|
+
require "turbo_frame_src/middleware"
|
|
5
|
+
require "turbo_frame_src/helper"
|
|
6
|
+
require "turbo_frame_src/engine" if defined?(Rails::Engine)
|
|
7
|
+
|
|
8
|
+
module TurboFrameSrc
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
end
|
data/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "turbo_frame_src",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Preserve initial Turbo Frame src during navigation",
|
|
5
|
+
"main": "app/javascript/turbo_frame_src/index.js",
|
|
6
|
+
"module": "app/javascript/turbo_frame_src/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"app/javascript"
|
|
9
|
+
],
|
|
10
|
+
"author": "Ton Nom",
|
|
11
|
+
"license": "MIT"
|
|
12
|
+
}
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: turbo_frame_src
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alexis Crozier
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: See https://github.com/AlexisCro/turbo_frame_src
|
|
13
|
+
email:
|
|
14
|
+
- alexis.crozier@spendhq.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- CHANGELOG.md
|
|
20
|
+
- CODE_OF_CONDUCT.md
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- app/javascript/turbo_frame_src/index.js
|
|
25
|
+
- lib/turbo_frame_src.rb
|
|
26
|
+
- lib/turbo_frame_src/engine.rb
|
|
27
|
+
- lib/turbo_frame_src/helper.rb
|
|
28
|
+
- lib/turbo_frame_src/middleware.rb
|
|
29
|
+
- lib/turbo_frame_src/railtie.rb
|
|
30
|
+
- lib/turbo_frame_src/version.rb
|
|
31
|
+
- package.json
|
|
32
|
+
- sig/turbo_frame_src.rbs
|
|
33
|
+
homepage: https://github.com/AlexisCro/turbo_frame_src
|
|
34
|
+
licenses:
|
|
35
|
+
- MIT
|
|
36
|
+
metadata:
|
|
37
|
+
allowed_push_host: https://rubygems.org
|
|
38
|
+
homepage_uri: https://github.com/AlexisCro/turbo_frame_src
|
|
39
|
+
source_code_uri: https://rubygems.org/
|
|
40
|
+
changelog_uri: https://github.com/AlexisCro/turbo_frame_src/blob/main/CHANGELOG.md
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 3.2.0
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 4.0.10
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: TurboFrameSrc tracks each turbo-frame's initial src on the client, forwards
|
|
58
|
+
it as a request header on every subsequent Turbo Frame fetch, and echoes it back
|
|
59
|
+
so your Rails controllers/views can read it at any point in the request cycle —
|
|
60
|
+
including from a generic, shared action.
|
|
61
|
+
test_files: []
|