Canonical link for PDF pages

Framework: NextJS, React

I have a middleware.ts file in which I set multiple headers.
I have a custom check for setting <Link rel:“canonical”> to pages that end with .pdf

  const isPdfRequest = request.nextUrl.pathname.toLowerCase().endsWith('.pdf')
  if (isPdfRequest) {
    // Set the canonical URL for the PDF file
    const domain = process.env.NETLIFY_WEBSITE_DOMAIN ?? strings.DOMAIN_URL
    const canonicalUrl = `${domain}${request.nextUrl.pathname}`

    headers.set('Link', `<${canonicalUrl}>; rel="canonical"`)


This all works as intended when I run my project locally, but when I deploy it, other headers (csp, x-frame, etc.) work except the one for PDF file links. This was working, but when I migrated from pages to app routing, the header doesn’t work. I have asked Netlify AI but got no positive feedback. What could be the issue?

Hi @Ivanmih99,

This has been responded to in the helpdesk.