summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiDeoo2024-12-17 20:07:28 +0100
committerGitHub2024-12-17 20:07:28 +0100
commitec4b85154ea301d9144ff49f3abd009e3a929387 (patch)
tree8df60a28a6d0dfaa2139808af083219635f7d3d3
parentd127bfb1783945487abb1c871c2de4a004058ebf (diff)
downloadIT.starlight-ec4b85154ea301d9144ff49f3abd009e3a929387.tar.gz
IT.starlight-ec4b85154ea301d9144ff49f3abd009e3a929387.tar.bz2
IT.starlight-ec4b85154ea301d9144ff49f3abd009e3a929387.zip
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>
-rw-r--r--.changeset/rude-lobsters-destroy.md5
-rw-r--r--package.json2
-rw-r--r--packages/starlight/components/LanguageSelect.astro10
-rw-r--r--packages/starlight/components/Select.astro11
4 files changed, 26 insertions, 2 deletions
diff --git a/.changeset/rude-lobsters-destroy.md b/.changeset/rude-lobsters-destroy.md
new file mode 100644
index 00000000..1d36f31f
--- /dev/null
+++ b/.changeset/rude-lobsters-destroy.md
@@ -0,0 +1,5 @@
+---
+'@astrojs/starlight': patch
+---
+
+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.
diff --git a/package.json b/package.json
index f63a9fd3..7682471c 100644
--- a/package.json
+++ b/package.json
@@ -33,7 +33,7 @@
{
"name": "/_astro/*.js",
"path": "examples/basics/dist/_astro/*.js",
- "limit": "23 kB",
+ "limit": "23.5 kB",
"gzip": true
},
{
diff --git a/packages/starlight/components/LanguageSelect.astro b/packages/starlight/components/LanguageSelect.astro
index dd9dbb6c..92a88b8a 100644
--- a/packages/starlight/components/LanguageSelect.astro
+++ b/packages/starlight/components/LanguageSelect.astro
@@ -42,6 +42,16 @@ function localizedPathname(locale: string | undefined): string {
window.location.pathname = e.currentTarget.value;
}
});
+ window.addEventListener('pageshow', (event) => {
+ if (!event.persisted) return;
+ // If the page was loaded from a cache, the language select selected index might not be
+ // in sync with the current page.
+ const markupSelectedIndex =
+ select.querySelector<HTMLOptionElement>('option[selected]')?.index;
+ if (markupSelectedIndex !== select.selectedIndex) {
+ select.selectedIndex = markupSelectedIndex ?? 0;
+ }
+ });
}
}
}
diff --git a/packages/starlight/components/Select.astro b/packages/starlight/components/Select.astro
index 8be16859..50925b1a 100644
--- a/packages/starlight/components/Select.astro
+++ b/packages/starlight/components/Select.astro
@@ -12,12 +12,21 @@ interface Props {
selected: boolean;
}>;
}
+
+/**
+ * The `autocomplete="off"` attribute is used to prevent the browser from automatically selecting a
+ * value for this input, e.g. based on the user's previous selections, as this could lead to
+ * incorrect values being selected for example when the user switches between languages and uses
+ * the back button.
+ * Note that this attribute is only useful when a value is not restored at a later stage, e.g. the
+ * bfcache that ignores this attribute when restoring the value.
+ */
---
<label style={`--sl-select-width: ${Astro.props.width}`}>
<span class="sr-only">{Astro.props.label}</span>
<Icon name={Astro.props.icon} class="icon label-icon" />
- <select value={Astro.props.value}>
+ <select value={Astro.props.value} autocomplete="off">
{
Astro.props.options.map(({ value, selected, label }) => (
<option value={value} selected={selected} set:html={label} />