-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Stats: Introduce LocalStorage helper for cache values #97560
base: trunk
Are you sure you want to change the base?
Conversation
Jetpack Cloud live (direct link)
Automattic for Agencies live (direct link)
|
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~226 bytes added 📈 [gzipped])
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to. Async-loaded Components (~202 bytes added 📈 [gzipped])
React components that are loaded lazily, when a certain part of UI is displayed for the first time. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
This PR modifies the release build for the following Calypso Apps: For info about this notification, see here: PCYsg-OT6-p2
To test WordPress.com changes, run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit leery of using a Singleton if it can be avoided but I'm not sure what best practice would be in React. I do think we could probably handle this with standard useState()
hooks though. What do you think about the approach here?
https://stackoverflow.com/questions/71402674/whats-the-best-way-to-set-localstorage-in-react
I'd also like it if the state was kept together in a single object. I think that makes it easier to inspect/debug and delete if needed. We know it goes together if it's recorded as a single object in storage.
That would be a concise approach if our LocalStorage usages were all located in functional components. However, we still use the hybrid React approaches. |
return this.cache.get( key ); | ||
} | ||
|
||
const value = localStorage.getItem( key ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we probably handle the possible exceptions here to make it more robust?
} | ||
} | ||
|
||
export default LocalStorageHelper.getInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like the singleton is not necessary here as all references would be actually the same object even it'd be imported in several places.
Should the following code do the same?
export default new LocalStorageHelper();
I shamelessly asked ChatGPT about it. Here's its recommended solution, which makes sense to me 😄 import { useState, useEffect } from 'react';
const useLocalStorage = (key, initialValue) => {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.error('Error reading localStorage key:', key, error);
return initialValue;
}
});
useEffect(() => {
try {
localStorage.setItem(key, JSON.stringify(storedValue));
} catch (error) {
console.error('Error setting localStorage key:', key, error);
}
}, [key, storedValue]);
return [storedValue, setStoredValue];
};
export default useLocalStorage; import React from 'react';
import useLocalStorage from './useLocalStorage';
const MyComponent = () => {
const [value, setValue] = useLocalStorage('myKey', '');
return (
<div>
<input
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Type something..."
/>
</div>
);
};
export default MyComponent; |
return null; | ||
} | ||
|
||
public setItem( key: string, value: string ): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might want to serialize the value and provide a more general interface with for example, JSON.stringify
.
@kangzj Thanks for the suggestion. Is there a way to apply the solution you mentioned to our Traffic |
@@ -0,0 +1,46 @@ | |||
class LocalStorageHelper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helper is useful even with the approach suggested by AI, so I think it still makes sense to address the feedback and move it forward 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's its recommended solution
The state passing from the hook is not updated when the state is set to a new value. The value
remains the original state even after setValue
is executed. The saved preference will be applied until a whole page reloads.
2024-12-31.3.08.17.mov
After tackling some code to experiment with the concept, the state would not be shared between components applying the hook, which would remain the original value until the next initiation of the parent component. cc @a8ck3n @kangzj |
The state passing from the hook is not updated when set to a new value. The 2024-12-31.3.08.17.movYou can reproduce the behavior in the recoding using this branch. |
Related to #97336 (comment)
Proposed Changes
LocalStorageHelper
to cache stored period and date range shortcut values, which will prevent frequently fetching items from localStorage when re-rendering.Why are these changes being made?
Testing Instructions
getItem
access is highly reduced.Pre-merge Checklist