Skip to content

Commit 49ce8de

Browse files
committed
API: send project-slug= and version-slug= to the backend
Follow the plan discussed in readthedocs/readthedocs.org#10536 Grab the `project-slug` and `version-slug` from the `meta` tag injected by CF and send it back to the backend. If there is any addons that can be enabled in the current page that depends on the URL, it will send it also. Each Addon class should implement `requireUrlParam` to decide whether or not to send the `url=` to the backend API or not.
1 parent 1cc3c26 commit 49ce8de

File tree

7 files changed

+65
-22
lines changed

7 files changed

+65
-22
lines changed

dist/readthedocs-addons.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/readthedocs-addons.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<html>
22
<head>
33
<title>Documentation Addons - Read the Docs</title>
4+
<meta name="readthedocs-project-slug" content="test-builds" />
5+
<meta name="readthedocs-version-slug" content="latest" />
46
</head>
57
<body>
68

src/docdiff.js

+4
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,8 @@ export class DocDiffAddon extends AddonBase {
205205
static isEnabled(config) {
206206
return config.addons && config.addons.doc_diff.enabled === true;
207207
}
208+
209+
static requiresUrlParam() {
210+
return window.location.host.endsWith(".readthedocs.build");
211+
}
208212
}

src/index.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,31 @@ export function setup() {
1414
return false;
1515
}
1616

17+
const addons = [
18+
flyout.FlyoutAddon,
19+
notification.NotificationAddon,
20+
analytics.AnalyticsAddon,
21+
ethicalads.EthicalAdsAddon,
22+
search.SearchAddon,
23+
docdiff.DocDiffAddon,
24+
hotkeys.HotKeysAddon,
25+
];
26+
1727
return new Promise((resolve) => {
1828
domReady
1929
.then(() => {
20-
return getReadTheDocsConfig();
30+
let sendUrlParam = false;
31+
for (const addon of addons) {
32+
if (addon.requiresUrlParam()) {
33+
sendUrlParam = true;
34+
break;
35+
}
36+
}
37+
38+
return getReadTheDocsConfig(sendUrlParam);
2139
})
2240
.then((config) => {
2341
let promises = [];
24-
let addons = [
25-
flyout.FlyoutAddon,
26-
notification.NotificationAddon,
27-
analytics.AnalyticsAddon,
28-
ethicalads.EthicalAdsAddon,
29-
search.SearchAddon,
30-
docdiff.DocDiffAddon,
31-
hotkeys.HotKeysAddon,
32-
];
3342

3443
// IS_PRODUCTION comes from Webpack and is undeclared otherwise
3544
if (typeof IS_PRODUCTION === "undefined" ? false : IS_PRODUCTION) {

src/readthedocs-config.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@ import { CLIENT_VERSION, ADDONS_API_VERSION } from "./utils";
44
* Load Read the Docs configuration from API endpoint.
55
*
66
*/
7-
export function getReadTheDocsConfig() {
8-
let url =
9-
"/_/addons/?" +
10-
new URLSearchParams({
11-
url: window.location.href,
12-
"client-version": CLIENT_VERSION,
13-
"api-version": ADDONS_API_VERSION,
14-
});
7+
export function getReadTheDocsConfig(sendUrlParam) {
8+
const metaProject = document.querySelector(
9+
"meta[name='readthedocs-project-slug']",
10+
);
11+
const metaVersion = document.querySelector(
12+
"meta[name='readthedocs-version-slug']",
13+
);
14+
15+
let projectSlug;
16+
let versionSlug;
17+
let params = {
18+
"client-version": CLIENT_VERSION,
19+
"api-version": ADDONS_API_VERSION,
20+
};
21+
22+
if (sendUrlParam) {
23+
params["url"] = window.location.href;
24+
}
25+
26+
if (metaProject && metaVersion) {
27+
projectSlug = metaProject.content;
28+
versionSlug = metaVersion.content;
29+
30+
params["project-slug"] = projectSlug;
31+
params["version-slug"] = versionSlug;
32+
}
33+
34+
let url = "/_/addons/?" + new URLSearchParams(params);
1535

1636
// Retrieve a static JSON file when working in development mode
1737
if (window.location.href.startsWith("http://localhost")) {

src/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ export class AddonBase {
4848
static isEnabled(config) {
4949
return false;
5050
}
51+
52+
static requiresUrlParam() {
53+
// Decide whether or not this addons requires sending `url=` parameter to the API endpoint.
54+
// Sending this attribute will make the API response to contain extra data (e.g. resolved URLs that depend on the exact URL)
55+
//
56+
// Note that sending `url=` attribute reduces the possibilities to use a cached response accross all the pages for the same project/version.
57+
return false;
58+
}
5159
}
5260

5361
/**

0 commit comments

Comments
 (0)