Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CypherpunkSamurai/50c2a63b5e42c6ab4f862784639b5543 to your computer and use it in GitHub Desktop.
Save CypherpunkSamurai/50c2a63b5e42c6ab4f862784639b5543 to your computer and use it in GitHub Desktop.
Cloudflare Workers Return SSR HTML
export default {
async fetch(request) {
// get a name
const name = await fetch("https://randommer.io/Name", {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"x-requested-with": "XMLHttpRequest"
},
"body": "type=firstname&number=1&X-Requested-With=XMLHttpRequest",
"method": "POST",
}).then(async(r) => await r.json());
const html = `<!DOCTYPE html>
<html>
<head><title>Can You Read My Name?</title></head>
<body>
<h1>Can You Read My Name??</h1>
My Name is <span id="can-you-read-me">${name[0]}</span>
</body>
</html>`;
return new Response(html, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment