Skip to content

Commit be948c7

Browse files
Refresh by replacing document body instead of using header
Co-authored-by: Étienne Barrié <[email protected]>
1 parent 77ade6d commit be948c7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

app/controllers/maintenance_tasks/application_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class ApplicationController < ActionController::Base
1414
"'sha384-" \
1515
"MJ6MP7wjBI68eI1gLBSKU86HYHooeOnBdlmsV/RfzritEYu5Xaa1vROm+3dkbeZt'",
1616
)
17+
policy.script_src(
18+
# page refresh script
19+
"'sha256-fADwJN++FN9LuhJ4XRCiIoJ7KPwmDYx2qiBgm20D8rA='",
20+
)
1721
policy.frame_ancestors(:self)
1822
end
1923

app/views/layouts/maintenance_tasks/application.html.erb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
<%= csrf_meta_tags %>
1616

1717
<%=
18-
stylesheet_link_tag URI.join(controller.class::BULMA_CDN, 'npm/[email protected]/css/bulma.css'),
18+
stylesheet_link_tag(URI.join(controller.class::BULMA_CDN, 'npm/[email protected]/css/bulma.css'),
1919
media: :all,
2020
integrity: 'sha384-Zkr9rpl37lclZu6AwYQZZm0CxiMqLZFiibodW+UXLnAWPBr6qgIzPpcmHkpwnyWD',
21-
crossorigin: 'anonymous'
21+
crossorigin: 'anonymous') if request.headers.key?('Sec-Fetch-User')
2222
%>
2323

2424
<style>
@@ -32,7 +32,19 @@
3232
</style>
3333

3434
<% if defined?(@refresh) %>
35-
<meta http-equiv="refresh" content="<%= @refresh %>">
35+
<script>
36+
function refresh() {
37+
fetch(document.location).then(
38+
async response => {
39+
const text = await response.text()
40+
const newDocument = new DOMParser().parseFromString(text, "text/html")
41+
document.body.replaceWith(newDocument.body)
42+
},
43+
error => location.reload()
44+
)
45+
}
46+
window.setInterval(refresh, <%= @refresh * 1000 %>)
47+
</script>
3648
<% end %>
3749
</head>
3850

0 commit comments

Comments
 (0)