Skip to content

Commit

Permalink
fix: export isAssetRequest utility
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 24, 2025
1 parent d0729ae commit ee0548e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export * from './HttpResponse'
export * from './delay'
export { bypass } from './bypass'
export { passthrough } from './passthrough'
export { isAssetRequest } from './isAssetRequest'

// Validate environmental globals before executing any code.
// This ensures that the library gives user-friendly errors
Expand Down
33 changes: 33 additions & 0 deletions src/core/isAssetRequest.ts
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,
)
}

0 comments on commit ee0548e

Please sign in to comment.