Fetch Data Server Side using withSSRContext

Ali Spittel
InstructorAli Spittel
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

To get our data from AWS, we are going to use withSSRContext. This will fetch our data on the server-side so we won't need to rebuild the site every time we get a new blog and the front end will be more performant. Server-Side Rendering is an abstraction built on top of GraphQL so we don't have to write GraphQL queries.

Instructor: [0:00] I want to render all my blogs on the home page. First, I need a few imports. I'll import withSSRContext from AWS Amplify. I'll also import SerializedModel from AWS Amplify DataStore server-side rendering. Then, I'll import my blog model from my models directory.

[0:29] I'll create a getServerSide props function. This will allow me to perform the data fetching on the server side. We won't need to rebuild the site every time we get a new blog. Also, the frontend will be more performant if we fetch the data on the server side. We'll create an instance of withServerSideContext. This will allow us to interact with AWS Amplify on the server side.

[0:48] I'm going to create a variable for my blog data. Then, I'll add a try-catch block. Inside the try block, I'll query for all my data. We'll be using data store to interact with our data.

[1:05] It leverages offline and online data storage without any additional developer work. It's an abstraction on top of GraphQL so that we don't have to write verbose GraphQL queries. If there's an error, we'll console.log that error. We'll return the blogs as props from this function.

[1:28] We'll use the serialized model function to convert the blog data into a true JSON so we can pass it as a prop to the home component. We'll destructure the blogs from the props object. We'll map through our blogs and render them on the page. The key will be the blog's ID.

[1:47] We'll render the blog name in each header. Now if I view my app in the browser, I'll see all the blog posts that I created. We fetched our data from AWS Amplify using server-side rendering with Next.js. We then displayed our data on the page.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today