Skip to content

Commit

Permalink
Use canonical URL for search index. Fixes #323
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Sep 21, 2024
1 parent 5f2fbfb commit bc13140
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/site-search/template.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<site-search class="app-site-search govuk-!-display-none-print" index="{{ params.indexPath | htmlBaseUrl }}" label="{{ params.label | default("Search site") }}">
<site-search class="app-site-search govuk-!-display-none-print" index="{{ params.indexPath | canonicalUrl }}" label="{{ params.label | default("Search site") }}">
{%- if params.sitemapPath -%}
<a class="app-site-search__link" href="{{ params.sitemapPath }}">Sitemap</a>
{%- endif -%}
Expand Down
11 changes: 8 additions & 3 deletions lib/filters/canonical-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ const path = require('node:path')
* @returns {string} Canonical site URL with resolved path
*/
module.exports = function (string = '') {
const { url } = this.ctx.options
const { options } = this.ctx

// No site URL configured
if (!options?.url) {
return string
}

// If plugin options do not provide a site URL, return the path
if (!URL.canParse(url)) {
if (!URL.canParse(options.url)) {
return string
}

const siteUrl = new URL(url)
const siteUrl = new URL(options.url)

// If incoming string is a URL, return that if it’s an external URL
if (URL.canParse(string)) {
Expand Down

0 comments on commit bc13140

Please sign in to comment.