Skip to content

Commit 6320ed6

Browse files
committed
fix noCache and add debug
1 parent 59489e2 commit 6320ed6

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

package-lock.json

+24-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"knip": "knip -c knip.config.ts",
2323
"prettier": "prettier --check --ignore-unknown .",
2424
"prettier:w": "prettier --write --ignore-unknown .",
25-
"test": "rm -rf test/.network-cache && npx playwright test",
26-
"test:d": "npm test -- --debug",
25+
"test": "npx playwright test",
26+
"test:d": "DEBUG=playwright-network-cache npm test",
2727
"example": "npx playwright test -c example",
2828
"example:serve": "npx ts-node ./example/src/server",
2929
"toc": "markdown-toc -i README.md --maxdepth 3",
@@ -33,6 +33,7 @@
3333
"devDependencies": {
3434
"@eslint/js": "9.5.0",
3535
"@playwright/test": "1.44.1",
36+
"@types/debug": "4.1.12",
3637
"@types/mime-types": "2.1.4",
3738
"@types/node": "^18.15.0",
3839
"eslint": "8.57.0",
@@ -62,6 +63,7 @@
6263
"funding": "https://github.com/sponsors/vitalets",
6364
"license": "MIT",
6465
"dependencies": {
66+
"debug": "4.3.7",
6567
"mime-types": "2.1.35"
6668
}
6769
}

playwright.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const baseURL = 'http://localhost:3000';
55
export default defineConfig({
66
testDir: 'test',
77
fullyParallel: true,
8+
globalSetup: './test/global-setup.ts',
89
use: {
910
baseURL,
1011
viewport: { width: 800, height: 600 },

src/CacheRouteHandler/index.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { BodyFile } from './BodyFile';
99
import { SyntheticApiResponse } from './SyntheticApiResponse';
1010
import { BuildCacheDirArg } from '../CacheRoute/options';
1111
import { HttpMethod, ResolvedCacheRouteOptions } from '../CacheRoute';
12+
import { debug } from '../debug';
1213

1314
export class CacheRouteHandler {
1415
private req: Request;
@@ -23,6 +24,7 @@ export class CacheRouteHandler {
2324
this.req = route.request();
2425
}
2526

27+
// eslint-disable-next-line complexity, max-statements
2628
async handle() {
2729
if (!this.isRequestMatched()) {
2830
await this.route.fallback();
@@ -40,10 +42,14 @@ export class CacheRouteHandler {
4042
this.buildCacheDir();
4143
this.storeLastModified();
4244

43-
const response =
44-
forceUpdate || this.isExpired()
45-
? await this.fetchFromServer() // prettier-ignore
46-
: await this.fetchFromCache();
45+
const useRealRequest = forceUpdate || this.isExpired();
46+
const response = useRealRequest
47+
? await this.fetchFromServer() // prettier-ignore
48+
: await this.fetchFromCache();
49+
50+
if (useRealRequest && this.matchHttpStatus(response)) {
51+
await this.saveResponse(response);
52+
}
4753

4854
await this.fulfillRoute(response);
4955
}
@@ -62,17 +68,13 @@ export class CacheRouteHandler {
6268
}
6369

6470
private async fetchFromServer() {
71+
debug(`Fetching from server: ${this.req.method()} ${this.req.url()}`);
6572
const overrides = toFunction(this.options.overrides)(this.req);
66-
const response = await this.route.fetch(overrides);
67-
68-
if (this.matchHttpStatus(response)) {
69-
await this.trySaveResponse(response);
70-
}
71-
72-
return response;
73+
return this.route.fetch(overrides);
7374
}
7475

7576
private async fetchFromCache() {
77+
debug(`Fetching from cache: ${this.req.method()} ${this.req.url()}`);
7678
const responseInfo = await new HeadersFile(this.cacheDir).read();
7779
const bodyFile = new BodyFile(this.cacheDir, responseInfo);
7880
const body = await bodyFile.read();
@@ -90,7 +92,7 @@ export class CacheRouteHandler {
9092
return lastModified > this.lastModified;
9193
}
9294

93-
private async trySaveResponse(response: APIResponse) {
95+
private async saveResponse(response: APIResponse) {
9496
const responseInfo: ResponseInfo = {
9597
url: response.url(),
9698
status: response.status(),
@@ -100,8 +102,11 @@ export class CacheRouteHandler {
100102
const body = await response.body();
101103
// file can be updated by another worker
102104
if (!this.isUpdated()) {
105+
debug(`Writing cache: ${this.cacheDir}`);
103106
new HeadersFile(this.cacheDir).save(responseInfo);
104107
new BodyFile(this.cacheDir, responseInfo).save(body);
108+
} else {
109+
debug(`Skip writing cache, updated by another worker: ${this.cacheDir}`);
105110
}
106111
}
107112

src/debug.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import createDebug from 'debug';
2+
3+
export const debug = createDebug('playwright-network-cache');

test/global-setup.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import fs from 'fs';
2+
3+
export default function clearCacheDir() {
4+
fs.rmSync('test/.network-cache', { recursive: true, force: true });
5+
}

0 commit comments

Comments
 (0)