2022-10-30 10:56:25 +00:00
|
|
|
#lang typed/racket/base
|
|
|
|
(require racket/path
|
2022-11-11 10:08:06 +00:00
|
|
|
racket/runtime-path
|
|
|
|
racket/string)
|
2022-10-30 10:56:25 +00:00
|
|
|
|
|
|
|
(provide
|
2022-11-11 10:08:06 +00:00
|
|
|
get-static-url
|
|
|
|
link-header)
|
2022-10-30 10:56:25 +00:00
|
|
|
|
|
|
|
(define-runtime-path path-static "../static")
|
|
|
|
|
|
|
|
(define static-data
|
2022-11-11 10:08:06 +00:00
|
|
|
(for/hash : (Immutable-HashTable Path Nonnegative-Integer)([f (directory-list path-static)])
|
2022-10-30 10:56:25 +00:00
|
|
|
(define built (simple-form-path (build-path path-static f)))
|
|
|
|
(values built (file-or-directory-modify-seconds built))))
|
|
|
|
|
2022-11-05 10:40:05 +00:00
|
|
|
(: get-static-url (Path-String -> String))
|
2022-10-30 10:56:25 +00:00
|
|
|
(define (get-static-url path-or-filename)
|
2022-11-05 10:40:05 +00:00
|
|
|
(define the-path (simple-form-path (if (path? path-or-filename)
|
|
|
|
path-or-filename
|
|
|
|
(build-path path-static path-or-filename))))
|
2022-10-30 10:56:25 +00:00
|
|
|
(format "/static/~a?t=~a" (file-name-from-path the-path) (hash-ref static-data the-path)))
|
2022-11-11 10:08:06 +00:00
|
|
|
|
2022-11-11 10:33:56 +00:00
|
|
|
; https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
|
2022-11-11 10:08:06 +00:00
|
|
|
(: link-header String)
|
|
|
|
(define link-header
|
2022-11-11 10:33:56 +00:00
|
|
|
(let* ([with-t '(("main.css" "as=style"))]
|
|
|
|
[without-t '(("preact.js" "as=script")
|
|
|
|
("source-sans-pro-v21-vietnamese_latin-ext_latin_greek-ext_greek_cyrillic-ext_cyrillic-regular.woff2" "as=font" "crossorigin" "type=font/woff2"))]
|
|
|
|
[with-t-full (map (λ ([path : (Listof String)]) (cons (get-static-url (car path)) (cdr path))) with-t)]
|
|
|
|
[without-t-full (map (λ ([path : (Listof String)]) (cons (format "/static/~a" (car path)) (cdr path))) without-t)]
|
|
|
|
[all (append with-t-full without-t-full)]
|
|
|
|
[header-parts
|
|
|
|
(for/list : (Listof String) ([full-path all])
|
|
|
|
(define attributes (map (λ ([s : String]) (format "; ~a" s)) (cdr full-path)))
|
|
|
|
(format "<~a>; rel=preload~a" (car full-path) (string-join attributes "")))])
|
|
|
|
(string-join header-parts ", ")))
|