Navigating the intricacies of Next.js can be challenging, but understanding how to set up optional dynamic routes with metadata can significantly enhance your web application’s SEO and usability. In this blog post, I’ll guide you through creating a Next.js 14 app with optional dynamic routes and setting up canonical URLs using metadata. By the end, you’ll have a better grasp of handling these aspects in your projects.
Thank me by sharing on Twitter 🙏
Introduction
Optional dynamic routes are crucial for applications requiring flexible URL structures. In my recent project, I needed a route like /company/{id}/{name-slug}
, where the name slug was optional. I also wanted to ensure that the canonical URLs were correctly set for SEO purposes. Here’s how I achieved this in Next.js 14 using TypeScript.
Setting Up the File Structure
First, let’s structure our Next.js 14 app to handle optional dynamic routes. For this example, the file structure under the /app
directory should look like this:
/app/company/[id]/[...name]/page.tsx
This structure indicates that the [id]
segment is required, while the [...name]
segment is optional.
Creating the Page Component
Next, we’ll create our page.tsx
file. This component will render the company information based on the id
and name
parameters from the URL. Here’s how to set it up:
Elebase USB to USB C Adapter 4 Pack,USBC Female to A Male Car Charger,Type C Converter for iPhone 16 Pro Max,15 14 13 12 11 Plus,Apple Watch iWatch 10 9 8,Airpods,iPad Air Mini 6 7,Samsung Galaxy S24
$9.99 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Genesis: Artificial Intelligence, Hope, and the Human Spirit
$21.00 (as of December 21, 2024 19:39 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)iPhone Charger 3Pack 10 FT Apple MFi Certified Lightning Cable Fast Charging iPhone Charger Cord Compatible with iPhone 14 13 12 11 Pro Max XR XS X 8 7 6 Plus SE iPad and More
$7.99 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)import { Metadata } from 'next';
export async function generateMetadata({ params }): Promise<Metadata> {
const { id, name } = params;
const nameSlug = name ? (Array.isArray(name) ? name.join('/') : name) : '';
const canonicalUrl = `https://yourwebsite.com/company/${id}${nameSlug ? `/${nameSlug}` : ''}`;
return {
title: 'Company Page',
alternates: {
canonical: canonicalUrl,
},
};
}
const CompanyPage = ({ params }) => {
const { id, name } = params;
const nameSlug = name ? (Array.isArray(name) ? name.join('/') : name) : '';
return (
<div>
<h1>Company Page</h1>
<p>Company ID: {id}</p>
{nameSlug && <p>Company Name Slug: {nameSlug}</p>}
</div>
);
};
export default CompanyPage;
In this component, we use the params
object to extract the id
and name
from the URL. The nameSlug
is constructed to handle both cases where name
could be an array or a single string. This flexibility is essential for optional segments.
Generating Metadata with Canonical URLs
Setting the canonical URL is crucial for SEO to avoid duplicate content issues. In Next.js 14, we use the generateMetadata
function to dynamically generate metadata for our pages. Here’s how it’s done:
- Define the
generateMetadata
Function:
export async function generateMetadata({ params }): Promise<Metadata> {
const { id, name } = params;
const nameSlug = name ? (Array.isArray(name) ? name.join('/') : name) : '';
const canonicalUrl = `https://yourwebsite.com/company/${id}${nameSlug ? `/${nameSlug}` : ''}`;
return {
title: 'Company Page',
alternates: {
canonical: canonicalUrl,
},
};
}
In this function, we construct the canonical URL based on the presence of the name
parameter. The alternates
property in the returned metadata object includes the canonical URL, ensuring that search engines index the correct URL.
Handling URL Parameters
The CompanyPage
component needs to correctly handle the URL parameters. Here’s the code snippet that does this:
const CompanyPage = ({ params }) => {
const { id, name } = params;
const nameSlug = name ? (Array.isArray(name) ? name.join('/') : name) : '';
return (
<div>
<h1>Company Page</h1>
<p>Company ID: {id}</p>
{nameSlug && <p>Company Name Slug: {nameSlug}</p>}
</div>
);
};
In this component:
- We destructure the
params
object to get theid
andname
parameters. - We handle the
name
parameter to create anameSlug
, which can be an array or a single string. - We render the company information, displaying the
nameSlug
only if it exists.
Conclusion
Creating optional dynamic routes in Next.js 14 and setting up canonical URLs with metadata can significantly enhance your application’s SEO and user experience. By structuring your project correctly, handling URL parameters efficiently, and generating metadata dynamically, you ensure that your web pages are both user-friendly and search engine optimized.
Implementing these features in your Next.js 14 application will make your routes more flexible and improve your SEO efforts. I hope this guide has provided a clear and practical approach to achieving this.