Skip to content
Dashboard

10 Next.js tips you might not know

Techniques from the Next.js experts that empower your application.

Vercel's Lee Robinson gives a quick overview on 10 little known tips on Next.js

Link to headingTip 1: Next.js Redirects

next.config.js
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true,
},
]
},
}

Link to headingTip 2: Next.js Rewrites

next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/about',
destination: '/',
},
]
},
}

Link to headingTip 3: Next.js Preview Mode

Link to headingTip 4: Hooking into the Build Process

Link to headingTip 5: Next.js With Preact

next.config.js
module.exports = {
webpack: (config, { dev }) => {
// Replace React with Preact only in client production build
if (!dev) {
Object.assign(config.resolve.alias, {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat'
});
}
return config;
}
};

Link to headingTip 6: Absolute Imports and Module Path Aliases

import Button from../../../components/button”;

import Button from “@/components/button”;

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/components/*": ["components/*"]
    }
  }
}

Link to headingTip 7: CRUD API Routes

pages/api/item.js
export default async function handler(req, res) {
if (req.method === 'PUT') {
res.status(201).json({});
}
if (req.method === 'GET') {
res.status(200).json({});
}
if (req.method === 'POST') {
res.status(200).json({});
}
if (req.method === 'DELETE') {
res.status(204).json({});
}
}

Link to headingTip 8: Setting Response HTTP Caching Headers

pages/api/user.js
export default function handler(req, res){
  res.setHeader(
    'Cache-Control',
    's-maxage=86400'
  );
  res.status(200).json({name: ‘John Doe’ });
};

Link to headingTip 9: Shared Component Attributes

pages/index.js
Home.title = "test"
Home.description = "A container for blog posts"
export default function Home() {
return (
<Container>
<Blogpost />
</Container>
)
}

pages/_app.js
import Head from 'next/head'
export default function App({ Component, pageProps }) {
return (
<>
<Head>
<title>{Component.title}</title>
<meta name="description" content={Component.description} />
</Head>
<Component {...pageProps} />
</>
)
}

Link to headingTip 10: Next.js Mobile Applications? 

The Mobile Stack Visualized
The Mobile Stack Visualized

Link to headingNext Steps

Ready to deploy?