From 1936b62bdad9ac7cfc799ea3c1648d44165f651e Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Sat, 29 Jan 2022 16:17:34 +1100 Subject: [PATCH] fix: made hsr self-clearing --- packages/perseus/src/state/freeze_idb.rs | 17 +++++++++++++++++ packages/perseus/src/state/hsr.rs | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/packages/perseus/src/state/freeze_idb.rs b/packages/perseus/src/state/freeze_idb.rs index 4b4fa689bd..64d453f0cd 100644 --- a/packages/perseus/src/state/freeze_idb.rs +++ b/packages/perseus/src/state/freeze_idb.rs @@ -134,6 +134,23 @@ impl IdbFrozenStateStore { Ok(()) } + /// Clears the stored frozen state entirely and irrecoverably. + pub async fn clear(&self) -> Result<(), IdbError> { + let transaction = self + .db + .transaction(&["frozen_state"], TransactionMode::ReadWrite) + .map_err(|err| IdbError::TransactionError { source: err })?; + let store = transaction + .store("frozen_state") + .map_err(|err| IdbError::TransactionError { source: err })?; + + store + .clear() + .await + .map_err(|err| IdbError::ClearError { source: err })?; + + Ok(()) + } /// Checks if the storage is persistently stored. If it is, the browser isn't allowed to clear it, the user would have to manually. This doesn't provide a guarantee that all users who've /// been to your site before will have previous state stored, you should assume that they could well have cleared it manually (or with very stringent privacy settings). /// diff --git a/packages/perseus/src/state/hsr.rs b/packages/perseus/src/state/hsr.rs index 3b53de3865..59799435d8 100644 --- a/packages/perseus/src/state/hsr.rs +++ b/packages/perseus/src/state/hsr.rs @@ -57,6 +57,14 @@ pub async fn hsr_thaw(render_ctx: RenderCtx) { Ok(_) => log("State restored."), Err(_) => log("Stored state corrupted, waiting for next code change to override."), }; + + // We don't want this old state to persist if the user manually reloads (they'd be greeted with state that's probably out-of-date) + match idb_store.clear().await { + Ok(_) => (), + Err(_) => { + return; + } + } } /// Thaws a previous state frozen in development.