I’m working on SEO component which needs a canonical URL.
How can I get URL of static page in Next.js with Automatic Static Optimization turned on?
I’m working on SEO component which needs a canonical URL.
How can I get URL of static page in Next.js with Automatic Static Optimization turned on?
6
Answers
use package called next-absolute-url . It works in getServerSideProps. Since getStaticProps run on build time so dont have data available.
can be used as
Using
useRouter
fromnext/router
you can get thepathname
for the current page and use it in a<Head/>
tag as following:Building on fzembow’s comment about
useRouter().asPath
and GorvGoyl’s answer, here’s an implementation which manages to handle both dynamic routes and excludes anchor and query param URL extensions:A super ugly, yet, the most adequate solution I found:
My solution was to use new URL with router.asPath.
I added a canonical link tag to
_app.js
so that it appears on every page: