Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active December 8, 2021 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/907ed9e9358944b309905ab831140032 to your computer and use it in GitHub Desktop.
Save ryanflorence/907ed9e9358944b309905ab831140032 to your computer and use it in GitHub Desktop.
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