Skip to content

Commit e9ae8fc

Browse files
committed
[BRAPI]: add storage state example for playwright
1 parent d8dd6f3 commit e9ae8fc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ 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+
```ts
191+
// gets persisted storage state from KV or undefined if it doesn't exist
192+
const storageStateJson = await env.KV_STORAGE_STATE.get('storageState');
193+
const storageState = storageStateJson ? await JSON.parse(storageStateJson) as BrowserContextOptions['storageState'] : undefined;
194+
195+
await using browser = await launch(env.MYBROWSER);
196+
// creates a new context with storage state persisted in KV
197+
await using context = await browser.newContext({ storageState });
198+
199+
await using page = await context.newPage();
200+
await page.goto(`${url.origin}/increment`);
201+
202+
// do some actions on the page that may update client-side storage
203+
204+
// gets updated storage state: cookies, localStorage and IndexedDB
205+
const updatedStorageState = await context.storageState({ indexedDB: true });
206+
207+
// persist updated storage state in KV
208+
await env.KV_STORAGE_STATE.put('storageState', JSON.stringify(updatedStorageState));
209+
```
210+
184211
### Keep Alive
185212

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