Skip to content

Commit 0f85464

Browse files
Copilothotlong
andcommitted
fix: call driver.connect() and flush() to enable localStorage persistence 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>
1 parent ec8a46a commit 0f85464

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

apps/console/src/mocks/createKernel.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ export interface KernelOptions {
2525
skipSystemValidation?: boolean;
2626
/** MSWPlugin options; when provided, MSWPlugin is added to the kernel. */
2727
mswOptions?: MSWPluginOptions;
28+
/**
29+
* InMemoryDriver persistence configuration.
30+
*
31+
* - `'auto'` (default) — auto-detect environment (browser → localStorage, Node.js → file)
32+
* - `'local'` — force localStorage persistence (browser only)
33+
* - `false` — disable persistence entirely (useful in tests)
34+
*
35+
* When omitted, defaults to `'auto'`.
36+
*/
37+
persistence?: false | 'auto' | 'local' | 'file';
2838
}
2939

3040
export interface KernelResult {
@@ -154,9 +164,11 @@ function patchDriverCreate(driver: InMemoryDriver): void {
154164
* so that kernel setup logic is not duplicated.
155165
*/
156166
export async function createKernel(options: KernelOptions): Promise<KernelResult> {
157-
const { appConfig, skipSystemValidation = true, mswOptions } = options;
167+
const { appConfig, skipSystemValidation = true, mswOptions, persistence } = options;
158168

159-
const driver = new InMemoryDriver();
169+
const driver = new InMemoryDriver(
170+
persistence !== undefined ? { persistence } : undefined,
171+
);
160172

161173
const kernel = new ObjectKernel({
162174
skipSystemValidation
@@ -190,5 +202,16 @@ export async function createKernel(options: KernelOptions): Promise<KernelResult
190202
await installBrokerShim(kernel);
191203
}
192204

205+
// Initialise persistence adapter and load any previously persisted data.
206+
// On first load this is a no-op (empty localStorage); on subsequent page
207+
// refreshes the persisted data overwrites the seed data so that user
208+
// changes survive a browser reload.
209+
await driver.connect();
210+
211+
// Ensure the current database state (seed data on first load, or the
212+
// just-restored persisted snapshot) is flushed to the persistence layer
213+
// so that localStorage always contains the latest data.
214+
await driver.flush();
215+
193216
return { kernel, driver, mswPlugin };
194217
}

apps/console/src/mocks/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export async function startMockServer() {
4747

4848
const result = await createKernel({
4949
appConfig,
50+
persistence: false,
5051
mswOptions: {
5152
enableBrowser: false,
5253
baseUrl: '/api/v1',

0 commit comments

Comments
 (0)