Skip to content

Commit f8592c0

Browse files
committed
docs: add wrangler config example for Playwright storage state persistence
1 parent d8dd6f3 commit f8592c0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,54 @@ export default {
181181
};
182182
```
183183

184+
### Storage state
185+
186+
Playwright supports [storage state](https://playwright.dev/docs/api/class-browsercontext#browsercontext-storage-state) to obtain and persist cookies and other storage data.
187+
188+
Here's an example that uses storage state to persist cookies and other storage data in [KV](https://developers.cloudflare.com/kv):
189+
190+
<WranglerConfig>
191+
192+
```jsonc
193+
{
194+
"name": "storage-state-examples",
195+
"main": "src/index.ts",
196+
"compatibility_flags": ["nodejs_compat"],
197+
"compatibility_date": "2025-09-17",
198+
"browser": {
199+
"binding": "MYBROWSER"
200+
},
201+
"kv_namespaces": [
202+
{
203+
"binding": "KV",
204+
"id": "<YOUR-KV-NAMESPACE-ID>"
205+
}
206+
]
207+
}
208+
```
209+
210+
</WranglerConfig>
211+
212+
```ts title="src/index.ts"
213+
// gets persisted storage state from KV or undefined if it doesn't exist
214+
const storageStateJson = await env.KV.get('storageState');
215+
const storageState = storageStateJson ? await JSON.parse(storageStateJson) as BrowserContextOptions['storageState'] : undefined;
216+
217+
await using browser = await launch(env.MYBROWSER);
218+
// creates a new context with storage state persisted in KV
219+
await using context = await browser.newContext({ storageState });
220+
221+
await using page = await context.newPage();
222+
223+
// do some actions on the page that may update client-side storage
224+
225+
// gets updated storage state: cookies, localStorage and IndexedDB
226+
const updatedStorageState = await context.storageState({ indexedDB: true });
227+
228+
// persist updated storage state in KV
229+
await env.KV.put('storageState', JSON.stringify(updatedStorageState));
230+
```
231+
184232
### Keep Alive
185233

186234
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)