| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | _ "embed" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | // speculationPath is Mapa's speculation ruleset (build note 70): a |
| 9 | // page or a day is fetched ahead when the pointer rests on a link to |
| 10 | // it (about 200ms, or on pointer-down), so the click finds it already |
| 11 | // here. Prefetch only, never prerender: no script of the next page |
| 12 | // runs before it is opened. A GET never writes (gate 10), so fetching |
| 13 | // a page nobody opens changes nothing. |
| 14 | const speculationPath = "/speculation-rules" |
| 15 | |
| 16 | // speculationRules is the ruleset: links to /p/{id} and /d/{date}, |
| 17 | // moderately eager, except while the editor holds text it would commit |
| 18 | // on leaving (<html data-mapa-uncommitted>, set by |
| 19 | // editor/page.mjs): a page fetched before that |
| 20 | // commit would not show it. |
| 21 | // |
| 22 | //go:embed speculation.json |
| 23 | var speculationRules []byte |
| 24 | |
| 25 | // serveSpeculationRules answers speculationPath. Browsers take a |
| 26 | // ruleset only as application/speculationrules+json, which a file |
| 27 | // under /static/ would not be served as. |
| 28 | func serveSpeculationRules(writer http.ResponseWriter, _ *http.Request) { |
| 29 | header := writer.Header() |
| 30 | header.Set("Content-Type", "application/speculationrules+json") |
| 31 | header.Set("Cache-Control", "public, max-age=3600") |
| 32 | writer.Write(speculationRules) |
| 33 | } |
| 34 | |
| 35 | // speculate names Mapa's ruleset on a page. It replaces rastrillo's |
| 36 | // own (Serve's default), which prerenders the links of rastrillo's |
| 37 | // shell sidebar; Mapa does not use that sidebar, so it matched nothing. |
| 38 | // A header, not an inline <script>: the CSP refuses inline scripts, |
| 39 | // and a ruleset delivered by header is not a script. |
| 40 | func speculate(writer http.ResponseWriter) { |
| 41 | writer.Header().Set("Speculation-Rules", `"`+speculationPath+`"`) |
| 42 | } |
| 43 | |