You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/browser-rendering/platform/playwright.mdx
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,10 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
36
36
37
37
Make sure you have the [browser binding](/browser-rendering/platform/wrangler/#bindings) configured in your `wrangler.toml` file:
38
38
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
+
39
43
<WranglerConfig>
40
44
41
45
```toml
@@ -181,6 +185,63 @@ export default {
181
185
};
182
186
```
183
187
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
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