Search Engine Optimization (SEO)
SEO can be tedious to do, but SwiftyLaunch for Web comes with a built-in SEO smarts to take care of it for you.
Generate Page Metadata
There is a neat way to define site metadata in Next.js, without having to mess with the head
tag. We simply export a metadata
object from the page we are at.
But since most of the pages the metadata doesn't really change, we have created a convenient way to keep the convenience of exporting the metadata from the page, but also have the ability to override it on a per-page basis.
Default Usage
Simply import getMetadata
from @/lib/getMetadata
and export it as a metadata
variable.
import { getMetadata } from "@/lib/getMetadata";
export const metadata = getMetadata();
export default function Home() {
return <div>Hello World</div>;
}
This will add metadata tags to the page, such as the title, description, open graph image and more.
Overriding Metadata
What if we want to display a different title? We simply pass the value that we want to override to the getMetadata
function.
import { getMetadata } from "@/lib/getMetadata";
export const metadata = getMetadata({ title: "My Custom Title" });
That's it! Now the title of the page will be "My Custom Title" instead of the default one (we default it to the name of the app).
Learn more about metadata in Next.js here (opens in a new tab).
Generate a Site Map
A site map is a list of all the pages on your website. It is used by search engines to index your website and make it easier to find.
To generate a site map, we define a sitemap.ts
file in the root of the project. Next.js will automatically generate an XML site map when building the app (and when deployed) that search engines can pick up.
Learn more about site map generation in Next.js here (opens in a new tab).
Generate a Robots.txt file
A robots.txt file is used to tell search engines which pages they are allowed to index. (Maybe you'd want to disallow some pages from being indexed?)
To generate a robots.txt file, we define a robots.ts
file in the root of the project. Next.js will automatically generate a robots.txt file when building the app. By default we allow
it to index all pages.
Learn more about robots.txt generation in Next.js here (opens in a new tab).