fix: enable InMemoryDriver localStorage persistence in MSW browser mode#992
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…ence in MSW browser mode - Add `persistence` option to KernelOptions to control InMemoryDriver persistence - Pass persistence config to InMemoryDriver constructor - Call driver.connect() after bootstrap to initialize persistence adapter and load any previously persisted data from localStorage - Call driver.flush() to ensure seed data is persisted on first load - Pass persistence: false in server.ts (test mode) to disable file-system persistence Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
|
@copilot 我不知道你开发的是否正确,我是希望实现自动化的保存,包括在浏览器中的保存和服务端的保存 |
实现是正确的,自动保存已经在两种环境下生效: 浏览器模式 ( 服务端 Node.js 模式:同样不传 测试模式 ( 关键改动(commit
|
There was a problem hiding this comment.
Pull request overview
Enables InMemoryDriver persistence in the console’s MSW browser mock environment by explicitly initializing and flushing the persistence adapter, while ensuring test/Node MSW usage disables persistence side effects.
Changes:
- Added a
persistenceoption to thecreateKernelfactory and pass-through to theInMemoryDriverconstructor. - Ensured persistence is initialized and written by calling
driver.connect()anddriver.flush()after bootstrap/seed setup. - Disabled persistence for Node/MSW server tests by passing
persistence: false.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/console/src/mocks/server.ts | Disables driver persistence in Node/MSW server (tests) by passing persistence: false. |
| apps/console/src/mocks/createKernel.ts | Adds configurable persistence, and initializes + flushes persistence after kernel bootstrap. |
| /** | ||
| * InMemoryDriver persistence configuration. | ||
| * | ||
| * - `'auto'` (default) — auto-detect environment (browser → localStorage, Node.js → file) | ||
| * - `'local'` — force localStorage persistence (browser only) | ||
| * - `false` — disable persistence entirely (useful in tests) | ||
| * | ||
| * When omitted, defaults to `'auto'`. | ||
| */ | ||
| persistence?: false | 'auto' | 'local' | 'file'; |
There was a problem hiding this comment.
The JSDoc for persistence lists auto, local, and false, but the type also allows 'file'. Either document the 'file' mode in the comment (including when it’s valid) or remove it from the union to keep the API contract consistent.
InMemoryDriverpersistence requires an explicitdriver.connect()call to initialize the adapter (localStorage in browser). Without it,markDirty()on every CRUD op is a no-op and data is lost on refresh.Changes
createKernel.ts: Addpersistenceoption toKernelOptions, pass it toInMemoryDriverconstructor. Calldriver.connect()+driver.flush()after bootstrap and seed sync.server.ts: Passpersistence: falseto avoid file-system persistence in test/Node.js environment.Lifecycle
connect()finds empty localStorage,flush()persists seed dataconnect()loads persisted data, overwriting fresh seeds — user changes survivepersistence: false— no I/O side effectsOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.