@@ -84,20 +84,18 @@ const _create_language_select = (languages) => {
8484 * @param {Array<string> } urls
8585 * @private
8686 */
87- const _navigate_to_first_existing = ( urls ) => {
87+ const _navigate_to_first_existing = async ( urls ) => {
8888 // Navigate to the first existing URL in urls.
8989 for ( const url of urls ) {
90- fetch ( url )
91- . then ( ( response ) => {
92- if ( response . ok ) {
93- window . location . href = url ;
94- return url ;
95- }
96- } )
97- . catch ( ( err ) => {
98- console . error ( `Error when fetching '${ url } '!` ) ;
99- console . error ( err ) ;
100- } ) ;
90+ try {
91+ const response = await fetch ( url , { method : 'HEAD' } ) ;
92+ if ( response . ok ) {
93+ window . location . href = url ;
94+ return url ;
95+ }
96+ } catch ( err ) {
97+ console . error ( `Error when fetching '${ url } ': ${ err } ` ) ;
98+ }
10199 }
102100
103101 // if all else fails, redirect to the d.p.o root
@@ -111,7 +109,7 @@ const _navigate_to_first_existing = (urls) => {
111109 * @returns {void }
112110 * @private
113111 */
114- const _on_version_switch = ( event ) => {
112+ const _on_version_switch = async ( event ) => {
115113 if ( _IS_LOCAL ) return ;
116114
117115 const selected_version = event . target . value ;
@@ -127,7 +125,7 @@ const _on_version_switch = (event) => {
127125 // 2. The current page in English with the new version
128126 // 3. The documentation home in the current language with the new version
129127 // 4. The documentation home in English with the new version
130- _navigate_to_first_existing ( [
128+ await _navigate_to_first_existing ( [
131129 window . location . href . replace ( _CURRENT_PREFIX , new_prefix ) ,
132130 window . location . href . replace ( _CURRENT_PREFIX , new_prefix_en ) ,
133131 new_prefix ,
@@ -142,7 +140,7 @@ const _on_version_switch = (event) => {
142140 * @returns {void }
143141 * @private
144142 */
145- const _on_language_switch = ( event ) => {
143+ const _on_language_switch = async ( event ) => {
146144 if ( _IS_LOCAL ) return ;
147145
148146 const selected_language = event . target . value ;
@@ -155,7 +153,7 @@ const _on_language_switch = (event) => {
155153 // Try the following pages in order:
156154 // 1. The current page in the new language with the current version
157155 // 2. The documentation home in the new language with the current version
158- _navigate_to_first_existing ( [
156+ await _navigate_to_first_existing ( [
159157 window . location . href . replace ( _CURRENT_PREFIX , new_prefix ) ,
160158 new_prefix ,
161159 ] ) ;
0 commit comments