Mastering Nextjs Relative Link Configuration for Better URLs

Author

Reads 285

Free stock photo of abstract, authentication error, backend development
Credit: pexels.com, Free stock photo of abstract, authentication error, backend development

Nextjs relative links are a powerful feature that allows you to create clean and maintainable URLs for your application. By default, Nextjs uses absolute URLs, but with relative links, you can create URLs that are relative to the current page.

Relative links are defined in the next.config.js file using the `linkResolver` option. This option allows you to specify a function that will resolve the link to a physical file path.

To use relative links, you need to create a link resolver function that will handle the link resolution. This function should return the correct file path for the link.

The link resolver function can be used to create links to pages, images, and other assets. By using relative links, you can create a more maintainable and scalable application.

Configuring Next.js URLs

To create self-healing URLs for blog posts, start with a fresh React app from the CLI and install the slugify package to create a URL-safe slug from the post's title.

Credit: youtube.com, Next.js Tutorial #4 - Linking Between Pages

You'll also need to use the Next.js App router, which is a powerful tool for creating dynamic routes.

To use the public dir while keeping relative paths, put assets in the public dir and refer to them as you would if they were located in the same directory of the markdown file. This approach is simple and effective, and it's the one I went with.

Use Public Dir with Relative Paths

Using the public dir with relative paths is a simple approach to configuring Next.js URLs. This method involves embracing Next.js's suggestion of putting assets in the public dir.

To achieve this behavior, you can create a custom plugin in your MDX/Markdown pipeline using remark. Remark is a tool that transforms markdown with plugins.

Add the plugin to the remarkPlugins list of your MDX build pipeline, such as in next.config.js or contentlayer.config.js, to use it. This will rewrite the src attribute of all images and videos to point to the public directory.

This approach allows you to refer to assets as if they were in the same directory as the markdown file, while still using the public dir.

Self-Healing URLs in Next.js

Credit: youtube.com, Clean Urls in Next.js using Next Routes

Self-Healing URLs in Next.js are a game-changer for creating clean and organized URLs for your blog posts. To create self-healing URLs, start with a fresh React app from the CLI and install the slugify package, which you'll use to create a URL-safe slug from the post's title.

You'll want to use the Next.js App router to create these self-healing URLs. This will allow you to generate URLs that are both clean and easy to understand. For example, if you have a post with the title "My Favorite Post", the slugify package will turn it into a URL like "my-favorite-post".

To generate a sitemap with the correct canonical URL for each post, you'll need to use the Sitemap feature in Next.js. This will help search engines like Google understand the structure of your site and index your pages more accurately. The canonical URL for each post should exactly match the pattern you've set up for your self-healing URLs, like "https://example.com/posts/${POST_TITLE}-${POST_ID}".

SEO and URL Optimization

Credit: youtube.com, Absolute or relative URLs, which is better?

SEO and URL Optimization is crucial for your Next.js app to rank well in search engines. Google uses your site's sitemap file to understand the structure of your site.

You'll need to install the slugify package to create a URL-safe slug from your post's title. This will help you generate self-healing URLs for your blog posts.

A sitemap should contain the canonical URL for every page in your site. This is the URL that you want Google to index for each post.

You should use the same pattern for your self-healing URLs in your sitemap, like https://example.com/posts/${POST_TITLE}-${POST_ID}. This will help avoid duplicate content issues.

A canonical tag looks like this, and you should add it to each post page to specify the original URL for the content. This will help search engines understand which URL is the original URL for each post.

If you don't use a canonical URL in your sitemap, it can cause problems with SEO. Google will think you have two pages with the same content and penalize you for duplicate content.

Calvin Connelly

Senior Writer

Calvin Connelly is a seasoned writer with a passion for crafting engaging content on a wide range of topics. With a keen eye for detail and a knack for storytelling, Calvin has established himself as a versatile and reliable voice in the world of writing. In addition to his general writing expertise, Calvin has developed a particular interest in covering important and timely subjects that impact society.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.