Util

FakeXMLHttpRequest

.create(url: string, response: Document | PromiseLike<Document>): FakeXMLHttpRequest

Create a fake XHR object that doesn’t send a request. You can also define and use another fake XMLHttpRequest.

import Pjax, { FakeXMLHttpRequest } from 'pjax-api';

new Pjax({
  fetch: {
    rewrite: url =>
      FakeXMLHttpRequest.create(
        url,
        fetch(url, { headers: { 'Content-Type': 'application/json' } })
          .then(res => res.json())
          .then(data =>
            new DOMParser().parseFromString(
              `<title>${data.title}</title><body>${data.body}</body>`,
              'text/html'))),
  },
});