See the comment in the code below. Is that possible? // Inject 'content' where content comes from a dynamic call to an API
.match("/example-react-progressive-web-apps-ecommerce/", ({ proxy }) => {
proxy("origin", {
path: "/company",
transformResponse: (res, req) => {
const $ = cheerio.load(res.body)
// Remove everything but the header and footer
$(".section").remove()
// Insert our new div that will house our new content
$(".navbar-wrapper").after('<div id="main-content-container"></div>')
// Change page title
$("title").text("Example eCommerce PWAs")
// Rewrite links in original page
$("a").each(function () {
let old_href = $(this).attr("href")
if (old_href[0] === "/") {
let new_href = "https://www.moovweb.com" + old_href
$(this).attr("href", new_href)
}
})
// Inject 'content' where content comes from a dynamic call to an API
$('#main-content-container').append(content)
res.body = $.html()
res.setHeader("content-length", res.body.length)
},
})
})
@ajay Your use case is one of the intended use cases of transformResponse. Working from your example and making it as optimal as possible, you would first issue the dynamic call and while waiting for that I/O to come back you would process the HTML you already have (notice the added async in the function declaration):
.match("/example-react-progressive-web-apps-ecommerce/", ({ proxy }) => {
proxy("origin", {
path: "/company",
transformResponse: async (res, req) => {
// Issue the dynamic call first.
const dynamicResponsePromise = fetch('https://maybe-another-upstream.com/dynamic-call')
const $ = cheerio.load(res.body)
// Remove everything but the header and footer
$(".section").remove()
// Insert our new div that will house our new content
$(".navbar-wrapper").after('<div id="main-content-container"></div>')
// Change page title
$("title").text("Example eCommerce PWAs")
// Rewrite links in original page
$("a").each(function () {
let old_href = $(this).attr("href")
if (old_href[0] === "/") {
let new_href = "https://www.moovweb.com" + old_href
$(this).attr("href", new_href)
}
})
// We have processed the HTML so now await the dynamic response.
const dynamicResponse = await dynamicResponsePromise
// TODO: add error checking and data validation of the dynamic response
// Now await the content of the dynamic response.
const content = await dynamicResponsePromise.text()
// Inject 'content' where content comes from a dynamic call to an API
$('#main-content-container').append(content)
res.body = $.html()
res.setHeader("content-length", res.body.length)
},
})
})
We used to call it MUR as in “Multiple Upstream Requests”.