如何在启用自动静态优化的情况下访问 Next.js 中的规范 URL? [英] How to access canonical URL in Next.js with Automatic Static Optimization turned on?

查看:48
本文介绍了如何在启用自动静态优化的情况下访问 Next.js 中的规范 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究需要规范 URL 的 SEO 组件.

I'm working on SEO component which needs a canonical URL.

如何在启用自动静态优化的情况下在 Next.js 中获取静态页面的 URL?

How can I get URL of static page in Next.js with Automatic Static Optimization turned on?

推荐答案

使用 useRouter from next/router 你可以得到 pathname当前页面并在 标签中使用它,如下所示:

Using useRouter from next/router you can get the pathname for the current page and use it in a <Head/> tag as following:

import { useRouter } from "next/router";

const site = "https://gourav.io";
const canonicalURL = site + useRouter().pathname;

<Head>
  <link rel="canonical" href={canonicalURL} />
</Head>

这篇关于如何在启用自动静态优化的情况下访问 Next.js 中的规范 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆