Skip to content

Commit

Permalink
fix(runtime-utils): devServer served html content didn't change after…
Browse files Browse the repository at this point in the history
… repacking. (#5187)
  • Loading branch information
xc2 committed Jan 5, 2024
1 parent 5bc653b commit 78e2722
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/honest-cherries-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime-utils': patch
---

fix(runtime-utils): FileReader#reset didn't clear storage as expected
fix(runtime-utils): FileReader#reset 没有按预期清除缓存的问题。
2 changes: 1 addition & 1 deletion packages/toolkit/runtime-utils/src/node/storer/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Storage<V = unknown> {
const keys = await this.keys?.();
await Promise.all(
keys?.map(async key => {
return this.delete(key);
return this.container.delete(key);
}) || [],
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tmp
30 changes: 30 additions & 0 deletions packages/toolkit/runtime-utils/tests/node/fileReader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import * as FSModule from 'node:fs/promises';
import { fileReader } from '../../src/node/fileReader';

test('should fileReader work correctly', async () => {
Expand All @@ -16,3 +17,32 @@ test('should fileReader work correctly', async () => {
expect(res1).not.toBeNull();
expect(res).toMatch('modern');
});

describe('FileReader#cache', () => {
const tmpFile = path.resolve(__dirname, '../fixtures/fileReader/x.tmp');
const writeTmpFile = (content: string) =>
FSModule.writeFile(tmpFile, content);

const writeReadTmpFile = (content: string) =>
writeTmpFile(content).then(() => fileReader.readFile(tmpFile));

beforeEach(async () => {
await fileReader.reset();
// touch
await writeTmpFile('');
});
afterEach(async () => {
await FSModule.rm(tmpFile);
});

it('should read cached content first', async () => {
await expect(writeReadTmpFile('foo')).resolves.toBe('foo');
await expect(writeReadTmpFile('bar')).resolves.toBe('foo');
});

it('should read file again after FileReader reset', async () => {
await expect(writeReadTmpFile('foo2')).resolves.toBe('foo2');
await fileReader.reset();
await expect(writeReadTmpFile('bar2')).resolves.toBe('bar2');
});
});

0 comments on commit 78e2722

Please sign in to comment.