There are many status codes, but only a handful are meaningful in an SEO context.
This is the default code that will be set when Next.js renders a page successfully.
Indicates that the resource requested has been definitively moved to the destination URL (a permanent redirect).
You can trigger a 308 redirect in Next.js by returning a redirect instead of props in the getStaticProps()
function.
// pages/about.js
export async function getStaticProps(context) {
return {
redirect: {
destination: '/',
permanent: true, // triggers 308
},
};
}
You can also use the permanent: true key in redirects set in next.config.js.
// next.config.js
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true, // triggers 308
},
];
},
};
Next.js will automatically return a 404 status code for URLs that do not exist in your application. In some cases, you might also want to return a 404 status code from page (for example with dynamic routes using fallbacks). You can do this by returning the following in place of props:
export async function getStaticProps(context) {
return {
notFound: true, // triggers 404
};
}
Next.js will automatically return a 500 status code for an unexpected application error.
It's recommended to return this status code when your website is down and you predict that the website will be down by an extended period of time. This prevents losing rankings on a long-term basis.
Todo...
https://nextjs.org/learn/seo/crawling-and-indexing/robots-txt
The above info is from the old tutorial that uses Page Router. This all needs to be translated to App Router. The following is App Router stuff.
For most meta data see nextjs_app_router.md.
A custom image when the url is shared.
Place a png|jpeg|gif
image called opengraph-image.png
in your app
directory and to any route segment. This will get added as the meta tags like:
<meta property="og:image" content="" />
Note you can also create this as a .js
file: see the open graph protocol.
Generate site maps at build time.
Create app/sitemap.js
and export
and async
function called sitemap
that returns an array of objects that contain a url and last modified. This page will be read by web crawlers and be available at http://localhost:3000/sitemap.xml
. For example:
export default async function sitemap() {
// List regular routes
const pages = ['', 'about', 'color', 'login', 'signup'];
// Also fetch any dynamic routes
const res = await fetch(url);
const posts = await res.json()
// Create arrays:
const dynamicRoutes = posts.map((post) => {
return {
url: `http://localhost:3000/post/${post.id}`,
lastModified: new Date().toISOString()
}
});
const routes = pages.map((route) => {
return {
url: `http://localhost:3000/${route}`,
lastModified: new Date().toISOString()
}
});
return [...routes, ...dynamicRoutes];
}