@museumwnf/viewer-core 2.5.1 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@museumwnf/viewer-core",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
4
4
  "description": "Application engine for MWNF websites: turns a dataset.config.js into a mounted Vue application.",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -93,7 +93,16 @@ export function resolveInitialLanguage(offered) {
93
93
  export function connectLanguageToRouter({ locale, offered, router }) {
94
94
  const multilingual = offered.length > 1
95
95
 
96
+ // The navigation under way, from the guard until it lands. Its URL already
97
+ // carries the language the guard set; rewriting the page being left
98
+ // instead cancels it, and the visitor stays there in the new language.
99
+ let pending = null
100
+ const settle = (to) => {
101
+ if (pending === to) pending = null
102
+ }
103
+
96
104
  router.beforeEach((to) => {
105
+ pending = to
97
106
  const next = negotiateLanguage(offered, {
98
107
  requested: multilingual ? to.query.lang : undefined,
99
108
  stored: readStoredLanguage(),
@@ -107,18 +116,28 @@ export function connectLanguageToRouter({ locale, offered, router }) {
107
116
  return true
108
117
  })
109
118
 
110
- // A change of language from the switcher rewrites the URL. Not before the
111
- // first navigation has resolved, though: the router is then still at its
112
- // start location, `/`, and a replace from there cancels the navigation the
113
- // visitor arrived with — every deep link landed on the home page. The guard
114
- // above puts the language in the URL of that first navigation itself.
119
+ router.afterEach((to, from, failure) => {
120
+ settle(to)
121
+ // The visitor switched language while this page loaded.
122
+ if (!failure && multilingual && to.query.lang !== locale.value) {
123
+ router.replace({ query: { ...to.query, lang: locale.value } })
124
+ }
125
+ })
126
+ router.onError((error, to) => settle(to))
127
+
128
+ // A change of language from the switcher rewrites the URL. Not while a
129
+ // navigation is under way (see `pending`), and so not before the first one
130
+ // has resolved: the router is then still at its start location, `/`, and a
131
+ // replace from there cancels the navigation the visitor arrived with, so
132
+ // every deep link landed on the home page. The guard above puts the
133
+ // language in the URL of that first navigation itself.
115
134
  watch(
116
135
  locale,
117
136
  (code) => {
118
137
  applyDocumentLanguage(code)
119
138
  storeLanguage(code)
120
139
  const current = router.currentRoute.value
121
- if (multilingual && current !== START_LOCATION && current.query.lang !== code) {
140
+ if (multilingual && !pending && current !== START_LOCATION && current.query.lang !== code) {
122
141
  router.replace({ query: { ...current.query, lang: code } })
123
142
  }
124
143
  },