@@ -62,35 +62,64 @@ function process_callback(app::DashApp, body::String)
6262
6363end
6464
65- function process_assets (app:: DashApp , path)
65+ function make_response (status, headers, body, compress:: Bool )
66+ message_headers = headers
67+
68+ if compress
69+ push! (message_headers, " Content-Encoding" => " gzip" )
70+ body = transcode (CodecZlib. GzipCompressor, body)
71+ end
72+
73+ response = HTTP. Response (status, body)
74+ for header in message_headers
75+ HTTP. Messages. setheader (response, header)
76+ end
77+ return response
78+ end
79+
80+ function process_assets (app:: DashApp , path, compress:: Bool )
6681 assets_path = " $(app. config. routes_pathname_prefix) " * strip (app. config. assets_url_path, ' /' ) * " /"
67-
68- filename = joinpath (app . config . assets_folder, replace (path, assets_path => " " ))
82+ filename = joinpath (app . config . assets_folder, replace (path, assets_path => " " ))
83+
6984 try
70- return HTTP. Response (200 , [], body = read (filename))
85+ file_contents = read (filename)
86+ mimetype = HTTP. sniff (file_contents)
87+ use_gzip = compress && occursin (r" text|javascript" , mimetype)
88+ headers = [" Content-Type" => mimetype]
89+ return make_response (200 , headers, file_contents, use_gzip)
7190 catch
7291 return HTTP. Response (404 )
7392 end
7493end
7594
76-
7795function make_handler (app:: DashApp ; debug:: Bool = false )
7896 index_string:: String = index_page (app, debug = debug)
7997
8098 return function (req:: HTTP.Request )
99+ body:: Union{Nothing, String} = nothing
81100 uri = HTTP. URI (req. target)
101+
102+ # verify that the client accepts compression
103+ accepts_gz = occursin (" gzip" , HTTP. header (req, " Accept-Encoding" ))
104+ # verify that the server was not launched with compress=false
105+ with_gzip = accepts_gz && app. config. compress
106+
107+ headers = []
108+
82109 ComponentPackages. @register_js_sources (uri. path, app. config. routes_pathname_prefix)
83110 if uri. path == " $(app. config. routes_pathname_prefix) "
84- return HTTP . Response ( 200 , index_string)
111+ body = index_page (app, debug = debug)
85112 end
86113 if uri. path == " $(app. config. routes_pathname_prefix) _dash-layout"
87- return HTTP. Response (200 , [" Content-Type" => " application/json" ], body = JSON2. write (app. layout))
114+ body = JSON2. write (app. layout)
115+ push! (headers, " Content-Type" => " application/json" )
88116 end
89117 if uri. path == " $(app. config. routes_pathname_prefix) _dash-dependencies"
90- return HTTP. Response (200 , [" Content-Type" => " application/json" ], body = dependencies_json (app))
118+ body = dependencies_json (app)
119+ push! (headers, " Content-Type" => " application/json" )
91120 end
92121 if startswith (uri. path, " $(app. config. routes_pathname_prefix) assets/" )
93- return process_assets (app, uri. path)
122+ return process_assets (app, uri. path, with_gzip )
94123 end
95124 if uri. path == " $(app. config. routes_pathname_prefix) _dash-update-component" && req. method == " POST"
96125 try
@@ -107,7 +136,9 @@ function make_handler(app::DashApp; debug::Bool = false)
107136 end
108137 end
109138 end
139+ if ! isnothing (body)
140+ return make_response (200 , headers, body, with_gzip)
141+ end
110142 return HTTP. Response (404 )
111- end
112-
113- end
143+ end
144+ end
0 commit comments