Worker - Canonical link in header not working and urls with parameters

I am trying to add the following link html element in header for the following pages through workers:

Though, I don’t know why, the element isn’t added in head: Blog Monsieur Chalets.

p.s. I have another question about workers, how would you apply the following link to all urls that have Blog Monsieur Chalets? conditions. What do you think about the screenshot under?

HI @raphael3

pathname will never start with a ?. If you test new URL('https://example.com/blog?p=1').pathname pathname will only equal /blog. Anything from the ? is part of search so if you test new URL('https://example.com/blog?p=1').search you’ll see ?p=1.

I’ve been able to make it work @the on the Blog Monsieur Chalets url though not on the following url: #1 en chalets à louer haut de gamme au Québec!.

How would you make it work?

As I mentioned previously, pathname will never contain a ?. E.g.

{
  hash: ""
  host: "www.monsieurchalets.com"
  hostname: "www.monsieurchalets.com"
  href: "https://www.monsieurchalets.com/blog?p=comment-faire-pour-louer-son-chalet"
  origin: "https://www.monsieurchalets.com"
  password: ""
  pathname: "/blog"
  port: ""
  protocol: "https:"
  search: "?p=comment-faire-pour-louer-son-chalet"
  searchParams: URLSearchParams { p → "comment-faire-pour-louer-son-chalet" }
  username: ""
}

You need to check search, i.e.

if  (url.search.startsWith('?'))
// or
if (url.search !== "")
// or
if (url.search)

However as the pathname is the same in both cases (as below) checking the existence of a search string is probably not necessary unless you are looking for a specific string

{
  hash: ""
  host: "www.monsieurchalets.com"
  hostname: "www.monsieurchalets.com"
  href: "https://www.monsieurchalets.com/blog"
  origin: "https://www.monsieurchalets.com"
  password: ""
  pathname: "/blog"
  port: ""
  protocol: "https:"
  search: ""
  searchParams: URLSearchParams(0)
  username: ""
}

Oh great.

So basically, I would need to be checking for 2 conditions for this one @the ?

I would need to be checking the following conditions: url.pathname.startsWith(“/blog”) AND

if  (url.search.startsWith('?'))
// or
if (url.search !== "")
// or
if (url.search)

. If those are applied, inject tag, correct? @the

To make sure that only the links with /blog? have tags added

As I wrote previously

This means

new URL('https://example.com/blog')
// and
new URL('https://example.com/blog?p=123')

will have the same pathname value of /blog, so checking the search string (i.e. search.startsWith('?')) is pointless.

However,

new URL('https://example.com/blog')
// and
new URL('https://example.com/blog/some-blog-post')

will have entirely different pathname values: /blog vs /blog/some-blog/post.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.