Skip to content

Commit

Permalink
fix: normalize iframe preview urls (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Jul 17, 2024
1 parent 5e6065e commit 344e5a0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-chairs-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root-cms': patch
---

fix: normalize iframe preview urls
2 changes: 1 addition & 1 deletion docs/root.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default defineConfig({
},
},
server: {
trailingSlash: false,
trailingSlash: true,
sessionCookieSecret: process.env.COOKIE_SECRET,
},
plugins: [
Expand Down
3 changes: 3 additions & 0 deletions packages/root-cms/core/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export async function renderApp(
base: rootConfig.base || '/',
gci: gci,
i18n: rootConfig.i18n,
server: {
trailingSlash: rootConfig.server?.trailingSlash,
},
},
firebaseConfig: cmsConfig.firebaseConfig,
gapi: cmsConfig.gapi,
Expand Down
27 changes: 18 additions & 9 deletions packages/root-cms/ui/pages/DocumentPage/DocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ DocumentPage.Preview = (props: PreviewProps) => {
window.__ROOT_CTX.rootConfig.domain ||
'https://example.com';
const servingPath = getDocServingPath({collectionId, slug});

const basePreviewPath = getDocPreviewPath({collectionId, slug});
const servingUrl = `${domain}${servingPath}`;

const [iframeUrl, setIframeUrl] = useState(servingUrl);
const [device, setDevice] = useLocalStorage<Device>(
'root::DocumentPage::preview::device',
Expand All @@ -175,8 +176,8 @@ DocumentPage.Preview = (props: PreviewProps) => {
const searchParams = new URLSearchParams(window.location.search);
searchParams.set('preview', 'true');
const query = `${searchParams.toString()}${window.location.hash}`;
const previewPath = `${getDocPreviewPath({collectionId, slug})}?${query}`;
const localizedPreviewPath = `${getDocPreviewPath({
const previewUrl = `${basePreviewPath}?${query}`;
const localizedPreviewUrl = `${getDocPreviewPath({
collectionId,
slug,
locale: selectedLocale,
Expand All @@ -194,8 +195,8 @@ DocumentPage.Preview = (props: PreviewProps) => {
const iframe = iframeRef.current!;
iframe.src = 'about:blank';
window.setTimeout(() => {
if (iframe.src !== localizedPreviewPath) {
iframe.src = localizedPreviewPath;
if (iframe.src !== localizedPreviewUrl) {
iframe.src = localizedPreviewUrl;
} else {
iframe.contentWindow!.location.reload();
}
Expand All @@ -209,8 +210,16 @@ DocumentPage.Preview = (props: PreviewProps) => {
if (!iframeWindow) {
return;
}
if (!iframeWindow.location.href.startsWith('about:blank')) {
// Whenever the iframe url changes (e.g. user navigates to a different
// page), update the iframe url bar. Note that if the preview path is
// different than the serving path, then the serving path will always be
// displayed regardless of the iframe url changes.
if (
basePreviewPath === servingPath &&
!iframeWindow.location.href.startsWith('about:blank')
) {
const currentPath = iframeWindow.location.pathname;
console.log(`iframe url change: ${currentPath}`);
setIframeUrl(`${domain}${currentPath}`);
}
}
Expand All @@ -228,7 +237,7 @@ DocumentPage.Preview = (props: PreviewProps) => {

useEffect(() => {
const iframe = iframeRef.current!;
iframe.src = localizedPreviewPath;
iframe.src = localizedPreviewUrl;
}, [selectedLocale]);

function toggleDevice(device: Device) {
Expand All @@ -246,7 +255,7 @@ DocumentPage.Preview = (props: PreviewProps) => {
}

function openNewTab() {
const tab = window.open(localizedPreviewPath, '_blank');
const tab = window.open(localizedPreviewUrl, '_blank');
if (tab) {
tab.focus();
}
Expand Down Expand Up @@ -371,7 +380,7 @@ DocumentPage.Preview = (props: PreviewProps) => {
</div>
)}
<div className="DocumentPage__main__previewFrame__iframeWrap">
<iframe ref={iframeRef} src={previewPath} />
<iframe ref={iframeRef} src={previewUrl} />
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions packages/root-cms/ui/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ declare global {
urlFormat?: string;
groups?: Record<string, {label?: string; locales: string[]}>;
};
server: {
trailingSlash?: boolean;
};
};
firebaseConfig: Record<string, string>;
gapi?: {
Expand Down
8 changes: 7 additions & 1 deletion packages/root-cms/ui/utils/doc-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ function formatUrlPath(urlFormat: string, params: Record<string, string>) {
return params[key] ?? match;
}
);
return normalizeUrlPath(urlPath);
return normalizeUrlPath(urlPath, {
trailingSlash: window.__ROOT_CTX.rootConfig.server.trailingSlash,
});
}

function normalizeUrlPath(
Expand All @@ -132,5 +134,9 @@ function normalizeUrlPath(
if (!urlPath.startsWith('/')) {
urlPath = `/${urlPath}`;
}
// Add trailing slash if needed.
if (options?.trailingSlash && !urlPath.endsWith('/')) {
urlPath = `${urlPath}/`;
}
return urlPath;
}

0 comments on commit 344e5a0

Please sign in to comment.