Skip to content
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

Create with-redux-persist example #7375

Merged
merged 2 commits into from
May 23, 2019
Merged

Conversation

jrusso1020
Copy link
Contributor

@jrusso1020 jrusso1020 commented May 18, 2019

What does this PR do

This commit will add in an example using redux persist so that you
can persist redux state in your next app. We pass in the app component
to the loading PersistGate value, so that we can render our page
on initial render and then redux persist will kick in and rehydrate
afterwards.

This was based on an example started by https://github.com/ashwamegh #6932 and addresses this issue #6852

but that PR was never updated and merged in so I thought I would take a stab at making the change myself. If there is a better way of going about this let me know

This commit will add in an example using redux persist so that you
can persist redux state in your next app. We pass in the app component
to the loading `PersistGate` value, so that we can render our page on
on initial render and then redux persist will kick in and rehydrate
aftewards.

This was based on an example started by @ashwamegh
@jrusso1020 jrusso1020 requested a review from lfades as a code owner May 18, 2019 00:51
@github-actions
Copy link
Contributor

Stats from current PR

Click to expand stats
zeit/next.js canary jrusso1020/next.js with-redux-persist Change
Build Duration 15s 15.6s ⚠️ +591ms
node_modules Size 40.1 MB 40.1 MB ⚠️ +24 B
Total Bundle (main, webpack, commons) Size 209 kB 209 kB
Total Bundle (main, webpack, commons) gzip Size 68.3 kB 68.3 kB
Client _app Size 2.54 kB 2.54 kB
Client _app gzip Size 1.05 kB 1.05 kB
Client _error Size 8.19 kB 8.19 kB
Client _error gzip Size 3.12 kB 3.12 kB
Client pages/index Size 288 B 288 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.83 kB 4.83 kB
Client pages/link gzip Size 2.11 kB 2.11 kB
Client pages/routerDirect Size 411 B 411 B
Client pages/routerDirect gzip Size 296 B 296 B
Client pages/withRouter Size 393 B 393 B
Client pages/withRouter gzip Size 280 B 280 B
Client main Size 23.3 kB 23.3 kB
Client main gzip Size 7.63 kB 7.63 kB
Client commons Size 183 kB 183 kB
Client commons gzip Size 59.4 kB 59.4 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 769 B 769 B
Base Rendered Size 1.34 kB 1.34 kB
Build Dir Size 801 kB 801 kB
Click to expand serverless stats
zeit/next.js canary jrusso1020/next.js with-redux-persist Change
Build Duration 16.3s 16.4s ⚠️ +170ms
node_modules Size 40.1 MB 40.1 MB ⚠️ +24 B
Total Bundle (main, webpack, commons) Size 209 kB 209 kB
Total Bundle (main, webpack, commons) gzip Size 68.3 kB 68.3 kB
Client _app Size 2.54 kB 2.54 kB
Client _app gzip Size 1.05 kB 1.05 kB
Client _error Size 8.19 kB 8.19 kB
Client _error gzip Size 3.12 kB 3.12 kB
Client pages/index Size 288 B 288 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.83 kB 4.83 kB
Client pages/link gzip Size 2.11 kB 2.11 kB
Client pages/routerDirect Size 411 B 411 B
Client pages/routerDirect gzip Size 296 B 296 B
Client pages/withRouter Size 393 B 393 B
Client pages/withRouter gzip Size 280 B 280 B
Client main Size 23.3 kB 23.3 kB
Client main gzip Size 7.63 kB 7.63 kB
Client commons Size 183 kB 183 kB
Client commons gzip Size 59.4 kB 59.4 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 769 B 769 B
Serverless pages/link Size 342 kB 342 kB
Serverless pages/link gzip Size 88.7 kB 88.7 kB
Serverless pages/index Size 333 kB 333 kB
Serverless pages/index gzip Size 86.3 kB 86.3 kB ⚠️ +1 B
Serverless pages/_error Size 334 kB 334 kB
Serverless pages/_error gzip Size 86.1 kB 86.1 kB
Serverless pages/routerDirect Size 333 kB 333 kB
Serverless pages/routerDirect gzip Size 86.3 kB 86.3 kB
Serverless pages/withRouter Size 333 kB 333 kB
Serverless pages/withRouter gzip Size 86.4 kB 86.4 kB
Build Dir Size 2.37 MB 2.37 MB

@jrusso1020
Copy link
Contributor Author

@lfades when you get a chance if you could let me know what you think that would be great!

@lfades
Copy link
Member

lfades commented May 22, 2019

👋 @jrusso1020 thank your for the PR , I ran the example locally, and I don't know what should I expect, Once the page was ready I activated offline mode in devtools, the page just shows the usual "no connection dinosaur", then I switched to slow 3G, and I was expecting the page to use the data from localStorage while the 10s request ends, but this is what I saw instead:
image

@jrusso1020
Copy link
Contributor Author

@lfades This is more just to have the infrastructure of redux-persist in the application and have the application still rendered our while you're waiting for the whole request to complete. If you wanted to you could retrieve the information from local storage yourself and use it right away(instead of waiting for it to be in redux and getting it from there), but redux-persist won't kick in and rehydrate the redux state until the initial request for the next application is complete.

If that makes sense.

An example of this that we are using currently is that we have an ecommerce shop that was originally written as a client side react.js application. We have now since started a Next.js application to SSR parts of our eccomerce shop to increase SEO and page load speeds. We have to do this in pieces though as it is a big project. The two applications need to be able to share state between them so that if you navigate between them we don't have to make more requests to get the current state we can just pass it through local storage persisted redux state. (the applications live on the same domain behind a HTTP Proxy)

Copy link
Member

@lfades lfades left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrusso1020 Makes sense, thank you for the PR 💯

@github-actions
Copy link
Contributor

Stats from current PR

Click to expand stats
zeit/next.js canary jrusso1020/next.js with-redux-persist Change
Build Duration 14.5s 14s -448ms
node_modules Size 44.8 MB 44.8 MB ⚠️ +24 B
Total Bundle (main, webpack, commons) Size 209 kB 209 kB
Total Bundle (main, webpack, commons) gzip Size 68.5 kB 68.5 kB
Client _app Size 2.92 kB 2.92 kB
Client _app gzip Size 1.2 kB 1.2 kB
Client _error Size 8.23 kB 8.23 kB
Client _error gzip Size 3.13 kB 3.13 kB
Client pages/index Size 288 B 288 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.76 kB 4.76 kB
Client pages/link gzip Size 2.06 kB 2.06 kB
Client pages/routerDirect Size 411 B 411 B
Client pages/routerDirect gzip Size 296 B 296 B
Client pages/withRouter Size 393 B 393 B
Client pages/withRouter gzip Size 280 B 280 B
Client main Size 23.3 kB 23.3 kB
Client main gzip Size 7.74 kB 7.74 kB
Client commons Size 182 kB 182 kB
Client commons gzip Size 59.4 kB 59.4 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 769 B 769 B
Base Rendered Size 1.34 kB 1.34 kB
Build Dir Size 804 kB 804 kB
Click to expand serverless stats
zeit/next.js canary jrusso1020/next.js with-redux-persist Change
Build Duration 15.6s 15.3s -351ms
node_modules Size 44.8 MB 44.8 MB ⚠️ +24 B
Total Bundle (main, webpack, commons) Size 209 kB 209 kB
Total Bundle (main, webpack, commons) gzip Size 68.5 kB 68.5 kB
Client _app Size 2.92 kB 2.92 kB
Client _app gzip Size 1.2 kB 1.2 kB
Client _error Size 8.23 kB 8.23 kB
Client _error gzip Size 3.13 kB 3.13 kB
Client pages/index Size 288 B 288 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.76 kB 4.76 kB
Client pages/link gzip Size 2.06 kB 2.06 kB
Client pages/routerDirect Size 411 B 411 B
Client pages/routerDirect gzip Size 296 B 296 B
Client pages/withRouter Size 393 B 393 B
Client pages/withRouter gzip Size 280 B 280 B
Client main Size 23.3 kB 23.3 kB
Client main gzip Size 7.74 kB 7.74 kB
Client commons Size 182 kB 182 kB
Client commons gzip Size 59.4 kB 59.4 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 769 B 769 B
Serverless pages/link Size 349 kB 349 kB
Serverless pages/link gzip Size 90.3 kB 90.3 kB ⚠️ +2 B
Serverless pages/index Size 340 kB 340 kB
Serverless pages/index gzip Size 87.9 kB 87.9 kB ⚠️ +2 B
Serverless pages/_error Size 340 kB 340 kB
Serverless pages/_error gzip Size 87.7 kB 87.7 kB ⚠️ +2 B
Serverless pages/routerDirect Size 341 kB 341 kB
Serverless pages/routerDirect gzip Size 88 kB 88 kB ⚠️ +1 B
Serverless pages/withRouter Size 341 kB 341 kB
Serverless pages/withRouter gzip Size 88 kB 88 kB ⚠️ +1 B
Build Dir Size 2.41 MB 2.41 MB

@lfades lfades merged commit ae8363c into vercel:canary May 23, 2019
@jrusso1020 jrusso1020 deleted the with-redux-persist branch May 23, 2019 20:48
@vercel vercel locked as resolved and limited conversation to collaborators Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants