Skip to content

Commit

Permalink
fix: produce the most unholy code
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 23, 2024
1 parent 27c073d commit 2664587
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 77 deletions.
18 changes: 18 additions & 0 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
res.end(buffer)
})
}
server.middlewares.use((req, res, next) => {
// 9000 mega head move
// Vite always caches optimized dependencies, but users might mock
// them in _some_ tests, while keeping original modules in others
// there is no way to configure that in Vite, so we patch it here
// to always ignore the cache-control set by Vite in the next middleware
if (req.url && req.url.includes('/deps/') && !req.url.includes('chunk-')) {
res.setHeader('Cache-Control', 'no-cache')
const setHeader = res.setHeader.bind(res)
res.setHeader = function (name, value) {
if (name === 'Cache-Control') {
return res
}
return setHeader(name, value)
}
}
next()
})
},
},
{
Expand Down
21 changes: 0 additions & 21 deletions packages/mocker/src/browser/interceptor-msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ export class ModuleMockerMSWInterceptor implements ModuleMockerInterceptor {
http.get(/.+/, async ({ request }) => {
const path = cleanQuery(request.url.slice(location.origin.length))
if (!this.mocks.has(path)) {
// do not cache deps like Vite does for performance
// because we want to be able to update mocks without restarting the server
// TODO: check if it's still neded - we invalidate modules after each test
if (path.includes('/deps/')) {
return fetch(bypass(request))
}

return passthrough()
}

Expand Down Expand Up @@ -151,20 +144,6 @@ function passthrough() {
})
}

function bypass(request: Request) {
const clonedRequest = request.clone()
clonedRequest.headers.set('x-msw-intention', 'bypass')
const cacheControl = clonedRequest.headers.get('cache-control')
if (cacheControl) {
clonedRequest.headers.set(
'cache-control',
// allow reinvalidation of the cache so mocks can be updated
cacheControl.replace(', immutable', ''),
)
}
return clonedRequest
}

const replacePercentageRE = /%/g
function injectQuery(url: string, queryToInject: string): string {
// encode percents for consistent behavior with pathToFileURL
Expand Down
59 changes: 3 additions & 56 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2664587

Please sign in to comment.