m1m02530125 Claude Sonnet 4.6 commited on
Commit
6f5f957
·
1 Parent(s): 8fd6b21

fix: generate page stale needs_regen due to bfcache

Browse files

Two-pronged fix:
1. Cache-Control: no-store on generate index response prevents the page
from being stored in bfcache at all.
2. updateChecklist() now fetches fresh data from /generate/checklist/<id>
on every call (AJAX), and on pageshow when e.persisted=true (bfcache
restore). Falls back to embedded data if fetch fails.
3. New /generate/checklist/<period_id> endpoint returns fresh DB data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. routes/generate.py +27 -2
  2. templates/generate/index.html +27 -6
routes/generate.py CHANGED
@@ -1,6 +1,6 @@
1
  import logging
2
  import threading
3
- from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app, jsonify
4
  from db import repositories as repo
5
  from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
6
  from utils.solver_logger import logger, log_path
@@ -30,13 +30,38 @@ def index():
30
  "gen_status": gen["status"],
31
  "needs_regen": gen["needs_regen"],
32
  }
33
- return render_template(
34
  "generate/index.html",
35
  periods=periods,
36
  period_checklist=period_checklist,
37
  result=None,
38
  priority_scale=PRIORITY_SCALE,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  )
 
 
 
 
 
 
 
 
40
 
41
 
42
  @bp.post("/run")
 
1
  import logging
2
  import threading
3
+ from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app, jsonify, make_response
4
  from db import repositories as repo
5
  from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
6
  from utils.solver_logger import logger, log_path
 
30
  "gen_status": gen["status"],
31
  "needs_regen": gen["needs_regen"],
32
  }
33
+ resp = make_response(render_template(
34
  "generate/index.html",
35
  periods=periods,
36
  period_checklist=period_checklist,
37
  result=None,
38
  priority_scale=PRIORITY_SCALE,
39
+ ))
40
+ # bfcache を無効化してページ再表示時に古い needs_regen が残らないようにする
41
+ resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate"
42
+ resp.headers["Pragma"] = "no-cache"
43
+ return resp
44
+
45
+
46
+ @bp.get("/checklist/<int:period_id>")
47
+ def checklist(period_id):
48
+ """チェックリストを最新状態で返す軽量 API"""
49
+ employees_count = len(repo.get_all_employees(active_only=True))
50
+ requests_list = repo.get_shift_requests(period_id)
51
+ filled = len({r.employee_id for r in requests_list})
52
+ counts = repo.get_reservation_counts(period_id)
53
+ has_counts = any(
54
+ c.get("breakfast", 0) > 0 or c.get("dinner", 0) > 0
55
+ for c in counts.values()
56
  )
57
+ gen = repo.get_period_gen_status(period_id)
58
+ return jsonify({
59
+ "filled": filled,
60
+ "total": employees_count,
61
+ "has_counts": has_counts,
62
+ "gen_status": gen["status"],
63
+ "needs_regen": gen["needs_regen"],
64
+ })
65
 
66
 
67
  @bp.post("/run")
templates/generate/index.html CHANGED
@@ -152,13 +152,13 @@
152
 
153
  {% block scripts %}
154
  <script>
155
- const CHECKLIST = {{ period_checklist | tojson }};
 
156
 
157
- function updateChecklist(pid) {
158
  const box = document.getElementById('checklist');
159
- if (!pid || !CHECKLIST[pid]) { box.style.display = 'none'; return; }
160
  box.style.display = 'block';
161
- const d = CHECKLIST[pid];
162
 
163
  // 希望シフト
164
  const rEl = document.getElementById('chk-requests');
@@ -181,15 +181,36 @@ function updateChecklist(pid) {
181
  : '予約客数:未入力(増員判定が無効になります)';
182
  cEl.querySelector('span').style.color = ok2 ? '#22c55e' : '#f59e0b';
183
 
184
- // 再生成
185
  const rg = document.getElementById('chk-regen');
186
  rg.style.display = d.needs_regen ? 'flex' : 'none';
187
  }
188
 
189
- // ページ読み込み時にも実行
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  document.addEventListener('DOMContentLoaded', () => {
191
  updateChecklist(document.getElementById('period-sel').value);
192
  });
 
 
 
 
 
 
193
  </script>
194
 
195
  <script>
 
152
 
153
  {% block scripts %}
154
  <script>
155
+ // ページ埋め込みの初期データ(初回表示用)
156
+ let CHECKLIST = {{ period_checklist | tojson }};
157
 
158
+ function applyChecklist(pid, d) {
159
  const box = document.getElementById('checklist');
160
+ if (!pid || !d) { box.style.display = 'none'; return; }
161
  box.style.display = 'block';
 
162
 
163
  // 希望シフト
164
  const rEl = document.getElementById('chk-requests');
 
181
  : '予約客数:未入力(増員判定が無効になります)';
182
  cEl.querySelector('span').style.color = ok2 ? '#22c55e' : '#f59e0b';
183
 
184
+ // 再生成フラグ
185
  const rg = document.getElementById('chk-regen');
186
  rg.style.display = d.needs_regen ? 'flex' : 'none';
187
  }
188
 
189
+ function updateChecklist(pid) {
190
+ if (!pid) { document.getElementById('checklist').style.display = 'none'; return; }
191
+ // サーバーから最新データを取得(bfcache や古いデータを回避)
192
+ fetch(`/generate/checklist/${pid}`)
193
+ .then(r => r.json())
194
+ .then(d => {
195
+ CHECKLIST[pid] = d;
196
+ applyChecklist(pid, d);
197
+ })
198
+ .catch(() => {
199
+ // フォールバック:ページ埋め込みデータで表示
200
+ applyChecklist(pid, CHECKLIST[pid]);
201
+ });
202
+ }
203
+
204
+ // 初回・bfcache から復元されたとき両方で最新化
205
  document.addEventListener('DOMContentLoaded', () => {
206
  updateChecklist(document.getElementById('period-sel').value);
207
  });
208
+ window.addEventListener('pageshow', e => {
209
+ if (e.persisted) {
210
+ // bfcache から復元された場合は必ず再取得
211
+ updateChecklist(document.getElementById('period-sel').value);
212
+ }
213
+ });
214
  </script>
215
 
216
  <script>