-
Notifications
You must be signed in to change notification settings - Fork 189
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
Use swr and reworking Posts, Post, and Search loading #1217
Conversation
I think this is ready for a proper review. I'll explain a bit more about what it does. I'm adding https://swr.vercel.app/, which is a fantastic React Hook to manage your data fetching and caching. The idea is, use cached data if we have it AND go get new data, so you always get fast results. I've refactored Posts and Post, and we now have Posts, Timeline, and Post. I wanted to re-use the logic of how our "posts" are shown for both the main timeline and also for search results. The Post component is now responsible for loading it's own content from the server. It accepts a url to a post on its props, then it manages getting the data and rendering it. This speeds things up by not having so many parallel promises that we have to block on in Posts. I also stripped out the redundant client-side syntax highlighting we were doing--it's all done server side now. The Posts component has been trimmed down a lot. It uses useSWRInfinite to manage loading posts page-by-page. The results are now returned/stored as an array of arrays (e.g., pages of posts). I manage going to the next page using the The Timeline renders pages of posts, and optionally shows a Load More button. It's used by the Posts component and SearchPage, both of which are responsible for the getting the post data to show. If a There's more I could do, but I think this might be good enough to start with, and we can build on it. NOTE: we need better UI error messages, which we can do in follow-ups. |
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.
A much needed refactor. Awesome!
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.
wow, the posts load instantly now O_O
I filed #1220 on the error and loading handling, which I didn't do enough of here. |
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.
Love everything about this
Fixes #1201. I've wanted to try using https://github.com/vercel/swr for a while now, and decided to do an experiment. This PR is not ready, but I wanted to get it build on Vercel so I could see how it works.
This PR does a few things:
swr
do it instead. The way it works is to give you back a cached version of the data while it goes and gets an updated one (then cache that for next time). The 2nd, 3rd, etc. load is extremely fastPost
andPosts
work, so that we delegate more of the loading and don't block for so long. Also, we can useswr
here too, since posts can get cached and re-usedAs I say, this needs more stuff added back (I ripped out most of
Posts
to do this).