Book Image

Next.js Quick Start Guide

By : Kirill Konshin
Book Image

Next.js Quick Start Guide

By: Kirill Konshin

Overview of this book

Next.js is a powerful addition to the ever-growing and dynamic JavaScript world. Built on top of React, Webpack, and Babel, it is a minimalistic framework for server-rendered universal JavaScript applications. This book will show you the best practices for building sites using Next. js, enabling you to build SEO-friendly and superfast websites. This book will guide you from building a simple single page app to a scalable and reliable client-server infrastructure. You will explore code sharing between client and server, universal modules, and server-side rendering. The book will take you through the core Next.js concepts that everyone is talking about – hot reloading, code splitting, routing, server rendering, transpilation, CSS isolation, and more. You will learn ways of implementing them in order to create your own universal JavaScript application. You will walk through the building and deployment stages of your applications with the JSON API,customizing the confguration, error handling,data fetching, deploying to production, and authentication.
Table of Contents (9 chapters)

Using the Apollo framework with Next.js to fetch data

Notice the bulkiness of the previous example. Even if we do not take into account the client/server code, we still end up with an inconvenient build process. What if we could make an app that will not require any build steps, precompilation, Babel plugins, and so on?

Recently, the Apollo Framework has received a lot of traction, and of course it has integration with Next.js.

All we need to do is to install a few packages:

$ npm i apollo-link-http next-apollo react-apollo graphql-tag --save

Then, we need to create a preconfigured HOC that we will attach to our pages:

import {withData} from 'next-apollo'
import {HttpLink} from 'apollo-link-http'

const config = {
link: new HttpLink({
uri: 'https://swapi.graph.cool',
opts: {
credentials: 'same-origin'
}
...