Skip to content

Commit ec4b851

Browse files
HiDeoodelucis
andauthored
Fix language selector invalid value (#2635)
Co-authored-by: Chris Swithinbank <357379+delucis@users.noreply.github.com> Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent d127bfb commit ec4b851

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

‎.changeset/rude-lobsters-destroy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Fixes an issue where the language picker in multilingual sites could display the wrong language when navigating between pages with the browser back/forward buttons.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{
3434
"name": "/_astro/*.js",
3535
"path": "examples/basics/dist/_astro/*.js",
36-
"limit": "23 kB",
36+
"limit": "23.5 kB",
3737
"gzip": true
3838
},
3939
{

‎packages/starlight/components/LanguageSelect.astro

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ function localizedPathname(locale: string | undefined): string {
4242
window.location.pathname = e.currentTarget.value;
4343
}
4444
});
45+
window.addEventListener('pageshow', (event) => {
46+
if (!event.persisted) return;
47+
// If the page was loaded from a cache, the language select selected index might not be
48+
// in sync with the current page.
49+
const markupSelectedIndex =
50+
select.querySelector<HTMLOptionElement>('option[selected]')?.index;
51+
if (markupSelectedIndex !== select.selectedIndex) {
52+
select.selectedIndex = markupSelectedIndex ?? 0;
53+
}
54+
});
4555
}
4656
}
4757
}

‎packages/starlight/components/Select.astro

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ interface Props {
1212
selected: boolean;
1313
}>;
1414
}
15+
16+
/**
17+
* The `autocomplete="off"` attribute is used to prevent the browser from automatically selecting a
18+
* value for this input, e.g. based on the user's previous selections, as this could lead to
19+
* incorrect values being selected for example when the user switches between languages and uses
20+
* the back button.
21+
* Note that this attribute is only useful when a value is not restored at a later stage, e.g. the
22+
* bfcache that ignores this attribute when restoring the value.
23+
*/
1524
---
1625

1726
<label style={`--sl-select-width: ${Astro.props.width}`}>
1827
<span class="sr-only">{Astro.props.label}</span>
1928
<Icon name={Astro.props.icon} class="icon label-icon" />
20-
<select value={Astro.props.value}>
29+
<select value={Astro.props.value} autocomplete="off">
2130
{
2231
Astro.props.options.map(({ value, selected, label }) => (
2332
<option value={value} selected={selected} set:html={label} />

0 commit comments

Comments
 (0)