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

Update examples with getInitialProps to SSG #11014

Closed
lfades opened this issue Mar 12, 2020 · 37 comments
Closed

Update examples with getInitialProps to SSG #11014

lfades opened this issue Mar 12, 2020 · 37 comments
Assignees
Labels
good first issue Easy to fix issues, good for newcomers
Milestone

Comments

@lfades
Copy link
Member

lfades commented Mar 12, 2020

With the release of Next.js 9.3 we are empowering all Next.js applications to take advantage of the JAMStack, before this, around half of our examples were using getInitialProps for data fetching in pages, and even though we are not removing it any time soon, it's not ideal for most applications. You can read more about this here.

Around half of our examples (110+) are using getInitialProps. and in most of those examples it can be replaced with getStaticProps to take advantage of SSG.

Any updates to take advantage of SSG in current examples using getInitialProps are deeply welcome and will be merged ASAP

PRs for New examples using getStaticProps / getStaticPaths and removal of current examples that are very outdated or not worth upgrading are also very welcome.

Here are some of the examples currently using getInitialProps:

https://github.com/zeit/next.js/tree/canary/examples/amp-first
https://github.com/zeit/next.js/tree/canary/examples/analyze-bundles
https://github.com/zeit/next.js/tree/canary/examples/api-routes
https://github.com/zeit/next.js/tree/canary/examples/auth0
https://github.com/zeit/next.js/tree/canary/examples/blog-starter
https://github.com/zeit/next.js/tree/canary/examples/custom-server-express
https://github.com/zeit/next.js/tree/canary/examples/data-fetch
https://github.com/zeit/next.js/tree/canary/examples/ssr-caching
https://github.com/zeit/next.js/tree/canary/examples/using-router

To find more examples, do a global search inside our examples for getInitialProps, and with the exception of pages/_document.js any other result is one that we don't want!

If an example can only work with getInitialProps, updating it to use getServerSideProps is also welcome.

Update: Please create a PR per example update, this will speed up the review and merge process 🙏

@samao
Copy link

samao commented Mar 13, 2020

This new feature doesn't seem to work with saga and redux now

@timneutkens
Copy link
Member

Updated a bunch of examples in this PR: #11136

@Albert-Gao
Copy link
Contributor

Thanks for the clarification. @lfades

Sorry for the misleading. I meant the original example only doing a getInitialProps at the top level but not at the page level or component level, and it only does getInitialProps if you enable ssr mode.

But your answer makes me think I might misunderstand something here. So, sorry for the noob question, let's say in a typical SaaS case, there is a page, and for each client, the page needs different data.

My question is:

In this case, isn't getInitialProps faster than getStaticProps in terms of the performance?

because getInitialProps just generates things on the fly, which is how SSR works without physically generates something that will be invalidated for a different tenant.

Thanks 😄

@lfades
Copy link
Member Author

lfades commented Jun 9, 2020

@Albert-Gao

In this case, isn't getInitialProps faster than getStaticProps in terms of the performance?

TL;DR: Never.

If a page uses data based on the user, you should do client side rendering with loading states, the typical case is for the page to be pretty much the same, but the data is different, so you output static HTML for the layout, and then make a request and show the data once it's ready. This is usually how pages like Twitter, vercel.com and more do it. It's better to show the client something very fast, and then make a request and let them wait with content (even if it's a loding spinner), that to make then wait in a blank page for a serverless function to return the initial HTML.

With SSG and without getInitialProps / getServerSideProps you always get static content, you never have to wait for something to "generate things on the fly" because it's already generated.

I hope that answers your question, if it's still not clear, go to our Discussions tab, there are multiple conversations about this 👍

@Albert-Gao
Copy link
Contributor

Thanks so much for the clarification!

so you output static HTML for the layout, and then make a request and show the data once it's ready. This is usually how pages like Twitter, vercel.com and more do it.

I will check the discussion tab!

kodiakhq bot pushed a commit that referenced this issue Jun 10, 2020
Related to #11014

The authentication method has been replaced with an improved version of the passport example.
kodiakhq bot pushed a commit that referenced this issue Jun 10, 2020
@Timer Timer modified the milestones: 9.x.x, june 2020 Jun 11, 2020
kodiakhq bot pushed a commit that referenced this issue Jun 11, 2020
darshkpatel pushed a commit to MLH-Fellowship/next.js that referenced this issue Jun 12, 2020
darshkpatel pushed a commit to MLH-Fellowship/next.js that referenced this issue Jun 12, 2020
@Timer Timer closed this as completed Jun 15, 2020
@armouti
Copy link

armouti commented Jun 25, 2020

@lfades
Thanks for your explanation.
Just to clarify, does that mean with ssg you can load initial HTML (a loading skeleton for example) and then run it on the client like an SPA where you populate data with something like useSWR ?
Or are the content of an ssg page fixed ?

rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to [11014](vercel#11014)

Upgraded all the packages from package.json, removed the saga-wrapper package since it is totally outdated to today's use.

I have refactored the whole example using the new API of the next-redux-wrapper package, using their new support of "getStaticProps".

All of the class components were converted to functional, using the new redux hooks API.

If you want me to change anything or you are not satisfied with any given change, I'm open to suggestions.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to [11014](vercel#11014)

1. I have changed the component from class to functional.
2. I have removed the getInitialProps and used getStaticProps instead.
3. I have removed the redundant connect to redux @ the index page, due to the fact that now we can dispatch the action with the required hook.

If you want me to change anything or you are not satisfied with any given change, I'm open to suggestions.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to [11014](vercel#11014)

First and most important, removed the getInitialProps and used getStaticProps.

Then, I refactored Counter, DataList and Examples components.
I have refactored them from class-based components to functional. Also in each component the redux implementation was refactored using the new hooks API, which resulted in ~40% less code.

If you want me to change anything or you are not satisfied with any given change, I'm open to suggestions.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to [11014](vercel#11014)

1. Moved the reducer into the store and created new store file
2. The example was using a server that was no longer available, now it uses JSON placeholder instead.
3. Moved from getInitialProps to getStaticProps
4. Refactored all the classes to functional components, using the new redux hooks API.
5. Upgraded all the packages and using custom redux wrapper instead of next-redux-wrapper, which I have removed from the example.
6. Upgraded all the other packages.

Please, let me know if I should change anything.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
[11014](vercel#11014)

Removed getInitialProps from _app
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to [11014](vercel#11014).

Removed getInitialProps in favor of getStaticProps and getServerSideProps. Refactored one of the components from class to functional. Removed redundant imports. Removed React.FC/FunctionComponent. Added two build files to gitignore.

Let me know if you want something to be changed.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Related to vercel#11014

The setup is very similar to the one used for Apollo examples.

@Joezo I've removed the demo example that you added to the readme (vercel#6482) as it will be outdated after this PR is merged.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
…el#13849)

Related to vercel#11014

The authentication method has been replaced with an improved version of the passport example.
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

No branches or pull requests