2023-12-22
Canonical tag for Next.js (App Router)
Since I last switched my blog from Next.js Pages Router to App Router in 2023 May, I didn't have canonical urls for each posts. It is because the paradigm changed without proper documentation. At the time, not all documentations were available for App Router, and many often linked back to Pages Router.
I tried to put the canonical urls back. Eventually I managed to do it but official documentation is still unkind. It is quite stupid that adding link tag to a page is so difficult. The fact that meta tag can only be added in server side made it challenging.
Current directory structure of app/
Add following generateMetadata function inside page.tsx
Above code will insert following link tag inside head tag for this post