I have the following head custom code for the CMS project we call Perspectives to set the canonical code to the page’s URL or to an external resource when we occasionally syndicate content.
When I view the page source of a Perspective after publishing I see the code in the header rather than the result of the code (see the following screenshot of the relevant page source):
The script would not go away, it would just execute and make its additional modifications.
However you’ve got a synchronous script here running before the DOM is built, so it’s possible that’s tripping things up.
Still no luck and the conversation here is a bit quiet so I thought I’d try chatting it through with pi.ai. This was my first experience of pi, and it was unexpectedly delightful.
Still no solution to my problem though. The ai chatbot describes it as “a bit of head-scratcher!”
I copied our chat to a google doc to share here should anyone be interested:
So I’ve been checking to see if a canonical link is set by viewing the page source in the browser. When I view page source in Firefox, the relevant section reads:
You’ll note that the line you have identified with the green rectangle is conspicuous by its absence.
However, now that I’ve used Firefox Web Developer Tools to inspect the page, there it is … exactly where you show it!
It appears then that I have been trying to fix something that does not need fixing!
So now I appreciate that this may happen, I’ve done a web search and found this explanation. Is that what’s happening here?
Thank you.
P.S. If you reply with “Yes, javascript can only change the DOM (what the user can see), not the source itself” I’ll tick it as the solution here to give you the credit