Skip to content

Commit

Permalink
More comprehensive canonical checking
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnyjth committed Mar 30, 2024
1 parent d5fbb37 commit 2593e38
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
registry-url: https://registry.npmjs.org/

- name: Deploy to NPM
run: npm run build && npm run bump
run: npm run bump
working-directory: web-embeds
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
19 changes: 16 additions & 3 deletions packages/web-shared/components/ContentSingle.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ function ContentSingle(props = {}) {
}
}, [invalidPage, navigate]);

// Try and convince Google that this is the canonical URL
const canonicalUrl = `${window.location.origin}/?id=${searchParams.get('id')}`;

// Some websites have existing canonical links that need to be updated.
useEffect(() => {
const canonicalLink = document.querySelector('link[rel="canonical"]');
if (canonicalLink) {
const oldValue = canonicalLink.getAttribute('href');
canonicalLink.setAttribute('href', canonicalUrl);
// Reset when we are done.
return () => {
canonicalLink.setAttribute('href', oldValue);
};
}
}, []);

if (props.loading || invalidPage) {
return (
<Box
Expand Down Expand Up @@ -190,9 +206,6 @@ function ContentSingle(props = {}) {
}
};

// Try and convince Google that this is the canonical URL
const canonicalUrl = `${window.location.origin}?id=${searchParams.get('id')}`;

return (
<>
{/* TODO: Max width set to 750px due to low resolution pictures. Can be increased as higher quality images are used */}
Expand Down
6 changes: 3 additions & 3 deletions web-embeds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
"dev": "craco start",
"build": "node scripts/build.js",
"test": "craco test",
"bump": "./scripts/bump.sh && npm publish && npm version 0.0.1 && npm run flush-cdn",
"bump:minor": "./scripts/bump.sh minor && npm publish && npm version 0.0.1 && npm run flush-cdn",
"bump:major": "./scripts/bump.sh major && npm publish && npm version 0.0.1 && npm run flush-cdn",
"bump": "yarn build && ./scripts/bump.sh && npm publish && npm version 0.0.1 && npm run flush-cdn",
"bump:minor": "yarn build && ./scripts/bump.sh minor && npm publish && npm version 0.0.1 && npm run flush-cdn",
"bump:major": "yarn build && ./scripts/bump.sh major && npm publish && npm version 0.0.1 && npm run flush-cdn",
"format": "prettier --write .",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
"flush-cdn": "./flush-cdn-cache.sh"
Expand Down

0 comments on commit 2593e38

Please sign in to comment.