Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt out of restoring placeholders for a promoted TF navigation #181

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion javascript/elements/futurism_utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global IntersectionObserver, CustomEvent, setTimeout */

const dispatchAppearEvent = (entry, observer = null) => {
if (!window.Futurism) {
if (!window.Futurism?.subscription) {
return () => {
setTimeout(() => dispatchAppearEvent(entry, observer)(), 1)
}
Expand Down
23 changes: 23 additions & 0 deletions javascript/elements/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ const cachePlaceholders = e => {
}

const restorePlaceholders = e => {
// we have to opt out of this if the request leading to this is a TF request
// if the TF request has been promoted to an advance action
// (data-turbo-action="advance"), this callback will fire inadvertently
// but the whole page will not be exchanged as in a regular TD visit
if (sessionStorage.getItem('requested-turbo-frame')) {
delete sessionStorage.removeItem('requested-turbo-frame')
julianrubisch marked this conversation as resolved.
Show resolved Hide resolved
return
}

const inNamespace = ([key, _payload]) => key.startsWith('futurism-')
Object.entries(sessionStorage)
.filter(inNamespace)
Expand All @@ -72,6 +81,15 @@ const restorePlaceholders = e => {
})
}

const storeRequestedTurboFrame = e => {
const { headers } = e.detail.fetchOptions

if (!headers['Turbo-Frame'] || headers['X-Sec-Purpose'] === 'prefetch') return

// we store the frame ID in case the incoming request was referencing one
sessionStorage.setItem('requested-turbo-frame', headers['Turbo-Frame'])
}

export const initializeElements = () => {
polyfillCustomElements()
document.addEventListener('DOMContentLoaded', defineElements)
Expand All @@ -80,4 +98,9 @@ export const initializeElements = () => {
document.addEventListener('turbolinks:load', defineElements)
document.addEventListener('turbolinks:before-cache', restorePlaceholders)
document.addEventListener('cable-ready:after-outer-html', cachePlaceholders)

document.addEventListener(
'turbo:before-fetch-request',
storeRequestedTurboFrame
)
}
4 changes: 3 additions & 1 deletion javascript/futurism_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const debounceEvents = (callback, delay = 20) => {
export const createSubscription = consumer => {
consumer.subscriptions.create('Futurism::Channel', {
connected () {
window.Futurism = this
window.Futurism = {
subscription: this
}
document.addEventListener(
'futurism:appear',
debounceEvents(events => {
Expand Down
Loading