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

Fix back/forward caching issues on Safari #43

Closed
wants to merge 10 commits into from

Conversation

Benjadahl
Copy link

Turns out issue #42 was cause by the use of BF cahing in Safari:
https://web.dev/bfcache/

BF caching takes the entire JS heap into a cache and reloads this instantly when using the back/forward buttons. The page state is restored from memory instead of being reloaded. This means that our connections would be disconnected when you navigate to a new page, and navigate back again. Example:

  1. Tab has loaded site1.com (A content script is injected with sendMessage calls - which establishes sockets)
  2. Tab load site2.com
  3. Users presses back button to got back to site1.com, so the browser simply puts the old state of site1.com back into memory instead of reloading it. This means that our old sockets wont be intialized, but they have now been disconnected.

Thus the solution is to reconnect them, when the onpageshow event is called, if the persisted field is true.

@zikaari should this event be wrapped in a check for if window is defined in the context? I don't seem to be getting any runtime errors now, but maybe you know of any cases where it would be necessary?

zikaari and others added 10 commits June 28, 2022 17:06
postMessage is now only used for initial handshake, once the ports
are exchanged, the messaging then carries on over MessageChannel
Credits to @gurupras for finding the bug. Original pull request
can be seen here - serversideup#26
Fixes serversideup#23

Each message sent now relies on delivery receipts to ensure they were
indeed delivered AND to expect the destination's response. In case the
destination disconnects before it could have responded, sendMessage
Promise will be rejected with an 'Transaction ended before it could
complete'.

Queued messages are also no longer tracked in the background page, since
it is ephemeral in manifest v3 extensions. Each endpoint now keeps track
of its queued messages, and responses it is waiting on, in its on own
runtime. This new strategy also acts as a safe-guard against
destinations receiving stale messages from endpoints that no longer
exist __(previsouly this could happen as there was no message invalidation
logic for when the sender disconneted)__.

The role of background page is no longer to maintain the "state" of the
system, but to just act as a relay and to inform the relevant endpoints
when the destination they are transacting with have been disconnected.
@zikaari
Copy link
Collaborator

zikaari commented Nov 14, 2022

What do you think of returning the connect from createPersistentPort as an alias like forceReconnect etc. and have content-script.ts do the check and trigger the re-connect if necessary? Just thinking if we can keep the concerns isolated to where they truly belong

@Benjadahl
Copy link
Author

What do you think of returning the connect from createPersistentPort as an alias like forceReconnect etc. and have content-script.ts do the check and trigger the re-connect if necessary? Just thinking if we can keep the concerns isolated to where they truly belong

That would definitely make sense. I can probably take a look at it later this week. :)

@zikaari zikaari deleted the branch serversideup:next April 6, 2023 20:05
@zikaari zikaari closed this Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants