@@ -16,6 +16,7 @@ import {
1616 RequestHandler ,
1717 WebSocketHandler ,
1818 handleRequest ,
19+ isCommonAssetRequest ,
1920} from 'msw'
2021import {
2122 type WebSocketClientEventMap ,
@@ -30,6 +31,14 @@ import {
3031export interface CreateNetworkFixtureArgs {
3132 initialHandlers ?: Array < RequestHandler | WebSocketHandler >
3233 onUnhandledRequest ?: UnhandledRequestStrategy
34+ /**
35+ * Skip common asset requests (e.g. `*.html`, `*.css`, `*.js`, etc).
36+ * This improves performance for certian projects.
37+ * @default true
38+ *
39+ * @see https://mswjs.io/docs/api/is-common-asset-request
40+ */
41+ skipAssetRequests ?: boolean
3342}
3443
3544/**
@@ -61,6 +70,7 @@ export function createNetworkFixture(
6170 async ( { context } , use ) => {
6271 const worker = new NetworkFixture ( {
6372 context,
73+ skipAssetRequests : args ?. skipAssetRequests ?? true ,
6474 initialHandlers : args ?. initialHandlers || [ ] ,
6575 onUnhandledRequest : args ?. onUnhandledRequest ,
6676 } )
@@ -85,6 +95,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
8595 constructor (
8696 protected args : {
8797 context : BrowserContext
98+ skipAssetRequests : boolean
8899 initialHandlers : Array < RequestHandler | WebSocketHandler >
89100 onUnhandledRequest ?: UnhandledRequestStrategy
90101 } ,
@@ -103,6 +114,16 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
103114 body : request . postDataBuffer ( ) as ArrayBuffer | null ,
104115 } )
105116
117+ /**
118+ * @note Skip common asset requests (default).
119+ * Playwright seems to experience performance degradation when routing all
120+ * requests through the matching logic below.
121+ * @see https://github.com/mswjs/playwright/issues/13
122+ */
123+ if ( this . args . skipAssetRequests && isCommonAssetRequest ( fetchRequest ) ) {
124+ return route . continue ( )
125+ }
126+
106127 const handlers = this . handlersController
107128 . currentHandlers ( )
108129 . filter ( ( handler ) => {
0 commit comments