Skip to content

Commit 07513f9

Browse files
Fix path of PR preview URL in production (#2014)
## Motivation for the change, related issues This fixes a problem with the PR preview URLs created by the PR preview modals in production. Before this fix, a preview URL created by the modal in production looks like ``` https://playground.wordpress.net/null./?core-pr=7757#... ``` cc @ajotka ## Implementation details This PR uses the current app location path as a basis for the PR preview URL. ## Testing Instructions (or ideally a Blueprint) - CI - Test PR preview in web app with `npm run dev` - Run `npm run build`, run `php -S localhost:8888 -t dist/packages/playground/wasm-wordpress-net`, and manually test PR preview for WP PRs
1 parent c795006 commit 07513f9

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

packages/playground/website/src/github/preview-pr/form.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import ModalButtons from '../../components/modal/modal-buttons';
77
import type { Blueprint } from '@wp-playground/blueprints';
88

99
interface PreviewPRFormProps {
10-
onImported: () => void;
1110
onClose: () => void;
1211
target: 'wordpress' | 'gutenberg';
1312
}
@@ -28,7 +27,6 @@ export const targetParams = {
2827
};
2928

3029
export default function PreviewPRForm({
31-
onImported,
3230
onClose,
3331
target = 'wordpress',
3432
}: PreviewPRFormProps) {
@@ -158,14 +156,6 @@ export default function PreviewPRForm({
158156
},
159157
steps: [],
160158
};
161-
const encoded = JSON.stringify(blueprint);
162-
let localhost = null;
163-
const isLocalhost =
164-
window.location.hostname === 'localhost' ||
165-
window.location.hostname === '127.0.0.1';
166-
if (isLocalhost) {
167-
localhost = /website-server/;
168-
}
169159

170160
if (target === 'wordpress') {
171161
// [wordpress] Passthrough the mode query parameter if it exists
@@ -174,12 +164,16 @@ export default function PreviewPRForm({
174164
targetParams.set('mode', urlParams.get('mode') as string);
175165
}
176166
targetParams.set('core-pr', prNumber);
177-
window.location.href =
178-
localhost +
179-
'./?' +
180-
targetParams.toString() +
181-
'#' +
182-
encodeURI(encoded);
167+
168+
const blueprintJson = JSON.stringify(blueprint);
169+
const urlWithPreview = new URL(
170+
window.location.pathname,
171+
window.location.href
172+
);
173+
urlWithPreview.search = targetParams.toString();
174+
urlWithPreview.hash = encodeURI(blueprintJson);
175+
176+
window.location.href = urlWithPreview.toString();
183177
} else if (target === 'gutenberg') {
184178
// [gutenberg] If there's a import-site query parameter, pass that to the blueprint
185179
const urlParams = new URLSearchParams(window.location.search);
@@ -201,15 +195,16 @@ export default function PreviewPRForm({
201195
logger.error('Invalid import-site URL');
202196
}
203197

204-
const encoded = JSON.stringify(blueprint);
205-
window.location.href =
206-
localhost +
207-
'./?gutenberg-pr=' +
208-
prNumber +
209-
'#' +
210-
encodeURI(encoded);
198+
const blueprintJson = JSON.stringify(blueprint);
199+
200+
const urlWithPreview = new URL(
201+
window.location.pathname,
202+
window.location.href
203+
);
204+
urlWithPreview.searchParams.set('gutenberg-pr', prNumber);
205+
urlWithPreview.hash = encodeURI(blueprintJson);
211206

212-
onImported();
207+
window.location.href = urlWithPreview.toString();
213208
}
214209
}
215210

packages/playground/website/src/github/preview-pr/modal.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@ export function PreviewPRModal({ target }: PreviewPRModalProps) {
1818
const closeModal = () => {
1919
dispatch(setActiveModal(null));
2020
};
21-
function handleImported() {
22-
closeModal();
23-
}
2421
return (
2522
<Modal
2623
small
2724
title={`Preview a ${targetName[target]} PR`}
2825
onRequestClose={closeModal}
2926
>
30-
<PreviewPRForm
31-
onClose={closeModal}
32-
onImported={handleImported}
33-
target={target}
34-
/>
27+
<PreviewPRForm onClose={closeModal} target={target} />
3528
</Modal>
3629
);
3730
}

0 commit comments

Comments
 (0)