Skip to content

Commit 8736cb4

Browse files
Improve Client-side Storage documentation (#1302)
* Add SessionStorage documentation with comparison table and use cases Co-Authored-By: Alek Petuskey <[email protected]> * Address PR comments: Remove sync parameter from SessionStorage and add Redis expiration use case Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]> Co-authored-by: Alek Petuskey <[email protected]>
1 parent f4ad8de commit 8736cb4

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

docs/api-reference/browser_storage.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,70 @@ rx.button(
142142
)
143143
```
144144

145+
## rx.SessionStorage
146+
147+
Represents a state Var that is stored in sessionStorage in the browser. Similar to localStorage, but the data is cleared when the page session ends (when the browser/tab is closed). Currently only supports string values.
148+
149+
Parameters
150+
151+
- `name`: The name of the storage key on the client side.
152+
153+
```python
154+
class SessionStorageState(rx.State):
155+
# session storage with default settings
156+
s1: str = rx.SessionStorage()
157+
158+
# session storage with custom settings
159+
s2: str = rx.SessionStorage("s2 default")
160+
s3: str = rx.SessionStorage(name="s3")
161+
```
162+
163+
### Session Persistence
164+
165+
SessionStorage data is cleared when the page session ends. A page session lasts as long as the browser is open and survives page refreshes and restores, but is cleared when the tab or browser is closed.
166+
167+
Unlike LocalStorage, SessionStorage is isolated to the tab/window in which it was created, so it's not shared with other tabs/windows of the same origin.
168+
169+
## rx.remove_session_storage
170+
171+
Remove a session storage item from the client's browser.
172+
173+
Parameters
174+
175+
- `key`: The key to remove from session storage.
176+
177+
```python
178+
rx.button(
179+
'Remove Session Storage',
180+
on_click=rx.remove_session_storage('key'),
181+
)
182+
```
183+
184+
This event can also be returned from an event handler:
185+
186+
```python
187+
class SessionStorageState(rx.State):
188+
...
189+
def logout(self):
190+
return rx.remove_session_storage('session_storage_state.s1')
191+
```
192+
193+
## rx.clear_session_storage()
194+
195+
Clear all session storage items from the client's browser. This may affect other
196+
apps running in the same domain or libraries within your app that use session
197+
storage.
198+
199+
```python
200+
rx.button(
201+
'Clear all Session Storage',
202+
on_click=rx.clear_session_storage(),
203+
)
204+
```
205+
145206
# Serialization Strategies
146207

147-
If a non-trivial data structure should be stored in a `Cookie` or `LocalStorage` var it needs to be serialized before and after storing it. It is recommended to use a dataclass for the data which provides simple serialization helpers and works recursively in complex object structures.
208+
If a non-trivial data structure should be stored in a `Cookie`, `LocalStorage`, or `SessionStorage` var it needs to be serialized before and after storing it. It is recommended to use a dataclass for the data which provides simple serialization helpers and works recursively in complex object structures.
148209

149210
```python demo exec
150211
import reflex as rx
@@ -234,3 +295,39 @@ def app_settings_example():
234295
),
235296
)
236297
```
298+
299+
# Comparison of Storage Types
300+
301+
Here's a comparison of the different client-side storage options in Reflex:
302+
303+
| Feature | rx.Cookie | rx.LocalStorage | rx.SessionStorage |
304+
|---------|-----------|----------------|------------------|
305+
| Persistence | Until cookie expires | Until explicitly deleted | Until browser/tab is closed |
306+
| Storage Limit | ~4KB | ~5MB | ~5MB |
307+
| Sent with Requests | Yes | No | No |
308+
| Accessibility | Server & Client | Client Only | Client Only |
309+
| Expiration | Configurable | Never | End of session |
310+
| Scope | Configurable (domain, path) | Origin (domain) | Tab/Window |
311+
| Syncing Across Tabs | No | Yes (with sync=True) | No |
312+
| Use Case | Authentication, Server-side state | User preferences, App state | Temporary session data |
313+
314+
# When to Use Each Storage Type
315+
316+
## Use rx.Cookie When:
317+
- You need the data to be accessible on the server side (cookies are sent with HTTP requests)
318+
- You're handling user authentication
319+
- You need fine-grained control over expiration and scope
320+
- You need to limit the data to specific paths in your app
321+
322+
## Use rx.LocalStorage When:
323+
- You need to store larger amounts of data (up to ~5MB)
324+
- You want the data to persist indefinitely (until explicitly deleted)
325+
- You need to share data between different tabs/windows of your app
326+
- You want to store user preferences that should be remembered across browser sessions
327+
328+
## Use rx.SessionStorage When:
329+
- You need temporary data that should be cleared when the browser/tab is closed
330+
- You want to isolate data to a specific tab/window
331+
- You're storing sensitive information that shouldn't persist after the session ends
332+
- You're implementing per-session features like form data, shopping carts, or multi-step processes
333+
- You want to persist data for a state after Redis expiration (for server-side state that needs to survive longer than Redis TTL)

0 commit comments

Comments
 (0)