diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index e29f6a3f13ec..fb9862dc9f57 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -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|||'
+ shell: zsh {0}
+
- name: Setup Pages
uses: actions/configure-pages@v2
diff --git a/theme/index.hbs b/theme/index.hbs
index df975d7b0e46..18e24bacafad 100644
--- a/theme/index.hbs
+++ b/theme/index.hbs
@@ -29,6 +29,10 @@
+ {{! Replaced with a element in
+ publish.yml. Remove when after fixing this issue:
+ https://github.com/google/mdbook-i18n-helpers/issues/70 }}
+
{{#if favicon_svg}}
{{/if}}