-
-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0729ae
commit ee0548e
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Determines if the given request is a static asset request. | ||
* Useful when deciding which unhandled requests to ignore. | ||
* | ||
* @example | ||
* import { isAssetRequest } from 'msw' | ||
* | ||
* await worker.start({ | ||
* onUnhandledRequest(request, print) { | ||
* if (!isAssetRequest(request)) { | ||
* print.warning() | ||
* } | ||
* } | ||
* }) | ||
*/ | ||
export function isAssetRequest(request: Request): boolean { | ||
const url = new URL(request.url) | ||
|
||
// Ignore static assets hosts. | ||
if (/(fonts\.googleapis\.com)/.test(url.hostname)) { | ||
return true | ||
} | ||
|
||
// Ignore node modules served over HTTP. | ||
if (/node_modules/.test(url.pathname)) { | ||
return true | ||
} | ||
|
||
// Ignore common static assets. | ||
return /\.(css|m?js|m?ts|html|ttf|otf|woff|woff2|gif|jpe?g|png|avif|webp|svg)$/i.test( | ||
url.pathname, | ||
) | ||
} |