Skip to content

Commit 9b0c57d

Browse files
authored
feat: preserve doc preview locale with url param (#758)
1 parent 7a49ed8 commit 9b0c57d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

.changeset/four-baboons-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@blinkk/root-cms': patch
3+
---
4+
5+
feat: preserve doc preview locale with url param (#758)

packages/root-cms/ui/pages/DocumentPage/DocumentPage.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ import {
2121
SplitPanel,
2222
useSplitPanel,
2323
} from '../../components/SplitPanel/SplitPanel.js';
24-
import {
25-
DraftDocProvider,
26-
useDraftDocData,
27-
useDraftDoc,
28-
} from '../../hooks/useDraftDoc.js';
24+
import {DraftDocProvider, useDraftDoc} from '../../hooks/useDraftDoc.js';
2925
import {useLocalStorage} from '../../hooks/useLocalStorage.js';
26+
import {useStringParam} from '../../hooks/useQueryParam.js';
3027
import {Layout} from '../../layout/Layout.js';
3128
import {joinClassNames} from '../../utils/classes.js';
3229
import {getDocPreviewPath, getDocServingPath} from '../../utils/doc-urls.js';
33-
import {CMSDoc} from '../../utils/doc.js';
3430

3531
interface DocumentPageProps {
3632
collection: string;
@@ -46,6 +42,13 @@ function getPreviewUrl(
4642
const basePreviewPath = getDocPreviewPath({collectionId, slug});
4743
const searchParams = new URLSearchParams(window.location.search);
4844
searchParams.set('preview', 'true');
45+
// Avoid passing through the ?locale= param (set by the CMS to preserve the
46+
// preview frame locale).
47+
// NOTE(stevenle): if we ever need to pass through the locale param, switch to
48+
// using hash params for the internal CMS params that shouldn't pass through.
49+
if (searchParams.has('locale')) {
50+
searchParams.delete('locale');
51+
}
4952
const query = `${searchParams.toString()}${window.location.hash}`;
5053
if (selectedLocale) {
5154
const localizedPreviewPath = getDocPreviewPath({
@@ -274,7 +277,7 @@ DocumentPage.Preview = (props: PreviewProps) => {
274277
'--iframe-height': '100%',
275278
'--iframe-scale': '1',
276279
});
277-
const [selectedLocale, setSelectedLocale] = useState('');
280+
const [selectedLocale, setSelectedLocale] = useStringParam('locale', '');
278281
const iframeRef = useRef<HTMLIFrameElement>(null);
279282

280283
const locales = draft.controller!.getLocales() || [];

0 commit comments

Comments
 (0)