Skip to content

Commit 4c1ec06

Browse files
ruifigueirakathaylToriLindsay
authored
[BRAPI]: add storage state example for playwright (#25423)
* docs: add wrangler config example for Playwright storage state persistence * docs: add KV setup for Playwright storage state example * Update playwright.mdx add missing close for WranglerConfig * Update playwright.mdx -update bullets in new storage section -update kv link -add note about mandatory compatibility flags to use Playwright latest version * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> --------- Co-authored-by: Kathy <[email protected]> Co-authored-by: ToriLindsay <[email protected]>
1 parent 4ff06b8 commit 4c1ec06

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/content/docs/browser-rendering/platform/playwright.mdx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
3636

3737
Make sure you have the [browser binding](/browser-rendering/platform/wrangler/#bindings) configured in your `wrangler.toml` file:
3838

39+
:::note
40+
To use the latest version of `@cloudflare/playwright`, your Worker configuration must include the `nodejs_compat` compatibility flag and a `compatibility_date` of 2025-09-15 or later. This change is necessary because the library's functionality requires the native `node.fs` API.
41+
:::
42+
3943
<WranglerConfig>
4044

4145
```toml
@@ -181,6 +185,63 @@ export default {
181185
};
182186
```
183187

188+
### Storage state
189+
190+
Playwright supports [storage state](https://playwright.dev/docs/api/class-browsercontext#browsercontext-storage-state) to obtain and persist cookies and other storage data. In this example, you will use storage state to persist cookies and other storage data in [Workers KV](/kv).
191+
192+
193+
First, ensure you have a KV namespace. You can create a new one with:
194+
195+
```bash
196+
npx wrangler kv namespace create KV
197+
```
198+
199+
Then, add the KV namespace to your `wrangler.toml` file:
200+
201+
<WranglerConfig>
202+
203+
```jsonc
204+
{
205+
"name": "storage-state-examples",
206+
"main": "src/index.ts",
207+
"compatibility_flags": ["nodejs_compat"],
208+
"compatibility_date": "2025-09-17",
209+
"browser": {
210+
"binding": "MYBROWSER"
211+
},
212+
"kv_namespaces": [
213+
{
214+
"binding": "KV",
215+
"id": "<YOUR-KV-NAMESPACE-ID>"
216+
}
217+
]
218+
}
219+
```
220+
221+
</WranglerConfig>
222+
223+
Now, you can use the storage state to persist cookies and other storage data in KV:
224+
225+
```ts title="src/index.ts"
226+
// gets persisted storage state from KV or undefined if it does not exist
227+
const storageStateJson = await env.KV.get('storageState');
228+
const storageState = storageStateJson ? await JSON.parse(storageStateJson) as BrowserContextOptions['storageState'] : undefined;
229+
230+
await using browser = await launch(env.MYBROWSER);
231+
// creates a new context with storage state persisted in KV
232+
await using context = await browser.newContext({ storageState });
233+
234+
await using page = await context.newPage();
235+
236+
// do some actions on the page that may update client-side storage
237+
238+
// gets updated storage state: cookies, localStorage, and IndexedDB
239+
const updatedStorageState = await context.storageState({ indexedDB: true });
240+
241+
// persists updated storage state in KV
242+
await env.KV.put('storageState', JSON.stringify(updatedStorageState));
243+
```
244+
184245
### Keep Alive
185246

186247
If users omit the `browser.close()` statement, the browser instance will stay open, ready to be connected to again and [re-used](/browser-rendering/workers-bindings/reuse-sessions/) but it will, by default, close automatically after 1 minute of inactivity. Users can optionally extend this idle time up to 10 minutes, by using the `keep_alive` option, set in milliseconds:

0 commit comments

Comments
 (0)