Created
June 22, 2021 10:28
-
-
Save justinvdm/e59e06c5f4a02c14214caac25ea40387 to your computer and use it in GitHub Desktop.
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
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); |
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
{ | |
"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 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
# 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