Skip to content

[Website] Update the fake address bar via setInterval() #2225

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

Open
wants to merge 1 commit into
base: merge-playground-private-2
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion packages/playground/remote/src/lib/boot-playground-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export async function bootPlaygroundRemote() {
bar.destroy();
},
async onNavigation(fn) {
// Manage the address bar
// Listen for iframe load events (for navigation)
wpFrame.addEventListener('load', async (e: any) => {
try {
/**
Expand Down Expand Up @@ -169,6 +169,34 @@ export async function bootPlaygroundRemote() {
// so let's ignore it for now and find a correct fix in time.
}
});

// Also propagate navigation changes twice a second for any
// updates we don't receive via the iframe load event.
//
// For more details on the challenges related to the load event,
// see:
//
// * https://github.com/WordPress/wordpress-playground/pull/1945
// * https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
// * https://html.spec.whatwg.org/dev/browsing-the-web.html#centralized-modifications-of-session-history
let lastPath: string | undefined;
setInterval(async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clear the interval after Playground is stopped.
Now, if you start with a temporary Playground, go to the Site Manager, and click on a Saved Playground, two interval loops will run in parallel.

try {
let href = '';
if (wpFrame.contentWindow) {
href = wpFrame.contentWindow.location.href;
} else {
href = wpFrame.src;
}
const path = await playground.internalUrlToPath(href);
if (path !== lastPath) {
lastPath = path;
fn(path);
}
} catch {
// Ignore errors due to CORS or CSP restrictions
}
}, 500);
},
async goTo(requestedPath: string) {
if (!requestedPath.startsWith('/')) {
Expand Down
Loading