bible270 1.3.1 → 1.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e581a537cf6100f7d0012cd3de3de1d24b53d908fadb928ae38f09b4fcd4ef65
4
- data.tar.gz: '025209163a4d12210d243596129b2c440f7ed2096acb8b3d64a88486f960c07d'
3
+ metadata.gz: 1045882b2a544c39d4f9c4b6282c4dd67861f1af380d4af7ad1cd226b57dcaee
4
+ data.tar.gz: 03c9162733bfaf96cc08fcd101231174efbf97378cdefe47e55d91d603c0b6bd
5
5
  SHA512:
6
- metadata.gz: bf4564bfb9c6edfca1bb7b1dbff2bcb93505ae26c04b8a07cfdfe441394e1d5ef757746acc81244c82f70ec5b9ef6498f191c57be537b814d4245492730ec8f3
7
- data.tar.gz: 6e95808d78d6c9224989be8664fcf2ae0e82cc61ae4a0d91ced27c830eebd77daa81596e3741aa63797001c62d50563c3751db12deeb54a36a61860beb5b744f
6
+ metadata.gz: 8bc8c454821233d8c899af8c6224b08d5c48b615ac2c6bbf5dc03140cce041c60c7f1208c78a631576875b3eb3507ca82ae61e2c57a19b5c4fe0e6aab3d30c1d
7
+ data.tar.gz: 735cf1ba5f33e2f56bd87cbd2720eeb6b685822b148b7b2a13fd8e7537dcd63cf73e6253e050445be136087bcacc4802dcda36c939f1080994c2a8733a1f4b78
data/CHANGELOG.md CHANGED
@@ -7,6 +7,7 @@ All notable changes to bible270. Format follows [Keep a Changelog](https://keepa
7
7
  ### Fixed
8
8
 
9
9
  - correct Day 27's New Testament reading to include Matthew 26:75
10
+ - silently recover all Turbo interactions from stale sessions, retry once with a fresh CSRF token, and reload rather than submit with a known-stale token if refreshing fails
10
11
 
11
12
  **[Full Changelog](https://github.com/avonderluft/bible270/compare/v1.3.0...main)**
12
13
 
@@ -116,18 +116,15 @@ module Bible270
116
116
  reason = error ? error.class : 'an unverified request'
117
117
  Rails.logger.info("[bible270] silently refreshing a page after #{reason}")
118
118
 
119
- if stale_turbo_checkoff?
119
+ if request.format.turbo_stream?
120
120
  response.set_header('X-Bible270-Refresh-Session', 'true')
121
+ response.set_header('Cache-Control', 'no-store')
121
122
  head :conflict
122
123
  else
123
124
  redirect_back fallback_location: root_path, allow_other_host: false, status: :see_other
124
125
  end
125
126
  end
126
127
 
127
- def stale_turbo_checkoff?
128
- controller_name == 'checkoffs' && action_name == 'toggle' && request.format.turbo_stream?
129
- end
130
-
131
128
  # Guard participation (checking off / commenting). Viewing is always open.
132
129
  def require_reader!
133
130
  return true if signed_in?
@@ -35,6 +35,7 @@
35
35
  let sessionRefreshedAt = Date.now();
36
36
  let sessionRefresh = null;
37
37
  let hiddenAt = null;
38
+ const submitters = new WeakMap();
38
39
 
39
40
  function updateAuthenticityToken(token) {
40
41
  const meta = document.querySelector("meta[name='csrf-token']");
@@ -97,6 +98,9 @@
97
98
  });
98
99
 
99
100
  document.addEventListener("submit", (event) => {
101
+ const submittedForm = event.target.closest("form");
102
+ if (submittedForm && event.submitter) submitters.set(submittedForm, event.submitter);
103
+
100
104
  const form = event.target.closest("form[data-b270-submit='true']");
101
105
  if (!form) return;
102
106
 
@@ -111,8 +115,7 @@
111
115
  form.requestSubmit(submitter || undefined);
112
116
  }).catch(() => {
113
117
  delete form.dataset.b270AwaitingSession;
114
- sessionRefreshedAt = Date.now();
115
- form.requestSubmit(submitter || undefined);
118
+ window.location.reload();
116
119
  });
117
120
  return;
118
121
  }
@@ -134,17 +137,22 @@
134
137
 
135
138
  function staleSessionResponse(fetchResponse) {
136
139
  if (!fetchResponse) return false;
137
- if (fetchResponse.statusCode === 422) return true;
138
140
 
139
- return fetchResponse.statusCode === 409 &&
140
- fetchResponse.response.headers.get("X-Bible270-Refresh-Session") === "true";
141
+ const response = fetchResponse.response;
142
+ if (fetchResponse.statusCode === 409 &&
143
+ response.headers.get("X-Bible270-Refresh-Session") === "true") return true;
144
+
145
+ // Older deployments can still return Rails' HTML 422 page. Do not confuse
146
+ // an intentional Turbo Stream validation response with an expired session.
147
+ const contentType = response.headers.get("Content-Type") || "";
148
+ return fetchResponse.statusCode === 422 && contentType.includes("text/html");
141
149
  }
142
150
 
143
151
  document.addEventListener("turbo:before-fetch-response", (event) => {
144
152
  const target = event.target;
145
- const form = target.closest && target.closest("form[data-b270-checkoff='true']");
153
+ const form = target.closest && target.closest("form");
146
154
  const response = event.detail.fetchResponse;
147
- if (!form || !staleSessionResponse(response)) return;
155
+ if (!form || form.method.toLowerCase() === "get" || !staleSessionResponse(response)) return;
148
156
 
149
157
  event.preventDefault();
150
158
  if (form.dataset.b270RetriedAfterStaleSession === "true") {
@@ -152,9 +160,12 @@
152
160
  return;
153
161
  }
154
162
 
163
+ const submitter = submitters.get(form);
155
164
  form.dataset.b270RetriedAfterStaleSession = "true";
156
165
  resetForm(form);
157
- refreshSession().then(() => form.requestSubmit()).catch(() => window.location.reload());
166
+ refreshSession()
167
+ .then(() => form.requestSubmit(submitter || undefined))
168
+ .catch(() => window.location.reload());
158
169
  });
159
170
 
160
171
  document.addEventListener("turbo:submit-end", (event) => {
@@ -165,6 +176,7 @@
165
176
  if (form.dataset.b270RetriedAfterStaleSession === "true" &&
166
177
  staleSessionResponse(event.detail.fetchResponse)) return;
167
178
 
179
+ delete form.dataset.b270RetriedAfterStaleSession;
168
180
  if (event.detail.success) {
169
181
  announce(form.dataset.b270Success || "Saved.", "success");
170
182
  } else {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bible270
4
- VERSION = '1.3.1'
4
+ VERSION = '1.3.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bible270
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew vonderLuft