Skip to content

Commit b3b61de

Browse files
committed
Add example
1 parent af05ce6 commit b3b61de

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,41 @@ async function getSpaMeta(url) {
135135
getSpaMeta('https://my-single-page-app.com');
136136
```
137137

138+
### Dynamic User-Agent Example
139+
140+
The default User-Agent is static. To avoid being blocked and to mimic a real, up-to-date browser, you can supply a fresh User-Agent for each request using a helper library like `user-agents`.
141+
142+
First, install the library:
143+
144+
```bash
145+
npm install user-agents
146+
```
147+
148+
Then, generate a new User-Agent and pass it in the `fetch` options:
149+
150+
```javascript
151+
import metafetch from 'metafetch';
152+
import UserAgent from 'user-agents';
153+
154+
async function getMetaWithFreshAgent(url) {
155+
try {
156+
// Generate a new, random, and up-to-date User-Agent string.
157+
const userAgent = new UserAgent();
158+
console.log('Using User-Agent:', userAgent.toString());
159+
160+
const meta = await metafetch.fetch(url, {
161+
userAgent: userAgent.toString()
162+
});
163+
164+
console.log(meta.title);
165+
} catch (err) {
166+
console.error(err);
167+
}
168+
}
169+
170+
getMetaWithFreshAgent('https://example.com');
171+
```
172+
138173
## API
139174

140175
### `metafetch.fetch(url, [options])`

0 commit comments

Comments
 (0)