From 910976b432fbbf4a5ca174e28a437c45731bd7dc Mon Sep 17 00:00:00 2001 From: Julian Rubisch Date: Thu, 27 Jun 2024 15:07:17 +0200 Subject: [PATCH 1/3] fix: Opt out of restoring placeholders for a promoted TF navigation --- javascript/elements/index.js | 23 +++++++++++++++++++++++ javascript/futurism_channel.js | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/javascript/elements/index.js b/javascript/elements/index.js index d87f2ee..0395154 100644 --- a/javascript/elements/index.js +++ b/javascript/elements/index.js @@ -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 (!!window.Futurism.requestedTurboFrame) { + delete window.Futurism.requestedTurboFrame + return + } + const inNamespace = ([key, _payload]) => key.startsWith('futurism-') Object.entries(sessionStorage) .filter(inNamespace) @@ -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 + window.Futurism.requestedTurboFrame = headers['Turbo-Frame'] +} + export const initializeElements = () => { polyfillCustomElements() document.addEventListener('DOMContentLoaded', defineElements) @@ -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 + ) } diff --git a/javascript/futurism_channel.js b/javascript/futurism_channel.js index 35c3c27..76c5aea 100644 --- a/javascript/futurism_channel.js +++ b/javascript/futurism_channel.js @@ -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 => { From a998ebfd99a296be9974cbe928f6ba2b5eb2db72 Mon Sep 17 00:00:00 2001 From: Julian Rubisch Date: Thu, 27 Jun 2024 15:37:23 +0200 Subject: [PATCH 2/3] lint: Remove double negation --- javascript/elements/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/elements/index.js b/javascript/elements/index.js index 0395154..a8f20ea 100644 --- a/javascript/elements/index.js +++ b/javascript/elements/index.js @@ -60,7 +60,7 @@ const restorePlaceholders = e => { // 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 (!!window.Futurism.requestedTurboFrame) { + if (window.Futurism.requestedTurboFrame) { delete window.Futurism.requestedTurboFrame return } From c8c92aa45da5a4ca0fe9fd043ebc01ac0fbba550 Mon Sep 17 00:00:00 2001 From: Julian Rubisch Date: Mon, 8 Jul 2024 13:06:35 +0200 Subject: [PATCH 3/3] chore: Use sessionStorage to store requested turbo frame --- javascript/elements/futurism_utils.js | 2 +- javascript/elements/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/elements/futurism_utils.js b/javascript/elements/futurism_utils.js index eed5c16..d23100d 100644 --- a/javascript/elements/futurism_utils.js +++ b/javascript/elements/futurism_utils.js @@ -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) } diff --git a/javascript/elements/index.js b/javascript/elements/index.js index a8f20ea..cc3077c 100644 --- a/javascript/elements/index.js +++ b/javascript/elements/index.js @@ -60,8 +60,8 @@ const restorePlaceholders = e => { // 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 (window.Futurism.requestedTurboFrame) { - delete window.Futurism.requestedTurboFrame + if (sessionStorage.getItem('requested-turbo-frame')) { + delete sessionStorage.removeItem('requested-turbo-frame') return } @@ -87,7 +87,7 @@ const storeRequestedTurboFrame = e => { if (!headers['Turbo-Frame'] || headers['X-Sec-Purpose'] === 'prefetch') return // we store the frame ID in case the incoming request was referencing one - window.Futurism.requestedTurboFrame = headers['Turbo-Frame'] + sessionStorage.setItem('requested-turbo-frame', headers['Turbo-Frame']) } export const initializeElements = () => {