Skip to content

Commit

Permalink
Preserve the wp_theme_preview query arg when navigating in Site Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed May 6, 2024
1 parent ccf9d33 commit ad13f80
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ const POPOVER_PROPS = {
/**
* Internal dependencies
*/
import {
isPreviewingTheme,
currentlyPreviewingTheme,
} from '../../utils/is-previewing-theme';
import { unlock } from '../../lock-unlock';

const { useLocation, useHistory } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -68,9 +64,6 @@ export default function LeafMoreMenu( props ) {
{
postType: attributes.type,
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
},
{
backPath: params,
Expand All @@ -82,9 +75,6 @@ export default function LeafMoreMenu( props ) {
{
postType: 'page',
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
},
{
backPath: params,
Expand Down
8 changes: 4 additions & 4 deletions packages/edit-site/src/utils/use-activate-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const { useHistory, useLocation } = unlock( routerPrivateApis );
*/
export function useActivateTheme() {
const history = useHistory();
const location = useLocation();
const { params } = useLocation();
const { startResolution, finishResolution } = useDispatch( coreStore );

return async () => {
Expand All @@ -36,9 +36,9 @@ export function useActivateTheme() {
startResolution( 'activateTheme' );
await window.fetch( activationURL );
finishResolution( 'activateTheme' );
const { wp_theme_preview: themePreview, ...params } =
location.params;
history.replace( params );
// Remove the wp_theme_preview query param: we've finished activating
// the queue and are switching to normal Site Editor.
history.replace( { ...params, wp_theme_preview: undefined } );
}
};
}
19 changes: 17 additions & 2 deletions packages/router/src/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ const history = createBrowserHistory();
const originalHistoryPush = history.push;
const originalHistoryReplace = history.replace;

// Preserve the `wp_theme_preview` query parameter when navigating
// around the Site Editor.
// TODO: move this hack out of the router into Site Editor code.
function preserveThemePreview( params ) {
if ( params.hasOwnProperty( 'wp_theme_preview' ) ) {
return params;
}
const currentSearch = new URLSearchParams( history.location.search );
const currentThemePreview = currentSearch.get( 'wp_theme_preview' );
if ( currentThemePreview === undefined ) {
return params;
}
return { ...params, wp_theme_preview: currentThemePreview };
}

function push( params, state ) {
const search = buildQueryString( params );
const search = buildQueryString( preserveThemePreview( params ) );
return originalHistoryPush.call( history, { search }, state );
}

function replace( params, state ) {
const search = buildQueryString( params );
const search = buildQueryString( preserveThemePreview( params ) );
return originalHistoryReplace.call( history, { search }, state );
}

Expand Down

0 comments on commit ad13f80

Please sign in to comment.