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
While you can create a `scratchpad.config.js` file above without installing this package,
62
+
it's recommended to install `@heymp/scratchpad` and use the `defineConfig` helper to access
63
+
the correct types.
64
+
65
+
**Usage:**
66
+
67
+
`scratchpad.config.js`
68
+
```ts
69
+
import { defineConfig } from'@heymp/scratchpad';
70
+
71
+
exportdefaultdefineConfig({
51
72
devtools: true,
73
+
url: 'https://google.com',
74
+
headless: true,
75
+
});
76
+
```
77
+
78
+
#### Reroute Documents
79
+
80
+
The `rerouteDocument` function allows you to replace the HTML content of any webpage with a local HTML file from your system. This is incredibly useful for testing changes or developing components in the context of a live site without deploying your code.
81
+
82
+
When used, `rerouteDocument` also watches your local HTML file for changes. If you save an update to the file, the Playwright page will automatically reload to reflect your latest edits, providing a fast feedback loop.
83
+
84
+
**How it works:**
85
+
86
+
* It intercepts requests made by the Playwright `page` that are for HTML documents.
87
+
* It maps the URL's path to a local file structure. For instance, if you are on `http://example.com/user/profile` and you've set your local directory to `'./my-pages'`, the function will look for `'./my-pages/user/profile/index.html'`.
88
+
89
+
**Usage:**
90
+
91
+
You would typically use this function within the `playwright` async method in your `scratchpad.config.js`:
// Tell Scratchpad to serve local HTML files from the './pages' directory
103
+
// whenever a document is requested.
104
+
awaitrerouteDocument(page, './pages');
105
+
106
+
// Now, if you navigate to https://example.com/some/path,
107
+
// Scratchpad will try to serve './pages/some/path/index.html'.
108
+
// If that file doesn't exist, it will load the original page from the web.
109
+
}
110
+
});
111
+
```
112
+
113
+
#### Reroute URLs
114
+
115
+
The `rerouteUrl` function gives you the power to intercept network requests for specific assets (like JavaScript files, CSS, images, or API calls) and redirect them. You can reroute a `target` URL to either another live `source` URL or to a `source` local file from your system. This is incredibly useful for testing local versions of assets against a live site, mocking API responses, or redirecting to different service endpoints without deploying code.
116
+
117
+
If you save an update to this file, the Playwright page will automatically reload to show your latest edits, creating a very fast development feedback loop.
118
+
119
+
**How it works:**
120
+
121
+
* It uses Playwright's `page.route()` to intercept network requests that match the `target` URL or pattern you specify.
122
+
* If you set `type: 'url'`, it fetches the content from your specified `source` URL instead of the original `target`.
123
+
* If you set `type: 'path'`, it serves the content from your local `source` file. It also starts watching this file, and if any changes are detected, it reloads the page.
124
+
125
+
**Usage:**
126
+
127
+
You typically use `rerouteUrl` within the `playwright` async method in your configuration file (e.g., `scratchpad.config.js`):
128
+
129
+
`scratchpad.config.js`
130
+
```javascript
131
+
import*asScratchpadfrom'@heymp/scratchpad';
132
+
133
+
exportdefaultScratchpad.defineConfig({
134
+
url:'https://www.your-website.com',
135
+
playwright:async (args) => {
136
+
const { page } = args;
137
+
138
+
// Example 1: Reroute a specific JavaScript file to a local version
139
+
awaitScratchpad.rerouteUrl(page, {
140
+
type:'path',
141
+
target:'**/scripts/main-app.js', // Intercept requests for this JS file
142
+
source:'./local-dev/main-app.js'// Serve your local version instead
143
+
});
144
+
145
+
// Example 2: Reroute an API call to a different endpoint
146
+
awaitScratchpad.rerouteUrl(page, {
147
+
type:'url',
148
+
target:'https://api.production.com/data', // Original API endpoint
149
+
source:'https://api.staging.com/data'// Reroute to staging API
150
+
});
151
+
152
+
// Example 3: Reroute an API call to a local mock JSON file
0 commit comments