Skip to content

Commit

Permalink
Insert <link rel="canonical"> elements
Browse files Browse the repository at this point in the history
This prevents duplicate search results in case copies of the material
is being left visible on the web. See

  https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls

for details. The implementation here is very simple and should go away
when google/mdbook-i18n-helpers#70 is
implemented.
  • Loading branch information
mgeisler committed Aug 30, 2023
1 parent 6827f62 commit 0773a12
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ jobs:
echo "::endgroup::"
done
# This helps guard against duplicate search results in case test
# instances are left around on the public Internet. See
# https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls
# for details.
#
# For simplicity, we replace a fixed string instead of
# attempting to parse the HTML in some clever way.
# https://github.com/google/mdbook-i18n-helpers/issues/70 will
# eventually make this obsolete.
- name: 'Add link rel="canonical" elements'
run: |
canonical="https://google.github.io/comprehensive-rust"
cd book/html
for page in **/*.html; do
if [[ ${page:t} -eq "index.html" ]]; then
link=${page:h}
else
link=$page
fi
sed -i 's|<!-- LINK-REL-CANONICAL -->|<link rel="canonical" href="'$canonical/$link'">|'
shell: zsh {0}

- name: Setup Pages
uses: actions/configure-pages@v2

Expand Down
4 changes: 4 additions & 0 deletions theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff" />

{{! Replaced with a <link rel="canonical"> element in
publish.yml. Remove when after fixing this issue:
https://github.com/google/mdbook-i18n-helpers/issues/70 }}
<!-- LINK-REL-CANONICAL -->
{{#if favicon_svg}}
<link rel="icon" href="{{ path_to_root }}favicon.svg">
{{/if}}
Expand Down

0 comments on commit 0773a12

Please sign in to comment.