Skip to content

Commit 95acc2f

Browse files
committed
add cachebusting
1 parent f74422e commit 95acc2f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ const result = await worker.db.exec(`select * from table where id = ?`, [123]);
7777

7878
```
7979

80+
## Cachebusting
81+
82+
Alongside the `url` or `urlPrefix`, config can take an optional `cacheBust` property whose value will be appended as a query parameter to URLs. If you set it to a random value when you update the database you can avoid caching-related database corruption.
83+
84+
If using a remote config (`from: 'jsonconfig'`), don't forget to cachebust that too.
85+
8086
## Debugging data fetching
8187

8288
If your query is fetching a lot of data and you're not sure why, try this:

src/sqlite.worker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export type SplitFileConfigPure = {
5959
};
6060
export type SplitFileConfigInner = {
6161
requestChunkSize: number;
62+
cacheBust?: string;
6263
} & (
6364
| {
6465
serverMode: "chunked";
@@ -145,20 +146,21 @@ const mod = {
145146
config.serverMode === "chunked" ? config.urlPrefix : config.url;
146147
console.log("constructing url database", id);
147148
let rangeMapper: RangeMapper;
149+
let suffix = config.cacheBust ? "?cb=" + config.cacheBust : "";
148150
if (config.serverMode == "chunked") {
149151
rangeMapper = (from: number, to: number) => {
150152
const serverChunkId = (from / config.serverChunkSize) | 0;
151153
const serverFrom = from % config.serverChunkSize;
152154
const serverTo = serverFrom + (to - from);
153155
return {
154-
url: config.urlPrefix + String(serverChunkId).padStart(3, "0"),
156+
url: config.urlPrefix + String(serverChunkId).padStart(3, "0") + suffix,
155157
fromByte: serverFrom,
156158
toByte: serverTo,
157159
};
158160
};
159161
} else {
160162
rangeMapper = (fromByte, toByte) => ({
161-
url: config.url,
163+
url: config.url + suffix,
162164
fromByte,
163165
toByte,
164166
});

0 commit comments

Comments
 (0)