Skip to content

Commit

Permalink
docs(other-api/serve): add preserve cache in development example (rem…
Browse files Browse the repository at this point in the history
…ix-run#4066)

Co-authored-by: Mehdi Achour <machour@gmail.com>
Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
Co-authored-by: Mehdi Achour <machour@gmail.com>
  • Loading branch information
3 people authored and freeman committed Sep 7, 2022
1 parent ce396bf commit 8864835
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -426,3 +426,4 @@
- zachdtaylor
- zainfathoni
- zhe
- jmorel88
27 changes: 26 additions & 1 deletion docs/other-api/serve.md
Expand Up @@ -32,7 +32,32 @@ In development, `remix-serve` will ensure the latest code is run by purging the
}

const record = await fakeDb.stuff.find(params.foo);
cache.set(params.foo, res);
cache.set(params.foo, record);
return json(record);
}
```

If you need a workaround for preserving cache in development, you can store it in the global variable.

```ts lines=[1-9]
// since the cache is stored in global it will only
// be recreated when you restart your dev server.
const cache = (() => {
if (!global.uniqueCacheName) {
global.uniqueCacheName = new Map();
}

return global.uniqueCacheName;
})


export async function loader({ params }) {
if (cache.has(params.foo)) {
return json(cache.get(params.foo));
}

const record = await fakeDb.stuff.find(params.foo);
cache.set(params.foo, record);
return json(record);
}
```
Expand Down

0 comments on commit 8864835

Please sign in to comment.