Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justinvdm/e59e06c5f4a02c14214caac25ea40387 to your computer and use it in GitHub Desktop.
Save justinvdm/e59e06c5f4a02c14214caac25ea40387 to your computer and use it in GitHub Desktop.
const http = require('http');
const markup = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>rarily rar rar</title>
<script defer src="/head.js"></script>
</head>
<body>
<script src="/body.js"></script>
</body>
</html>
`;
const HEAD_SCRIPT_DELAY = 10000;
const BODY_SCRIPT_DELAY = 5000;
const headScript = `
console.log('head eval-ed');
`;
const bodyScript = `
console.log('body eval-ed');
`
const handleIndex = (req, res) => {
res.write(markup);
res.end();
};
const handleHeadScript = (req, res) => {
console.log('head requested');
setTimeout(() => {
res.write(headScript);
res.end();
}, HEAD_SCRIPT_DELAY);
};
const handleBodyScript = (req, res) => {
console.log('body requested');
setTimeout(() => {
res.write(bodyScript);
res.end();
}, BODY_SCRIPT_DELAY);
};
const handleDefault = (req, res) => {
res.statusCode = 404;
res.end();
}
const handle = (req, res) => ({
'/': handleIndex,
'/head.js': handleHeadScript,
'/body.js': handleBodyScript,
}[req.url] || handleDefault)(req, res);
http.createServer(handle).listen(8080);
{
"name": "script-eval-order-head-defer-vs-body",
"version": "1.0.0",
"description": "",
"main": "_script-eval-order-head-defer-vs-body.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment