Vercel is a cloud platform for static sites and Serverless Functions...
It enables developers to host Jamstack websites ...
— Mathias Biilmann (CEO & Co-founder of Netlify).
client-side JavaScript
reusable APIs
(third-party APIs)
prebuilt Markup
(static site generators)
Traditional Web
Jamstack
Technologies in the stack include JavaScript frameworks, Static Site Generators, Headless CMSs, and CDNs.
release date
major releases
github stars
pages
pages
about.js
pages
about.js
book
[id].js
pages
about.js
book
[id].js
function About() {
return <h1>About page</h1>
}
pages
about.js
book
[id].js
function About() {
return <h1>About page</h1>
}
export default About;
Static HTML at build time
pages
books.js
function Books({ books }) {
// render books
}
// This function gets called at build time
export async function getStaticProps(context) {
// Call an external API endpoint to get posts
const books = await Promise.resolve([1,2,3])
// By returning { props: { books } }, the Books component
// will receive `books` as a prop at build time
return {
props: {
books,
},
}
}
export default Books;
Your page content depends on the external data
pages
books.js
function Book({ book }) {
// render a book
}
export async function getStaticPaths() {
// Call an external API endpoint to get books
const books = await Promise.resolve([{id: '1'}, {id: '2'}])
// Get the paths we want to pre-render based on books
const paths = books.map((book) => ({params: { id: book.id }}))
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false }
}
// This also gets called at build time
export async function getStaticProps({ params }) {
// params contains the book `id`.
// If the route is like /book/1, then params.id is 1
const book = await Promise.resolve(`Book #${params.id}`)
// Pass post data to the page via props
return { props: { book } }
}
export default Book
Your page paths depends on the external data
book
[id].js
Use Static Generation (with and without data) whenever possible (build once and served by CDN)
Good for: Marketing pages, Blog posts, E-commerce product listings, Help and documentation
"Can I pre-render this page ahead of a user's request?"
pages
books.js
function Page({ data }) {
// Render data...
}
// This gets called on every request
export async function getServerSideProps(context) {
// Fetch data from external API
const res = await fetch(`https://.../data`)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
export default Page
The page HTML is generated on each request.
import useSWR from 'swr'
function Profile() {
const { data, error } = useSWR('/api/user', fetch)
if (error) return <div>failed to load</div>
if (!data) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
For example, the following API route pages/api/user.js handles a json response:
export default function handler(req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ name: 'John Doe' }))
}
Any file inside the folder pages/api is mapped to /api/*
For example, the API route `pages/api/post/[pid].js` has the following code:
export default function handler(req, res) {
const {
query: { pid },
} = req
res.end(`Post: ${pid}`)
}
Now, a request to `/api/post/abc` will respond with the text: `Post: abc`.
API routes provide built in middlewares which parse the incoming request (req). Those middlewares are:
Adding a Global Stylesheet
import '../styles.css'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
Also supports: CSS Modules, Sass, Less, CSS-in-JS
The Automatic Image Optimization allows for resizing, optimizing, and serving images in modern formats like WebP when the browser supports it. This avoids shipping large images to devices with a smaller viewport.
import Image from 'next/image'
<Image
src="/me.png"
alt="Picture of the author"
width={500}
height={500}
/>
Resizing, optimizing, and serving images in modern formats like WebP when the browser supports it.
Images are lazy loaded by default.
Questions?