A tiny web framework for Gleam targeting all JavaScript runtimes
0

Configure Feed

Select the types of activity you want to include in your feed.

:sparkles: add last_modified

+41 -4
+1
CHANGELOG.md
··· 11 11 - Added `content_security_policy_protection` middleware for nonce-based strict CSP headers. 12 12 - Added `as_file_download` for sending responses with `Content-Disposition: attachment`. 13 13 - Added automatic `ETag` and `Last-Modified` headers for `send_file` responses and `cache` middleware for conditional requests. 14 + - Added `last_modified` to set the header of the same name. 14 15 15 16 ### Changed 16 17
+22 -4
src/smol.gleam
··· 104 104 //// [server_sent_events](#server_sent_events "Send a SSE stream response"), 105 105 //// [websocket](#websocket "Open a websocket connection"), 106 106 //// [with_status](#with_status "Set the status of a response"), 107 + //// [last_modified](#last_modified "Set the Last-Modified header of a response"), 107 108 //// [with_extra_headers](#with_extra_headers "Add some headers to a response"), 108 109 //// [as_file_download](#as_file_download "Send a response as a file download"), 109 110 //// [send](#send "Helper for chunked/SSE/Websocket responses"), ··· 1215 1216 #("content-length", int.to_string(content_length)), 1216 1217 #("content-type", content_type), 1217 1218 #("etag", generate_etag(file.size, file.mtime)), 1218 - #( 1219 - "last-modified", 1220 - format_http_date(file.mtime), 1221 - ), 1219 + #("last-modified", format_http_date(file.mtime)), 1222 1220 ]), 1223 1221 ) 1224 1222 } ··· 1549 1547 ) -> Promise(Response) { 1550 1548 use response <- promise.await(response) 1551 1549 promise.resolve(Response(..response, status:)) 1550 + } 1551 + 1552 + /// Convenience function to set the `Last-Modified` header on a response from a 1553 + /// `Timestamp`. 1554 + /// 1555 + /// Pairs with the [`cache`](#cache) middleware, which uses the header to respond 1556 + /// with `304 Not Modified` for conditional requests. 1557 + /// 1558 + /// ## Example 1559 + /// 1560 + /// ```gleam 1561 + /// smol.send_html(render_post(post)) 1562 + /// |> smol.last_modified(at: post.updated_at) 1563 + /// ``` 1564 + /// 1565 + pub fn last_modified( 1566 + response: Promise(Response), 1567 + at timestamp: Timestamp, 1568 + ) -> Promise(Response) { 1569 + with_extra_headers(response, [#("last-modified", format_http_date(timestamp))]) 1552 1570 } 1553 1571 1554 1572 /// Convenience function to add headers to a response.
+18
test/smol_test.gleam
··· 1018 1018 promise.resolve(Nil) 1019 1019 } 1020 1020 1021 + pub fn last_modified_sets_header_test() -> Promise(Nil) { 1022 + let updated_at = 1023 + timestamp.from_calendar( 1024 + date: calendar.Date(1994, calendar.November, 6), 1025 + time: calendar.TimeOfDay(8, 49, 37, 0), 1026 + offset: calendar.utc_offset, 1027 + ) 1028 + 1029 + use response <- promise.await( 1030 + smol.send_string("ok") 1031 + |> smol.last_modified(at: updated_at), 1032 + ) 1033 + 1034 + assert_header(response, "last-modified", "Sun, 06 Nov 1994 08:49:37 GMT") 1035 + 1036 + promise.resolve(Nil) 1037 + } 1038 + 1021 1039 pub fn send_html_sets_content_type_and_body_test() -> Promise(Nil) { 1022 1040 use response <- promise.await(smol.send_html("<h1>Hello</h1>")) 1023 1041