Skip to content

Commit 5a95237

Browse files
authored
fix(ui): replace hard coded path to API with serverURL and routes.api (#10618)
Fixes #10617
1 parent 7d10e1b commit 5a95237

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/ui/src/providers/Locale/index.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ export const LocaleLoadingContext = createContext({
1919

2020
const fetchPreferences = async <T extends Record<string, unknown> | string>(
2121
key: string,
22+
baseURL: string,
2223
): Promise<{ id: string; value: T }> =>
23-
await fetch(`/api/payload-preferences/${key}`, {
24+
await fetch(`${baseURL}/payload-preferences/${key}`, {
2425
credentials: 'include',
2526
headers: {
2627
'Content-Type': 'application/json',
@@ -38,7 +39,11 @@ export const LocaleProvider: React.FC<{ children?: React.ReactNode; locale?: Loc
3839
locale: initialLocaleFromPrefs,
3940
}) => {
4041
const {
41-
config: { localization = false },
42+
config: {
43+
localization = false,
44+
routes: { api: apiRoute },
45+
serverURL,
46+
},
4247
} = useConfig()
4348

4449
const { user } = useAuth()
@@ -80,6 +85,8 @@ export const LocaleProvider: React.FC<{ children?: React.ReactNode; locale?: Loc
8085
prevLocale.current = locale
8186
}, [locale])
8287

88+
const fetchURL = `${serverURL}${apiRoute}`
89+
8390
useEffect(() => {
8491
/**
8592
* This effect should only run when `localeFromParams` changes, i.e. when the user clicks an anchor link
@@ -90,7 +97,7 @@ export const LocaleProvider: React.FC<{ children?: React.ReactNode; locale?: Loc
9097
if (localization && user?.id) {
9198
const localeToUse =
9299
localeFromParams ||
93-
(await fetchPreferences<Locale['code']>('locale')?.then((res) => res.value)) ||
100+
(await fetchPreferences<Locale['code']>('locale', fetchURL)?.then((res) => res.value)) ||
94101
defaultLocale
95102

96103
const newLocale =
@@ -102,7 +109,7 @@ export const LocaleProvider: React.FC<{ children?: React.ReactNode; locale?: Loc
102109
}
103110

104111
void resetLocale()
105-
}, [defaultLocale, getPreference, localization, localeFromParams, user?.id])
112+
}, [defaultLocale, getPreference, localization, fetchURL, localeFromParams, user?.id])
106113

107114
return (
108115
<LocaleContext.Provider value={locale}>

0 commit comments

Comments
 (0)