-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support setting timeout for
client:idle
(#11743)
* feat: support setting timeout for `client:idle` * tst: add client:idle timeout e2e test * Update .changeset/clever-emus-roll.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update .changeset/clever-emus-roll.md Co-authored-by: Sarah Rainsberger <[email protected]> * nit: we wait for times, not values! --------- Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
5af8b4f
commit cce0894
Showing
9 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Adds a new, optional property `timeout` for the `client:idle` directive. | ||
|
||
This value allows you to specify a maximum time to wait, in milliseconds, before hydrating a UI framework component, even if the page is not yet done with its initial load. This means you can delay hydration for lower-priority UI elements with more control to ensure your element is interactive within a specified time frame. | ||
|
||
```astro | ||
<ShowHideButton client:idle={{timeout: 500}} /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expect } from '@playwright/test'; | ||
import { testFactory, waitForHydrate } from './test-utils.js'; | ||
|
||
const test = testFactory({ root: './fixtures/client-idle-timeout/' }); | ||
|
||
let devServer; | ||
|
||
test.beforeAll(async ({ astro }) => { | ||
devServer = await astro.startDevServer(); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
test.describe('Client idle timeout', () => { | ||
test('React counter', async ({ astro, page }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
|
||
const counter = page.locator('#react-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = counter.locator('pre'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
await waitForHydrate(page, counter); | ||
|
||
const inc = counter.locator('.increment'); | ||
await inc.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/astro/e2e/fixtures/client-idle-timeout/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import react from '@astrojs/react'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [ | ||
react(), | ||
], | ||
}); |
13 changes: 13 additions & 0 deletions
13
packages/astro/e2e/fixtures/client-idle-timeout/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@e2e/client-idle-timeout", | ||
"version": "0.0.0", | ||
"private": true, | ||
"devDependencies": { | ||
"@astrojs/react": "workspace:*", | ||
"astro": "workspace:*" | ||
}, | ||
"dependencies": { | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/astro/e2e/fixtures/client-idle-timeout/src/components/Counter.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React, { useState } from 'react'; | ||
|
||
export default function Counter({ children, count: initialCount = 0, id }) { | ||
const [count, setCount] = useState(initialCount); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<> | ||
<div id={id} className="counter"> | ||
<button className="decrement" onClick={subtract}>-</button> | ||
<pre>{count}</pre> | ||
<button className="increment" onClick={add}>+</button> | ||
</div> | ||
<div className="counter-message">{children}</div> | ||
</> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/astro/e2e/fixtures/client-idle-timeout/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
import Counter from '../components/Counter.jsx'; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
</head> | ||
<body> | ||
<main> | ||
<Counter id="react-counter" client:idle={{timeout: 200}}></Counter> | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.