Created
August 24, 2024 11:23
-
-
Save CypherpunkSamurai/50c2a63b5e42c6ab4f862784639b5543 to your computer and use it in GitHub Desktop.
Cloudflare Workers Return SSR HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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