Skip to content

Commit 533d676

Browse files
committed
[PHP Playground] Dont reload on first code run, ensure result refreshing after every re-run
1 parent b0b9b70 commit 533d676

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/playground/website/public/php-playground.html

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ <h2>💡 Tips</h2>
831831
let client = null;
832832
let currentPhpVersion = phpVersionSelect.value;
833833
let currentWpVersion = wpVersionSelect.value;
834+
let bootPromise = Promise.resolve();
835+
let runCounter = 0;
834836

835837
// Detect platform for keyboard shortcut display
836838
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
@@ -879,9 +881,9 @@ <h2>💡 Tips</h2>
879881
option.textContent = version;
880882
wpVersionSelect.appendChild(option);
881883
}
882-
// Restore selection if still available; otherwise fall back to latest
883-
wpVersionSelect.value = keys.includes(startingWpVersion)
884-
? startingWpVersion
884+
// Use the requested version if available; otherwise fall back to latest
885+
wpVersionSelect.value = keys.includes(wpVersion)
886+
? wpVersion
885887
: latest;
886888
} catch (e) {
887889
console.warn(
@@ -892,6 +894,9 @@ <h2>💡 Tips</h2>
892894
await client.isReady;
893895
await client.writeFile('/wordpress/code.php', startingCode);
894896
await client.goTo('/code.php'); // Blank document
897+
// Sync current versions after boot to avoid unnecessary reboots on first run
898+
currentPhpVersion = phpVersionSelect.value;
899+
currentWpVersion = wpVersionSelect.value;
895900
}
896901

897902
async function executeCode() {
@@ -902,17 +907,25 @@ <h2>💡 Tips</h2>
902907
// Save code, PHP version, and WordPress version to URL fragment immediately when running
903908
saveStateToURL(code, phpVersion, wpVersion);
904909

910+
// Ensure initial boot finished before proceeding
911+
await bootPromise;
912+
905913
if (
906914
phpVersion !== currentPhpVersion ||
907915
wpVersion !== currentWpVersion
908916
) {
909917
currentPhpVersion = phpVersion;
910918
currentWpVersion = wpVersion;
911-
await bootPlayground(currentPhpVersion, currentWpVersion);
919+
bootPromise = bootPlayground(
920+
currentPhpVersion,
921+
currentWpVersion
922+
);
923+
await bootPromise;
912924
}
913925
await client.writeFile('/wordpress/code.php', code);
914-
const uuid = crypto.randomUUID();
915-
await client.goTo('/code.php?' + uuid);
926+
runCounter += 1;
927+
const cacheBuster = 'run=' + runCounter + '-' + Date.now();
928+
await client.goTo('/code.php?' + cacheBuster);
916929
}
917930

918931
runBtn.addEventListener('click', executeCode);
@@ -966,7 +979,7 @@ <h2>💡 Tips</h2>
966979
}
967980

968981
// Initial boot
969-
bootPlayground(currentPhpVersion, currentWpVersion);
982+
bootPromise = bootPlayground(currentPhpVersion, currentWpVersion);
970983
</script>
971984
</body>
972985
</html>

0 commit comments

Comments
 (0)