Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blueprints] Make landingPage handling runtime-specific and move it to PlaygroundViewport #1916

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/playground/blueprints/src/lib/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,6 @@ export function compileBlueprint(
}
}
} finally {
try {
await (playground as any).goTo(
blueprint.landingPage || '/'
);
} catch (e) {
/*
* PHP exposes no goTo method.
* We can't use `goto` in playground here,
* because it may be a Comlink proxy object
* with no such method.
*/
}
progress.finish();
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function EnsurePlaygroundSiteIsSelected({
return;
}

dispatch(setActiveSite(requestedSiteSlug));
dispatch(setActiveSite(requestedSiteSlug, { redirect: false }));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
useAppDispatch,
useAppSelector,
} from '../../lib/state/redux/store';
import { removeClientInfo } from '../../lib/state/redux/slice-clients';
import {
removeClientInfo,
selectClientBySiteSlug,
} from '../../lib/state/redux/slice-clients';
import { bootSiteClient } from '../../lib/state/redux/boot-site-client';
import { SiteError } from '../../lib/state/redux/slice-ui';
import { Button, Spinner } from '@wordpress/components';
Expand Down Expand Up @@ -231,6 +234,34 @@ export const JustViewport = function JustViewport({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [siteSlug, iframeRef, runtimeConfigString]);

const client = useAppSelector((state) =>
selectClientBySiteSlug(state, siteSlug)
);
useEffect(() => {
if (!client) {
return;
}
const url = new URL(window.location.href);
const landingPage =
url.searchParams.get('url') ||
site.metadata?.originalBlueprint?.landingPage ||
'/';
client.goTo(landingPage).catch((e) => {
/*
* PHP exposes no goTo method.
* We can't use `goto` in playground here,
* because it may be a Comlink proxy object
* with no such method.
*/
});
if (site.metadata.storage !== 'none') {
url.searchParams.delete('url');
redirectTo(url.toString());
}
// Only ever re-run this effect once – when the client becomes available.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [!client]);

const error = useAppSelector(selectActiveSiteError);

if (error) {
Expand Down
11 changes: 9 additions & 2 deletions packages/playground/website/src/lib/state/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ export const selectActiveSiteError = (

export const useActiveSite = () => useAppSelector(selectActiveSite);

export const setActiveSite = (slug: string | undefined) => {
export const setActiveSite = (
slug: string | undefined,
{
redirect = true,
}: {
redirect?: boolean;
} = {}
) => {
return (
dispatch: PlaygroundDispatch,
getState: () => PlaygroundReduxState
Expand All @@ -87,7 +94,7 @@ export const setActiveSite = (slug: string | undefined) => {
return;
}
dispatch(__internal_uiSlice.actions.setActiveSite(slug));
if (slug) {
if (slug && redirect) {
const site = selectSiteBySlug(getState(), slug);
redirectTo(PlaygroundRoute.site(site));
}
Expand Down
Loading