@@ -58,55 +58,67 @@ const _create_language_select = (current_language) => {
5858
5959const _navigate_to_first_existing = ( urls ) => {
6060 // Navigate to the first existing URL in urls.
61- const url = urls . shift ( ) ;
62- if ( urls . length === 0 || url . startsWith ( 'file:///' ) ) {
63- window . location . href = url ;
64- return ;
61+ for ( const url of urls ) {
62+ if ( url . startsWith ( 'file:///' ) ) {
63+ window . location . href = url ;
64+ return ;
65+ }
66+ fetch ( url )
67+ . then ( ( response ) => {
68+ if ( response . ok ) {
69+ window . location . href = url ;
70+ return url ;
71+ }
72+ } )
73+ . catch ( ( err ) => {
74+ console . error ( `Error when fetching '${ url } '!` ) ;
75+ console . error ( err ) ;
76+ } ) ;
6577 }
66- fetch ( url )
67- . then ( ( response ) => {
68- if ( response . ok ) {
69- window . location . href = url ;
70- } else {
71- navigate_to_first_existing ( urls ) ;
72- }
73- } )
74- . catch ( ( err ) => {
75- void err ;
76- navigate_to_first_existing ( urls ) ;
77- } ) ;
78+
79+ // if all else fails, redirect to the d.p.o root
80+ window . location . href = '/' ;
81+ return '/' ;
7882} ;
7983
8084const _on_version_switch = ( event ) => {
81- const selected_version = event . target . value + '/' ;
82- const url = window . location . href ;
83- const new_url = url . replace (
84- _CURRENT_PREFIX ,
85- '/' + _CURRENT_LANGUAGE + selected_version ,
86- ) ;
87- if ( new_url !== url ) {
85+ const selected_version = event . target . value ;
86+ // English has no language prefix.
87+ const new_prefix_en = `/${ selected_version } /` ;
88+ const new_prefix =
89+ _CURRENT_LANGUAGE === 'en'
90+ ? new_prefix_en
91+ : `/${ _CURRENT_LANGUAGE } /${ selected_version } /` ;
92+ if ( _CURRENT_PREFIX !== new_prefix ) {
93+ // Try the following pages in order:
94+ // 1. The current page in the current language with the new version
95+ // 2. The current page in English with the new version
96+ // 3. The documentation home in the current language with the new version
97+ // 4. The documentation home in English with the new version
8898 _navigate_to_first_existing ( [
89- new_url ,
90- url . replace ( _CURRENT_PREFIX , '/' + selected_version ) ,
91- '/' + _CURRENT_LANGUAGE + selected_version ,
92- '/' + selected_version ,
93- '/' ,
99+ window . location . href . replace ( _CURRENT_PREFIX , new_prefix ) ,
100+ window . location . href . replace ( _CURRENT_PREFIX , new_prefix_en ) ,
101+ new_prefix ,
102+ new_prefix_en ,
94103 ] ) ;
95104 }
96105} ;
97106
98107const _on_language_switch = ( event ) => {
99- let selected_language = event . target . value + '/' ;
100- const url = window . location . href ;
101- if ( selected_language === 'en/' )
102- // Special 'default' case for English.
103- selected_language = '' ;
104- let new_url = url . replace (
105- _CURRENT_PREFIX ,
106- '/' + selected_language + _CURRENT_VERSION ,
107- ) ;
108- if ( new_url !== url ) {
109- _navigate_to_first_existing ( [ new_url , '/' ] ) ;
108+ const selected_language = event . target . value ;
109+ // English has no language prefix.
110+ const new_prefix =
111+ selected_language === 'en'
112+ ? `/${ _CURRENT_VERSION } /`
113+ : `/${ selected_language } /${ _CURRENT_VERSION } /` ;
114+ if ( _CURRENT_PREFIX !== new_prefix ) {
115+ // Try the following pages in order:
116+ // 1. The current page in the new language with the current version
117+ // 2. The documentation home in the new language with the current version
118+ _navigate_to_first_existing ( [
119+ window . location . href . replace ( _CURRENT_PREFIX , new_prefix ) ,
120+ new_prefix ,
121+ ] ) ;
110122 }
111123} ;
112124
0 commit comments