Skip to content

Commit

Permalink
docs: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 25, 2021
1 parent b5b34fe commit 62004a0
Show file tree
Hide file tree
Showing 20 changed files with 1,541 additions and 739 deletions.
55 changes: 0 additions & 55 deletions examples/create-a-client.md

This file was deleted.

117 changes: 0 additions & 117 deletions examples/custom-caching.md

This file was deleted.

9 changes: 9 additions & 0 deletions examples/custom-caching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Remember that in the browser, caching is enabled by default by using the global
function. Browsers will automatically utilize their caching mechanism unless it
is disabled by a user.

**Note**: This example is written for Node.js. If you plan to use this code for
the browser, you can remove `node-fetch` from the example.

```diff
import * as prismic from '@prismicio/client'
- import fetch from 'node-fetch'
import QuickLRU from 'quick-lru'
```

## How to run the example

```sh
Expand Down
10 changes: 5 additions & 5 deletions examples/custom-caching/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as prismic from '@prismicio/client'
import fetch from 'node-fetch'
import QuickLRU from 'quick-lru'

const endpoint = prismic.getEndpoint('my-repo')
const endpoint = prismic.getEndpoint('qwerty')
const cache = new QuickLRU({
maxAge: 10000, // 10 seconds
maxSize: 1000, // 1000 entries
})

export const client = prismic.createClient(endpoint, {
const client = prismic.createClient(endpoint, {
fetch: async (url, options) => {
// The cache key contains the requested URL and headers
const key = JSON.stringify({ url, options })
Expand All @@ -30,9 +30,9 @@ export const client = prismic.createClient(endpoint, {
})

const homepage = await client.getByUID('page', 'home')
console.log(homepage)
console.log('First uncached result: ', homepage)
// => The `page` document with a UID of `home`

const homepageFetchedAgain = await client.getByUID('page', 'home')
console.log(homepageFetchedAgain)
console.log('Second cached result: ', homepageFetchedAgain)
// => This call will use the cache rather than make another network request.
1 change: 1 addition & 0 deletions examples/custom-caching/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/custom-caching/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"type": "module",
"dependencies": {
"@prismicio/client": "^5.0.0",
"node-fetch": "^2.6.1",
"quick-lru": "^6.0.0"
}
}
67 changes: 0 additions & 67 deletions examples/server-usage.md

This file was deleted.

31 changes: 31 additions & 0 deletions examples/server-usage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Server Usage

This example shows how to use `@prismicio/client` on the server. Usage is
identical to usage in the browser, except for the need to provide a `fetch()`
function.

In this example, we're using a library called [node-fetch][node-fetch] to make
network requests. This is a nearly drop-in Node.js implementation for the
browser's `fetch()` function.

For our use-case, its only limitation is the lack of built-in caching. If you
are running in a serverless architecture where instances are started and stopped
per-request, caching is unnecessary. If you are running on a long-living server,
caching would be beneficial. See the [Custom Caching](../custom-caching) example
to see how to setup caching.

## How to run the example

```sh
# Clone the repository to your computer
git clone https://github.com/prismicio/prismic-javascript.git
cd prismic-javascript/examples/server-usage

# Install the dependencies
npm install

# Run the example
node index.js
```

[node-fetch]: https://github.com/node-fetch/node-fetch
11 changes: 4 additions & 7 deletions examples/server-usage/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as prismic from '@prismicio/client'
import got from 'got'
import fetch from 'node-fetch'

const endpoint = prismic.getEndpoint('my-repo')
const endpoint = prismic.getEndpoint('qwerty')
const client = prismic.createClient(endpoint, {
// Here, we provide a way for the client to make network requests.
// Got is one way to make network requests on the server. It has built-in
// caching and automatically retries requests on failures.
fetch: async (url, options) => {
return await got(url, options).json()
},
// `node-fetch` is a Node.js fetch-compatible package.
fetch,
})

const homepage = await client.getByUID('page', 'home')
Expand Down

0 comments on commit 62004a0

Please sign in to comment.