mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
Co-authored-by: Felmon Fekadu <[email protected]> Co-authored-by: Felmon Fekadu <[email protected]>
35 lines
781 B
HTML
35 lines
781 B
HTML
<script>
|
|
window.request = () => {
|
|
const { worker, http } = window.parent.msw
|
|
|
|
function isolatedResolver(resolver) {
|
|
return (info) => {
|
|
if (info.request.referrer !== location.href) {
|
|
return
|
|
}
|
|
return resolver(info)
|
|
}
|
|
}
|
|
|
|
worker.use(
|
|
http.get(
|
|
'./resource',
|
|
isolatedResolver(({ request }) => {
|
|
return new Response('one')
|
|
}),
|
|
),
|
|
)
|
|
|
|
return fetch('./resource')
|
|
.then((response) => response.text())
|
|
.then((data) => {
|
|
const node = document.createElement('p')
|
|
node.innerText = data
|
|
document.body.appendChild(node)
|
|
})
|
|
.catch((error) => console.error(error))
|
|
}
|
|
|
|
document.addEventListener('click', window.request)
|
|
</script>
|