Skip to content

Commit 8e59b24

Browse files
committed
use custom logic to resolve latest page URL and canonical URL
- delegate to helper to compute latest page URL (latest-page-url) - delegate to helper to compute canonical URL (canonical-url) - update latest page URL resolution to consider aliases - update canonical URL resolution to consider aliases - don't show prerelease banner if all versions are prereleases - document the logic for computing the latest page URL and canonical URL - hide View Latest button if page is not available in latest version
1 parent 2d56908 commit 8e59b24

File tree

8 files changed

+137
-17
lines changed

8 files changed

+137
-17
lines changed

README.adoc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,61 @@ You can test the feedback widget directly from the preview site by setting the `
360360

361361
The configuration for the widget is currently hardcoded into the partial template.
362362

363+
== View Latest and Canonical URL
364+
365+
This section documents the logic used to compute the URL for the View Latest button and the canonical URL.
366+
367+
=== View Latest
368+
369+
If the version of the current page does not match the latest version of the component (i.e., product), a banner is displayed to the visitor.
370+
If the version is a prerelease, the banner states that you're viewing a prerelease version.
371+
If the version is an older stable release, the banner states that a newer version is available.
372+
On the banner offers a button named "View Latest" that directs the visitor to the latest version.
373+
374+
The "View Latest" button tries to preserve the current page when switching versions.
375+
If the page is no longer available, then the button directs the user to the start page for the component.
376+
377+
The URL for the "View Latest" button is computed by the latest-page-url helper.
378+
Here's the logic that the helper uses:
379+
380+
* If the current page is found in the latest version, the latest page URL resolves to the URL of that page.
381+
For example, the latest page URL for https://docs.couchbase.com/server/6.0/introduction/intro.html resolves to https://docs.couchbase.com/server/6.5/introduction/intro.html (assuming 6.5 is the latest version)
382+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, the version segment in the URL is replaced with the word "current".
383+
For example, the latest page URL for https://docs.couchbase.com/server/6.0/introduction/intro.html resolves to https://docs.couchbase.com/server/current/introduction/intro.html
384+
* If the current page is not found in the latest version, but the page is claimed by an alias, the latest page URL resolves to the URL of the page to which the alias points.
385+
For example, the latest page URL for https://docs.couchbase.com/server/5.5/admin/ui-intro.html resolves to https://docs.couchbase.com/server/6.5/manage/management-overview.html (assuming 6.5 is the latest version)
386+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, the version segment in the URL is replaced with the word "current".
387+
For example, the latest page URL for https://docs.couchbase.com/server/5.5/admin/ui-intro.html resolves to https://docs.couchbase.com/server/current/manage/management-overview.html
388+
* If neither the current page or an alias is found in the latest version, the latest page URL resolves to the component start page.
389+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, the version segment in the URL is replaced with the word "current".
390+
391+
If the current page is in the archive site and the latest version is in the production site, then the latest page URL will point to the production site.
392+
In this case, the version segment will only be replaced with "current" if the PRIMARY_SITE_SUPPORTS_CURRENT_URL=true environment variable is set.
393+
394+
=== Canonical URL
395+
396+
The canonical URL differs slightly from the URL for the "View Latest" button in that if the page cannot be found in the latest version, it instead resolves to the newest version of the page.
397+
The canonical URL can resolve to the current URL (if the current URL is the canonical URL).
398+
399+
The canonical URL is computed by the canonical-url helper.
400+
Here's the logic that the helper uses:
401+
402+
* If the site.url is not set to an absolute path, no value is returned.
403+
* If the current page is found in the latest version, the canonical URL resolves to the URL of that page.
404+
For example, the canonical URL for https://docs.couchbase.com/server/6.0/introduction/intro.html resolves to https://docs.couchbase.com/server/6.5/introduction/intro.html (assuming 6.5 is the latest version)
405+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, the version segment in the URL is replaced with the word "current".
406+
For example, the canonical URL for https://docs.couchbase.com/server/6.0/introduction/intro.html resolves to https://docs.couchbase.com/server/current/introduction/intro.html
407+
* If the current page is not found in the latest version, but the page is claimed by an alias, the canonical URL resolves to the URL of the page to which the alias points.
408+
For example, the canonical URL for https://docs.couchbase.com/server/5.5/admin/ui-intro.html resolves to https://docs.couchbase.com/server/6.5/manage/management-overview.html (assuming 6.5 is the latest version)
409+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, the version segment in the URL is replaced with the word "current".
410+
For example, the canonical URL for https://docs.couchbase.com/server/5.5/admin/ui-intro.html resolves to https://docs.couchbase.com/server/current/manage/management-overview.html
411+
* If neither the current page or an alias is found in the latest version, the current URL resolves to the newest version of the page (which could be the current page).
412+
For example, the canonical URL for https://docs.couchbase.com/server/4.0/architecture/cluster-ram-quotas.html resolves to https://docs.couchbase.com/server/4.1/architecture/cluster-ram-quotas.html
413+
** If the SUPPORTS_CURRENT_URL=true environment variable is set, it has no affect on this case.
414+
415+
If the current page is in the archive site and the latest version is in the production site, then the canonical URL will point to the production site.
416+
In this case, the version segment will only be replaced with "current" if the PRIMARY_SITE_SUPPORTS_CURRENT_URL=true environment variable is set and the newest version of the page is the latest version of the component.
417+
363418
== Release the UI Bundle
364419

365420
Once you're satisfied with the changes you've made to the UI and would like to make those changes available to Antora, you'll need to publish the UI as a bundle by making a release.

src/helpers/canonical-url.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const VERSIONED_ROOT_RELATIVE_URL_RX = /^(\/[^/]+)\/[^/]+(?=\/)/
4+
5+
module.exports = ({ data: { root } }) => {
6+
const { contentCatalog, env, page, site } = root
7+
const siteUrl = site.url
8+
if (!siteUrl || siteUrl.charAt() === '/') return
9+
let { url, version, missing } = page.versions ? page.latest || { url: page.url } : page
10+
const latestVersion = version
11+
if (missing) {
12+
const family = 'alias'
13+
const baseAliasId = { component: page.component.name, module: page.module, family, relative: page.relativeSrcPath }
14+
let latestReached
15+
for (const it of page.versions) {
16+
if (!(latestReached || (latestReached = it.latest))) continue
17+
if (it.missing) {
18+
const alias = contentCatalog.getById({ ...baseAliasId, version: it.version })
19+
if (alias) {
20+
url = alias.rel.pub.url
21+
version = it.version
22+
break
23+
}
24+
} else {
25+
;({ url, version } = it)
26+
break
27+
}
28+
}
29+
}
30+
const targetSiteUrl = url.charAt() === '/' ? siteUrl : page.componentVersion.asciidoc.attributes['primary-site-url']
31+
if (version === 'master' || version !== latestVersion) {
32+
return targetSiteUrl + url
33+
} else if (siteUrl === targetSiteUrl) {
34+
if (env.SUPPORTS_CURRENT_URL === 'true') return siteUrl + url.replace(VERSIONED_ROOT_RELATIVE_URL_RX, '$1/current')
35+
return siteUrl + url
36+
} else if (env.PRIMARY_SITE_SUPPORTS_CURRENT_URL === 'true') {
37+
return targetSiteUrl + url.substr(targetSiteUrl.length).replace(VERSIONED_ROOT_RELATIVE_URL_RX, '$1/current')
38+
}
39+
return targetSiteUrl + url
40+
}

src/helpers/concat.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/helpers/current-url.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/helpers/latest-page-url.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const VERSIONED_ROOT_RELATIVE_URL_RX = /^(\/[^/]+)\/[^/]+(?=\/)/
4+
5+
module.exports = ({ data: { root } }) => {
6+
const { contentCatalog, env, page } = root
7+
let { url, version, missing } = page.latest || { url: page.url }
8+
if (missing) {
9+
const latestAlias = contentCatalog.getById({
10+
component: page.component.name,
11+
version,
12+
module: page.module,
13+
family: 'alias',
14+
relative: page.relativeSrcPath,
15+
})
16+
if (!latestAlias) return
17+
url = latestAlias.rel.pub.url
18+
}
19+
if (url.charAt() === '/') {
20+
return env.SUPPORTS_CURRENT_URL === 'true' ? url.replace(VERSIONED_ROOT_RELATIVE_URL_RX, '$1/current') : url
21+
} else if (env.PRIMARY_SITE_SUPPORTS_CURRENT_URL === 'true') {
22+
const primarySiteUrl = page.componentVersion.asciidoc.attributes['primary-site-url']
23+
return primarySiteUrl + url.substr(primarySiteUrl.length).replace(VERSIONED_ROOT_RELATIVE_URL_RX, '$1/current')
24+
}
25+
return url
26+
}

src/layouts/default.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<head>
44
{{> head-first}}
55
<title>{{{detag (or page.title 'Untitled')}}}{{#with site.title}} | {{this}}{{/with}}</title>
6-
{{#if page.canonicalUrl}}
7-
<link rel="canonical" href="{{#if (and (eq env.SUPPORTS_CURRENT_URL 'true') (ne page.version 'master'))}}{{concat site.url (current-url page.url)}}{{else}}{{page.canonicalUrl}}{{/if}}">
8-
{{/if}}
6+
{{#with (canonical-url)}}
7+
<link rel="canonical" href="{{this}}">
8+
{{/with}}
99
{{> head-last}}
1010
</head>
1111
<body class="article">

src/layouts/tutorials.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<head>
44
{{> head-first}}
55
<title>Tutorials{{#with site.title}} | {{this}}{{/with}}</title>
6-
{{#if page.canonicalUrl}}
7-
<link rel="canonical" href="{{#if (and (eq env.SUPPORTS_CURRENT_URL 'true') (ne page.version 'master'))}}{{concat site.url (current-url page.url)}}{{else}}{{page.canonicalUrl}}{{/if}}">
8-
{{/if}}
6+
{{#with (canonical-url)}}
7+
<link rel="canonical" href="{{this}}">
8+
{{/with}}
99
{{> head-last}}
1010
</head>
1111
<body class="tutorials body tiles">

src/partials/main.hbs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<main class="article" data-ceiling="topbar">
2-
{{#unless (or page.attributes.hide-view-latest (eq page.componentVersion page.component.latest))}}
2+
{{#unless (or (ne page.attributes.hide-view-latest undefined) (eq page.componentVersion page.component.latest))}}
3+
{{#with (latest-page-url)}}
34
<div class="article-banner">
4-
{{#if page.componentVersion.prerelease}}
5+
{{#if (and @root.page.componentVersion.prerelease (not @root.page.latest.prerelease))}}
56
<p>You are viewing the documentation for a prerelease version.</p>
67
{{else}}
7-
<p> <i class="fab fa-asymmetrik"></i> A newer version of this documentation is available.</p>
8+
<p><i class="fab fa-asymmetrik"></i> A newer version of this documentation is available.</p>
89
{{/if}}
9-
<a class="btn" href="{{#if (eq env.SUPPORTS_CURRENT_URL 'true')}}{{relativize (current-url page.url)}}{{else}}{{relativize page.latest.url}}{{/if}}">View Latest</a>
10+
<a class="btn" href="{{relativize this}}">View Latest</a>
1011
</div>
12+
{{else if (and page.componentVersion.prerelease (not page.latest.prerelease))}}
13+
<div class="article-banner">
14+
<p>You are viewing the documentation for a prerelease version.</p>
15+
</div>
16+
{{/with}}
1117
{{/unless}}
1218
<div class="article-header">
1319
{{> nav-control}}
1420
{{> crumbs}}
15-
{{!-- {{> toolbar}} --}}
1621
</div>
1722
{{> article}}
1823
</main>

0 commit comments

Comments
 (0)