Skip to content

Commit 77d3c62

Browse files
author
Maurice Snip
committed
docs: add non-browser environment example
1 parent d54b23c commit 77d3c62

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ navigationClient
8787
});
8888
```
8989

90+
## Non-browser environments
91+
92+
By default, `Client` is intended to work in a browser environment. For non-browser environments, like Static Site Generation with Next.js or Astro, you need to opt-out from using the default `baseUrl` and `fetchClient` which rely on the `window` object. For example:
93+
94+
```javascript
95+
import Client from 'sulu-headless-api-client';
96+
97+
// Create client
98+
const client = new Client({
99+
baseUrl: process.env.SULU_BASE_URL,
100+
fetchClient: fetch,
101+
});
102+
```
103+
90104
## Error handling
91105

92106
```javascript
@@ -128,7 +142,7 @@ Creates a client.
128142
| `options` | No | `Object` | `{}` | The options for the client. |
129143
| `options.basePath` | No | `String` | `/api` | The base path of the API. |
130144
| `options.baseUrl` | No | `String` | `window.location.pathname` | The base URL of the API. |
131-
| `options.fetchClient` | No | `Function` | `fetch` | The fetch client to use. Tested with `fetch` and `axios`. |
145+
| `options.fetchClient` | No | `Function` | `fetch.bind(window)` | The fetch client to use. Tested with `fetch` and `axios`. |
132146
| `options.fetchOptions` | No | `Object` | `{}` | The options for the fetch client. |
133147
| `options.locale` | No | `String` | `''` | The locale for every request. |
134148
| `options.onError` | No | `Function(Error)` | `() => {}` | The function to call on error. |

0 commit comments

Comments
 (0)