Visitar URL original
fix(dev-toolbar): keep app canvas content across client-side navigati… · withastro/astro@eaf70fa · GitHub
Skip to content

Commit eaf70fa

Browse files
SudoDevStudioSudoDevStudio
andauthored
fix(dev-toolbar): keep app canvas content across client-side navigation (#17905)
Co-authored-by: SudoDevStudio <sudodevstudio@Maninderpreets-MacBook-Pro-2.local>
1 parent 453c050 commit eaf70fa

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎.changeset/lucky-hoops-repair.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes custom dev toolbar apps losing their UI after client-side navigation with `<ClientRouter />`.

‎packages/astro/e2e/dev-toolbar.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,30 @@ test.describe('Dev Toolbar', () => {
358358
await expect(myAppWindow).not.toBeVisible();
359359
});
360360

361+
test('third-party app content survives a client-side navigation', async ({ page, astro }) => {
362+
await page.goto(astro.resolveUrl('/view-transition-a'));
363+
364+
const toolbar = page.locator('astro-dev-toolbar');
365+
const appButton = toolbar.locator('button[data-app-id="my-plugin"]');
366+
await appButton.click();
367+
368+
const myAppCanvas = toolbar.locator('astro-dev-toolbar-app-canvas[data-app-id="my-plugin"]');
369+
const myAppWindow = myAppCanvas.locator('astro-dev-toolbar-window');
370+
await expect(myAppWindow).toHaveCount(1);
371+
372+
await page.click('#go-to-b');
373+
374+
// Wait for content that only exists on the second page, so the swap has
375+
// definitely finished before the canvas is inspected.
376+
await expect(page.locator('#go-to-a')).toBeVisible();
377+
378+
// Navigating re-appends the toolbar into the swapped body, which reconnects
379+
// every app canvas. The app rendered its content once in `init()` and is
380+
// never initialized again, so the canvas must not discard it.
381+
await expect(myAppWindow).toHaveCount(1);
382+
await expect(myAppWindow).toBeVisible();
383+
});
384+
361385
test('islands include their server and client render time', async ({ page, astro }) => {
362386
await page.goto(astro.resolveUrl('/'));
363387

‎packages/astro/src/runtime/client/dev-toolbar/toolbar.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,19 @@ export class AstroDevToolbar extends HTMLElement {
577577

578578
export class DevToolbarCanvas extends HTMLElement {
579579
shadowRoot: ShadowRoot;
580+
hasRenderedStyles = false;
580581

581582
constructor() {
582583
super();
583584
this.shadowRoot = this.attachShadow({ mode: 'open' });
584585
}
585586

587+
// Skip re-initialization when the element reconnects. With <ClientRouter />,
588+
// each client-side navigation moves the toolbar into the new document, so
589+
// connectedCallback runs again for every canvas it holds.
586590
connectedCallback() {
591+
if (this.hasRenderedStyles) { return };
592+
this.hasRenderedStyles = true;
587593
this.shadowRoot.innerHTML = `
588594
<style>
589595
:host {

0 commit comments

Comments
 (0)