diff --git a/src/window/index.ts b/src/window/index.ts index 85f1b4431..723844ace 100644 --- a/src/window/index.ts +++ b/src/window/index.ts @@ -2998,10 +2998,13 @@ export class Window { try { iframe.contentWindow?.location.reload(); } catch { - // Cross-origin or detached frame — re-assign src - // as a fallback. The current src is what's already - // loaded, so re-setting it triggers a fresh load. - iframe.src = iframe.src; + // An isolated page (the block editor sends + // `Document-Isolation-Policy`) or a detached frame. + // Load the URL the page last reported, not `src`: + // the editor rewrites its own URL on a first save, + // and `src` would reload post-new.php, an empty new + // draft instead of the post that was just saved. + iframe.src = withChromelessParam( reloadedUrl ) ?? iframe.src; } }; } else { diff --git a/tests/vitest/window-unsaved-navigation.test.ts b/tests/vitest/window-unsaved-navigation.test.ts index ed93300f2..2b81c5686 100644 --- a/tests/vitest/window-unsaved-navigation.test.ts +++ b/tests/vitest/window-unsaved-navigation.test.ts @@ -187,6 +187,29 @@ describe( 'a navigation the page inside can refuse', () => { expect( win._deferredNavigationCommit ).toBeNull(); } ); + test( 'reload of a frame it cannot reach loads the URL the page reported', async () => { + // The block editor is sent with `Document-Isolation-Policy`, so + // the shell cannot call its `location.reload()`, and a first save + // moves it from post-new.php to the saved post without touching + // `src`. Reloading `src` would open an empty new draft. + const iframe = win.iframe as HTMLIFrameElement; + iframe.src = ADMIN + 'post-new.php?openstation_chromeless=1'; + win._reportedLocation = { + url: ADMIN + 'post.php?post=52&action=edit', + src: iframe.src, + }; + + win.reload(); + await vi.waitFor( () => expect( asked ).toHaveLength( 1 ) ); + answerGuard( false ); + + await vi.waitFor( () => + expect( iframe.src ).toBe( + ADMIN + 'post.php?post=52&action=edit&openstation_chromeless=1', + ), + ); + } ); + test( 'a second reload click cannot slip past the in-flight query', async () => { win.reload(); await vi.waitFor( () => expect( asked ).toHaveLength( 1 ) );