Skip to content
This repository was archived by the owner on Nov 23, 2026. It is now read-only.

Commit f1f3ab2

Browse files
committed
Read the stable version tag from the latest GitHub release
1 parent 92929df commit f1f3ab2

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎overrides/assets/js/versions.js‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,47 @@ const root = new URL(JSON.parse(document.querySelector('#__config').textContent)
22
const isLocal = ['localhost', '127.0.0.1', ''].includes(location.hostname);
33
const versionsUrl = isLocal ? new URL('assets/versions.json', root) : 'https://codeigniter4.github.io/CodeIgniter4/versions.json';
44

5-
const samePageIn = (version) => version.url + location.pathname.slice(new URL(root).pathname.length) + location.hash;
5+
const releaseUrl = 'https://api.github.com/repos/codeigniter4/CodeIgniter4/releases/latest';
6+
const releaseKey = 'version.latest';
7+
8+
const latestTag = async () => {
9+
try {
10+
const cached = sessionStorage.getItem(releaseKey);
11+
12+
if (cached !== null) {
13+
return cached;
14+
}
15+
} catch {}
16+
17+
const response = await fetch(releaseUrl);
18+
19+
if (!response.ok) {
20+
throw new Error(response.status);
21+
}
22+
23+
const { tag_name: tag } = await response.json();
24+
25+
try {
26+
sessionStorage.setItem(releaseKey, tag);
27+
} catch {}
28+
29+
return tag;
30+
};
31+
32+
const withLatestTag = async (versions) => {
33+
if (!versions.some((version) => version.title.includes('{tag}'))) {
34+
return versions;
35+
}
36+
37+
const tag = await latestTag().catch(() => null);
38+
39+
return versions.map((version) => ({
40+
...version,
41+
title: tag === null ? version.title.replace(' ({tag})', '') : version.title.replace('{tag}', tag),
42+
}));
43+
};
44+
45+
const samePageIn =(version) => version.url + location.pathname.slice(new URL(root).pathname.length) + location.hash;
646

747
const goTo = async (version) => {
848
const target = samePageIn(version);
@@ -84,5 +124,6 @@ const renderVersions = (deployed) => {
84124

85125
fetch(versionsUrl)
86126
.then((response) => (response.ok ? response.json() : Promise.reject(response.status)))
127+
.then(withLatestTag)
87128
.then(renderVersions)
88129
.catch(() => {});

‎overrides/assets/versions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{"title": "Stable (v4.7.5)", "url": "https://codeigniter.com/user_guide/"},
2+
{"title": "Stable ({tag})", "url": "https://codeigniter.com/user_guide/"},
33
{"title": "Latest (develop)", "url": "https://codeigniter4.github.io/CodeIgniter4/develop/"},
44
{"title": "Next minor (4.8)", "url": "https://codeigniter4.github.io/CodeIgniter4/next/"}
55
]

0 commit comments

Comments
 (0)