![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
replicache
Advanced tools
This tutorial walks through creating a basic offline-first todo app with Replicache. If you have any problems or questions, please join us on Slack. We'd be happy to help.
You can also skip to the end and check out the full working version of this sample.
Note: This document assumes you already know what Replicache is, why you might need it, and broadly how it works. If that's not true check out the design document for a detailed deep-dive.
npm install replicache
Replicache ships with both ES6 and CommonJS modules. For simplicity, these examples use ES6.
<script type="module">
import Replicache from './node_modules/replicache/out/mod.js';
var rep = new Replicache({
// URL of the diff server to use. The diff server periodically fetches
// the "client view" from your service and forwards any delta to the
// client. You can use our hosted diff server (as here) or a local diff
// server, which is useful during development. See
// https://github.com/rocicorp/replicache#server-side for more
// information on setting up your client view.
diffServerURL: 'https://serve.replicache.dev/pull',
// Auth token for the diff server, if any.
diffServerAuth: '1',
// URL of your service's Replicache batch endpoint. Replicache
// will send batches of mutations here for application.
batchURL: 'https://replicache-sample-todo.now.sh/serve/replicache-batch',
// Auth token for your client view and batch endpoints, if any.
dataLayerAuth: '2',
});
</script>
Use subscribe()
to open standing queries. Replicache fires onData
whenever the result of the query changes, either because of local changes or sync.
rep.subscribe(
async tx => {
return await toArray(tx.scan({prefix: '/todo/'}));
},
{
onData: result => {
// Using lit-html, but the principle is the same in any UI framework.
// See https://github.com/rocicorp/replicache-sdk-js/tree/master/sample/cal
// for an example using React.
const toggle = complete =>
html`<td><input type="checkbox" .checked=${complete} /></td>`;
const title = text => html`<td>${text}</td>`;
const row = todo =>
html`<tr>
${toggle(todo.complete)}${title(todo.text)}
</tr>`;
render(
html`<table>
${result.map(row)}
</table>`,
document.body,
);
},
},
);
Register client-side mutators using register()
.
Mutators run completely locally, without waiting on the server — online, offline, whatever! A record of the mutation is queued and sent to your service's batch endpoint when possible.
Replicache also invokes mutators itself, during sync, to replay unacknowledged changes on top of newly received server state.
const updateTodo = rep.register('updateTodo', async (tx, {id, complete}) => {
const key = `/todo/${id}`;
const todo = await tx.get(key);
todo.complete = complete;
await tx.put(key, todo);
});
const handleCheckbox = async (id, e) => {
await updateTodo({id, complete: e.srcElement.checked});
};
That's it! You've built a fully-functioning offline-first todo app against our sample backend. What will you do next?
FAQs
Realtime sync for any backend stack
The npm package replicache receives a total of 4,491 weekly downloads. As such, replicache popularity was classified as popular.
We found that replicache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.