WhoBuilt

10 Next.js Tips You Might Not Know!

Jan 4, 2021· 8 min· 71K views·Watch on YouTube

Lee Robinson, a software developer at leerob, shares 10 lesser-known Next.js tips in a video on the leerob channel.

Redirects and rewrites

Next.js 9.5 added redirects for sending requests to another path or website, with path and wildcard matching. Robinson uses leerob.io/youtube as an example of an external redirect. Rewrites proxy requests to another destination while keeping the original URL visible, which can help when incrementally moving a site to Next.js.

Preview drafts and customize builds

Preview mode lets editors view draft content from a headless CMS on a statically generated site through a unique preview URL. Robinson also shows how to hook a Node script into the build process to generate a sitemap, RSS feed, or search index.

Tune client code and imports

Robinson describes swapping React for Preact in the client production build to reduce shipped JavaScript, while cautioning that compatibility with newer React features may vary. He also recommends absolute imports and module aliases configured in jsconfig.json or a TypeScript config to simplify import paths.

Build APIs and share page metadata

A single Next.js API route can handle different HTTP methods: his views example increments a count on POST and returns it on GET. He also demonstrates setting Cache-Control headers, which can let a client receive a 304 not modified response. Page components can expose title and description properties for a shared layout in _app.js.

Create mobile-like experiences

Robinson highlights Capacitor.js, created by Ionic's creator, as a way to build mobile-like experiences with Next.js. The example combines Next.js and Tailwind and includes hot module reloading, modals, swipe-to-close, and virtual lists.

Takeaways

  • Use redirects for visible URL changes and rewrites to proxy requests while preserving the URL.
  • Preview mode can make draft content reviewable even when a site is statically generated.
  • Build hooks can automate tasks such as sitemap generation and search indexing.
  • Evaluate Preact against your app's React feature needs and measure the client bundle before adopting it.
  • Handle related HTTP methods in one API route and use cache headers when responses can be reused.

Mentioned in this talk

Search inside this talk

What was claimed 4

  • feature
    Next.js: Built-in redirects and rewrites were added in Next.js 9.5.
    “After Next.js 9.5, redirects were added directly into the framework”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: Preview mode lets users view draft content in a statically generated site.
    “Preview mode solves that by letting you see a statically generated version of the draft content.”
    YouTube
    Unverified1 source· Talk
  • uses
    “You can swap out React with Preact very easily in Next.js.”
    YouTube
    Unverified1 source· Talk
  • feature
    Capacitor.js: Supports native-like app experiences, including hot module reloading with Next.js.
    “You can do things like hot module reloading with Next.js and still get a native-like experience with your app.”
    YouTube
    Unverified1 source· Talk

Related talks