Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active Dec 8, 2021
Embed
What would you like to do?
import { createServer } from "http";
createServer(async (req, res) => {
if (req.url === "/app.js") {
res.setHeader("Content-Type", "application/javascript; charset=UTF-8");
res.setHeader("Cache-Control", "max-age=10");
res.write(`console.log("lol")`);
res.end();
return;
}
if (req.url === "/app.css") {
res.setHeader("Content-Type", "text/css; charset=UTF-8");
res.setHeader("Cache-Control", "max-age=10");
res.write(`body { font-family: system-ui }`);
res.end();
return;
}
res.setHeader(
"Link",
"</app.js>; rel=modulepreload, </app.css>; rel=preload; as=style"
);
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.write(`
<!doctype html>
<html>
<head>
<title>Okay</title>
`);
await new Promise((res) => setTimeout(res, 5000));
res.write(`
<link rel="stylesheet" href="/app.css" />
</head>
<body>
<p>(are the assets loading?)</p>
<h1>LOL I'm done</h1>
<script type="module" src="/app.js"></script>
</body>
</html>
`);
res.end();
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment