-
-
Save miebach/6375183 to your computer and use it in GitHub Desktop.
JS Early Head Flush - see https://docs.google.com/presentation/d/1DNljLkRpe9LIDfcqcpHzdLvEOyuVH4d1y9dtAJBr1I8/preview#slide=id.gbba09f01_067
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>Hello</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
Hello World | |
</body> | |
</html> | |
This file contains hidden or 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
require 'goliath' | |
class Delay < Goliath::API | |
def response(env) | |
case env['REQUEST_PATH'] | |
when /css$/ then | |
# simulate slow (1s delay) stylesheet | |
EM::Synchrony.sleep(1) | |
return [200, {'Content-Type' => 'text/css'}, 'body { color: red }'] | |
else | |
data = File.open('file.html') | |
# flush the HTML head (140 bytes) immediately after the headers | |
# this will allow the preloader to request the stylesheet before | |
# the rest of the page completes | |
EM.add_timer(0.01) do | |
env.chunked_stream_send(data.read(140)) | |
end | |
# flush remainder of document after 1.5s | |
EM.add_timer(1.5) do | |
env.chunked_stream_send(data.read.to_s) | |
env.chunked_stream_close | |
end | |
# flush 200 response immediately | |
chunked_streaming_response(200, {'Content-Type' => 'text/html'}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment