Skip to content

Commit

Permalink
fix: made hsr self-clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 29, 2022
1 parent 9805a7b commit 1936b62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/perseus/src/state/freeze_idb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
///
Expand Down
8 changes: 8 additions & 0 deletions packages/perseus/src/state/hsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1936b62

Please sign in to comment.