L&D June 2021
Steve Olsen
GOALS:
We're proud to see that community continue to grow. Within the last six months alone, we've seen a 50% increase in Next.js downloads on NPM, from 4.1M to 6.2M and the number of homepages using Next.js in the Alexa top 10,000 has grown 50%."
What does this thing do?
v10.x
v11.x
Front end pages (eg. React templates, JAM stack) + Back end routes (eg. Express) aka BFF
Routing (file system based) - /pages + /pages/api
Static site generation (optional) and Server side rendering (optional)
Styling options: CSS-modules ([name].module.css), Sass (.scss, .module.scss), Less (.less, .styl), CSS-in-JS (styled-jsx)
TypeScript support
Native .env variable support
Dynamic module importing (code splitting), eg. https://nextjs.org/docs/advanced-features/dynamic-import#basic-usage
Import path aliasing, eg. https://nextjs.org/docs/advanced-features/module-path-aliases
Custom document manipulation, eg: https://nextjs.org/docs/advanced-features/custom-document
Image optimizations, eg: https://nextjs.org/docs/basic-features/image-optimization
CRA migration, eg: https://nextjs.org/blog/next-11#cra-migration
Front-end pages
React templates
JAM stack
Back-end API routes
like Express, Koa, etc.
/pages << front-end code
/pages/api << back-end code
a.k.a. Back-end for front-end (BFF)
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' });
}Any file inside the folder /pages/api/* is mapped to http /api/* and will be treated as an API endpoint (not a web component).
For example, the following API route http /api/user returns a json response with a status code of 200:
/pages/api/user.js
HTTP GET: mysite.com/api/user
Notes on API:
if (req.method === 'POST') {...}
res.status(code) - Sets the status code. res.json(body) - Sends a JSON response. res.send(body) - Sends a string/object/Buffer response. res.redirect([status,] path) - Redirects to a specified path or URL.
Static sites FTW!
(a.k.a. SSR, Isomorphic)
Static
Server-side
<html>
<html>
+ hydration js
SPA
build
file server / CDN
web server
....JSX...
/* Called at build-time (i.e. STATIC) */
export async function getStaticProps() {...}
export async function getStaticPaths() {...}pages/blog/[id].js
....JSX...
/* Called at run-time (i.e. SSR) */
export async function getServerSideProps() {
...
}-or-
Monkey see, monkey do?
/src/pages/… << MVC controllers
/src/pages/api/… << MVC controllers
/src/ui << web component code/logic
/src/api << api route handler code/logic
/src/common << common libs
*except public pages, try to static render those
More detailed notes on SSR
Give it a try.
Easy to start. Hard to stop.
Next.js fills in what React alone doesn't.
Questions?
Demo: