-
Notifications
You must be signed in to change notification settings - Fork 5
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
Ga topic status with localstorage #1604
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 1902036 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Preview URLs:
|
isGlobalNotificationVisible={Boolean(props.globalNotification)} | ||
> | ||
{isClientSide ? ( | ||
<SWRConfig value={{ provider: localStorageProvider }}> |
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.
First doubt. If we want to have the course/topic status available in the sidebar and (potentially in the future) in the actual content page, the only place common between these 2 layouts is the application layout.
Adding cache provider here means that all the usages of swr in the app will be cached in local cache. This might be what we actually want, or might be creating side effects.
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.
#1 I wonder if something like the SWRConfig would not be better placed in gatsby-browser's "wrapXXXElement" hooks. That implicitly makes them client side only and also makes sure they are in all layouts.
#2 having one cache provider for any SWR even across microsites felt weird to me too in the beginning but I concluded that it's the whole point of it. The cache keys are the URLs you use to fetch from SWR and the content is the API response. And we want that information to be available at least in stale state for other pages / views / microsites. So it must be one fixed key like "app-cache" and also at top level for the domain. We could use a more specific localstorage key maybe that is more explanatory.
const map = new Map(JSON.parse(localStorage.getItem('app-cache') || '[]')); | ||
|
||
const handleBeforUnload = (event) => { | ||
const appCache = JSON.stringify(Array.from(map.entries())); |
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.
This handler is rather blind approach as it will simply dump swr state to the cache each time the page is refreshed. But we refresh the page also each time we login and logout.
This is the reason of the 2 custom events that basically disable writing the cache upon login and logout.
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 behavior is nice :-)
Still a slight delay but that is the general hydration of the static page after reloading (it just becomes more visible but is not new, the top meny does not work too in the first second).
isGlobalNotificationVisible={Boolean(props.globalNotification)} | ||
> | ||
{isClientSide ? ( | ||
<SWRConfig value={{ provider: localStorageProvider }}> |
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.
#1 I wonder if something like the SWRConfig would not be better placed in gatsby-browser's "wrapXXXElement" hooks. That implicitly makes them client side only and also makes sure they are in all layouts.
#2 having one cache provider for any SWR even across microsites felt weird to me too in the beginning but I concluded that it's the whole point of it. The cache keys are the URLs you use to fetch from SWR and the content is the API response. And we want that information to be available at least in stale state for other pages / views / microsites. So it must be one fixed key like "app-cache" and also at top level for the domain. We could use a more specific localstorage key maybe that is more explanatory.
If you have time I'd like you to have a look to more specific scenarios like, for example:
I felt something was wrong, but I might be wrong |
@FFawzy could you do me a favor and test through the mentioned scenarios with a diligent look at the status indicators in the navigation? Realistically I would not be able to follow up because I'll only be there for a day next week. |
Notes while testing the preview:
TBC with more test cases |
|
Attempt to use localstorage cache for swr.
I invested some time on it and still has glitches: