This is useful to mock an API response if the api doesn't exist yet. This goes in a service worker that is registered to the site.
self.addEventListener("fetch", function (event) {
if (event.request.url.indexOf("/route/to/mock") !== -1) {
console.log("Caught response for ", event.request.url);
event.respondWith(
fetch(event.request).then(function () {
let data = JSON.stringify({
data: [
{
link: "/forum/me-cuesta-mucho",
title: "Me cuesta mucho",
reply_count: 0,
last_update: "February 5, 2019 03:13pm",
category: "General Discussion",
category_link: "/discussion-forum/general-discussion",
author_avatar:
"https://s3.amazonaws.com/finehomebuilding.s3.tauntoncloud.com/app/uploads/2016/04/09173139/Minnie_Moran__sqm.jpg",
},
],
total: 100,
current_page: 1,
total_pages: 10,
});
var init = {
status: 200,
statusText: "OK",
headers: {
"Content-Type": "application/json",
},
};
let response = new Response(data, init);
return response;
})
);
}
});