@@ -2,7 +2,47 @@ const root = new URL(JSON.parse(document.querySelector('#__config').textContent)
22const isLocal = [ 'localhost' , '127.0.0.1' , '' ] . includes ( location . hostname ) ;
33const 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
747const goTo = async ( version ) => {
848 const target = samePageIn ( version ) ;
@@ -84,5 +124,6 @@ const renderVersions = (deployed) => {
84124
85125fetch ( versionsUrl )
86126 . then ( ( response ) => ( response . ok ? response . json ( ) : Promise . reject ( response . status ) ) )
127+ . then ( withLatestTag )
87128 . then ( renderVersions )
88129 . catch ( ( ) => { } ) ;
0 commit comments