Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: 修复 Demo 环境重置失效

tianma-if4737da0b5612
diff --git a/apps/api/src/database-write-optimizations.test.mjs b/apps/api/src/database-write-optimizations.test.mjs
index 6c200ae5..caa032c6 100644
--- a/apps/api/src/database-write-optimizations.test.mjs
+++ b/apps/api/src/database-write-optimizations.test.mjs
@@ -176,6 +176,28 @@ describe("database write optimizations", () => {
sqlite.close();
});
+ test("does not report a demo reset as successful when another reset owns the lease", async () => {
+ const { database, sqlite } = createDatabase();
+ expect(await acquireMaintenanceLease(database, "demo-reset", 60_000)).toStartWith("lease_");
+
+ const response = await fetchEdgeEverApp(
+ new Request("https://notes.example.com/api/v1/demo/reset", { method: "POST" }),
+ {
+ storage: { db: database, resources: {} },
+ EDGE_EVER_ALLOW_UNAUTHENTICATED: "true",
+ EDGE_EVER_DEMO_MODE: "true",
+ },
+ executionContext,
+ );
+
+ expect(response.status).toBe(409);
+ expect(response.headers.get("Retry-After")).toBe("60");
+ expect(await response.json()).toMatchObject({
+ error: { code: "demo_reset_in_progress" },
+ });
+ sqlite.close();
+ });
+
test("touches API tokens at most once per hour", async () => {
const { database, sqlite } = createDatabase();
const token = "edgeever_api_test_token";
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index f497ed03..1bb0c115 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -597,7 +597,20 @@ app.post("/api/v1/demo/reset", async (c) => {
);
}
- await resetDemoData(c.env, Date.now());
+ const reset = await resetDemoData(c.env, Date.now());
+ if (!reset) {
+ c.header("Retry-After", String(Math.ceil(DEMO_RESET_COOLDOWN_MS / 1000)));
+ return c.json(
+ {
+ error: {
+ code: "demo_reset_in_progress",
+ message: "Demo reset is already in progress or cooling down",
+ },
+ },
+ 409
+ );
+ }
+
return c.json({
success: true,
message: "Demo seed data successfully restored",
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index e707aada..a8372544 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -75,6 +75,7 @@ import { useBrowserBackLayer } from "@/lib/app-hooks";
import { updateMemoSummaryInLists, type MemoListQueryData } from "@/lib/memo-list-cache";
import { shouldAcceptRemoteMemoDetail } from "@/lib/memo-detail-freshness";
import {
+ clearLocalScope,
createLocalDataScope,
putLocalMemo,
putLocalNotebook,
@@ -738,13 +739,26 @@ export const WorkspaceApp = ({
}, [pluginHost]);
const resetDemoMutation = useMutation({
- mutationFn: () => api.resetDemo(),
- onSuccess: () => {
+ mutationFn: async () => {
+ await api.resetDemo();
+ await clearLocalScope(localDataScope);
+ await repository.sync();
+ },
+ onMutate: () => {
+ const previousSelectedMemoId = selectedMemoIdRef.current;
+ setSelectedMemoId(null);
+ setCreatedMemoEditId(null);
+ pendingCreatedMemoIdRef.current = null;
+ pendingQuickSwitcherMemoIdRef.current = null;
+ return { previousSelectedMemoId };
+ },
+ onSuccess: async () => {
setDemoResetConfirmationOpen(false);
- void Promise.all([
+ queryClient.removeQueries({ queryKey: ["memo"] });
+ await Promise.all([
queryClient.invalidateQueries({ queryKey: ["memos"] }),
- queryClient.invalidateQueries({ queryKey: ["memo"] }),
queryClient.invalidateQueries({ queryKey: ["notebooks"] }),
+ queryClient.invalidateQueries({ queryKey: ["templates"] }),
queryClient.invalidateQueries({ queryKey: ["resources"] }),
queryClient.invalidateQueries({ queryKey: ["tags"] }),
]);
@@ -753,8 +767,11 @@ export const WorkspaceApp = ({
description: t("demo.resetSuccess"),
});
},
- onError: () => {
+ onError: (_error, _variables, context) => {
setDemoResetConfirmationOpen(false);
+ if (context?.previousSelectedMemoId) {
+ setSelectedMemoId(context.previousSelectedMemoId);
+ }
setAppNoticeDialog({
title: t("demo.resetFailed"),
description: t("demo.resetFailed"),
diff --git a/apps/web/src/lib/local-mirror.test.mjs b/apps/web/src/lib/local-mirror.test.mjs
index 8550084e..9e40102b 100644
--- a/apps/web/src/lib/local-mirror.test.mjs
+++ b/apps/web/src/lib/local-mirror.test.mjs
@@ -7,6 +7,7 @@ globalThis.IDBKeyRange = IDBKeyRange;
const { localDb } = await import("./local-db.ts");
const {
createLocalDataScope,
+ clearLocalScope,
createLocalMemo,
getLocalMemo,
listLocalTemplates,
@@ -60,6 +61,56 @@ afterEach(async () => {
});
describe("local mirror", () => {
+ test("clears cached data, drafts, and pending changes for a reset scope", async () => {
+ const scope = createLocalDataScope("https://demo.edgeever.org", "user-1");
+ const otherScope = createLocalDataScope("https://notes.example.org", "user-2");
+ const scopedMemo = await createLocalMemo(scope, { notebookId: "demo-notebook", title: "Changed demo note" });
+ const otherMemo = await createLocalMemo(otherScope, { notebookId: "private-notebook", title: "Keep me" });
+ await localDb.drafts.bulkPut([
+ { memoId: scopedMemo.id, title: "Demo draft", contentJson: { type: "doc", content: [] }, tagsText: "", updatedAt: "2026-01-01T00:00:00.000Z" },
+ { memoId: otherMemo.id, title: "Private draft", contentJson: { type: "doc", content: [] }, tagsText: "", updatedAt: "2026-01-01T00:00:00.000Z" },
+ ]);
+ await localDb.syncQueue.bulkPut([
+ {
+ id: `memo.update:${scopedMemo.id}`,
+ kind: "memo.update",
+ scope,
+ memoId: scopedMemo.id,
+ status: "pending",
+ payload: {},
+ attemptCount: 0,
+ lastError: null,
+ nextAttemptAt: null,
+ claimId: null,
+ createdAt: "2026-01-01T00:00:00.000Z",
+ updatedAt: "2026-01-01T00:00:00.000Z",
+ },
+ {
+ id: `memo.update:${otherMemo.id}`,
+ kind: "memo.update",
+ scope: otherScope,
+ memoId: otherMemo.id,
+ status: "pending",
+ payload: {},
+ attemptCount: 0,
+ lastError: null,
+ nextAttemptAt: null,
+ claimId: null,
+ createdAt: "2026-01-01T00:00:00.000Z",
+ updatedAt: "2026-01-01T00:00:00.000Z",
+ },
+ ]);
+
+ await clearLocalScope(scope);
+
+ expect(await getLocalMemo(scope, scopedMemo.id)).toBeNull();
+ expect(await localDb.drafts.get(scopedMemo.id)).toBeUndefined();
+ expect(await localDb.syncQueue.get(`memo.update:${scopedMemo.id}`)).toBeUndefined();
+ expect(await getLocalMemo(otherScope, otherMemo.id)).not.toBeNull();
+ expect(await localDb.drafts.get(otherMemo.id)).toBeDefined();
+ expect(await localDb.syncQueue.get(`memo.update:${otherMemo.id}`)).toBeDefined();
+ });
+
test("rebuilds the browser mirror when the server change cursor rewinds", () => {
expect(hasLocalSyncCursorRewound(42, 7)).toBe(true);
expect(hasLocalSyncCursorRewound(42, 42)).toBe(false);
diff --git a/apps/web/src/lib/local-mirror.ts b/apps/web/src/lib/local-mirror.ts
index 472575e1..b9c84596 100644
--- a/apps/web/src/lib/local-mirror.ts
+++ b/apps/web/src/lib/local-mirror.ts
@@ -717,10 +717,11 @@ export const restoreLocalMemo = async (scope: string, memoId: string) => {
export const clearLocalScope = async (scope: string) => {
const scopedResources = await localDb.resources.where("scope").equals(scope).toArray();
- await localDb.transaction("rw", [localDb.notebooks, localDb.memos, localDb.templates, localDb.revisions, localDb.resources, localDb.syncQueue, localDb.syncMeta, localDb.idMappings], async () => {
+ await localDb.transaction("rw", [localDb.drafts, localDb.notebooks, localDb.memos, localDb.templates, localDb.revisions, localDb.resources, localDb.syncQueue, localDb.syncMeta, localDb.idMappings], async () => {
const scopedMemos = await localDb.memos.where("scope").equals(scope).toArray();
const scopedMemoIds = new Set(scopedMemos.map((memo) => memo.id));
const queuedItems = await localDb.syncQueue.toArray();
+ await localDb.drafts.bulkDelete([...scopedMemoIds]);
await localDb.notebooks.where("scope").equals(scope).delete();
await localDb.memos.where("scope").equals(scope).delete();
await localDb.templates.where("scope").equals(scope).delete();