Overview
You can use this document to learn how you can return a static response from serverless scripts. Specifically, this document will use robots.txt as an example.
Before you begin, you must enable serverless scripting for your stack. To learn more, see Enable Serverless Scripting and Create a Script.
View a sample script
In the following example, the route is: robots.txt
Review the following sample script:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest() {
const headers = new Headers();
//Set the proper content-type so the browser understands what we're sending back
headers.set("content-type", ["text/html; charset=UTF-8"]);
//Create a response with the robots.txt data we want
return new Response("User-agent: *\nDisallow: /", {
status: "200",
headers
});
}
Access your script
You can access your script with any of the delivery domains on your account.
Based on the above example, you can access the script with: https://sitehash.stackpathcdn.com/robots.txt