⚡️ instant soft navigation with prefetch, forge link to profile
Browse files at this commit5d4b0e7768e429e5c5d772838ace339114f11f6eParent: 2fe04f4All checks have passed
Show checks
ci/buildSucceededDetails
- feat
- ⚡️ instant soft navigation with prefetch, forge link to profile
4 changed files +150 -2
- assets/main.css +4 -0
- assets/nav.js +136 -0
- src/app.rs +3 -2
- src/app/assets.rs +7 -0
assets/main.cssmodified+4 -0
| @@ -457,3 +457,7 @@ h1, h2, p { | |||
| 457 | grid-template-columns: 84px minmax(0, 1fr) 20px; | 457 | grid-template-columns: 84px minmax(0, 1fr) 20px; |
| 458 | } | 458 | } |
| 459 | } | 459 | } |
| 460 | + | ||
| 461 | +html[aria-busy="true"] { | ||
| 462 | + cursor: progress; | ||
| 463 | +} | ||
assets/nav.jsadded+136 -0
| @@ -0,0 +1,136 @@ | |||
| 1 | +(function () { | ||
| 2 | + 'use strict'; | ||
| 3 | + if (window.__personalityNav) { return; } | ||
| 4 | + window.__personalityNav = true; | ||
| 5 | + | ||
| 6 | + var root = document.documentElement; | ||
| 7 | + var FILE_PATH = /^\/(fonts\/|main\.css$|nav\.js$|favicon\.ico$)/; | ||
| 8 | + var TTL = 30000; | ||
| 9 | + var HOVER_DELAY = 50; | ||
| 10 | + var cache = new Map(); | ||
| 11 | + var latest = 0; | ||
| 12 | + var inFlight = 0; | ||
| 13 | + var shown = location.pathname + location.search; | ||
| 14 | + var hoverTimer = null; | ||
| 15 | + var hoverLink = null; | ||
| 16 | + | ||
| 17 | + function local(url) { | ||
| 18 | + return url.origin === location.origin && !FILE_PATH.test(url.pathname); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + function linkUrl(a) { | ||
| 22 | + var href = a.getAttribute('href'); | ||
| 23 | + if (href.charAt(0) !== '/' || href.charAt(1) === '/') { return null; } | ||
| 24 | + var url = new URL(href, location.href); | ||
| 25 | + return local(url) ? url : null; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + function linkOf(node) { | ||
| 29 | + return node && node.closest ? node.closest('a[href]') : null; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + function canPrefetch() { | ||
| 33 | + return !(navigator.connection && navigator.connection.saveData); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + function fetchText(href) { | ||
| 37 | + return fetch(href, { headers: { accept: 'text/html' } }).then(function (res) { | ||
| 38 | + var type = res.headers.get('content-type') || ''; | ||
| 39 | + var final = new URL(res.url || href); | ||
| 40 | + if (!res.ok || !/^\s*text\/html/i.test(type) || final.origin !== location.origin) { | ||
| 41 | + throw new Error('not soft'); | ||
| 42 | + } | ||
| 43 | + return res.text(); | ||
| 44 | + }); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + function prefetch(href) { | ||
| 48 | + var key = href.split('#')[0]; | ||
| 49 | + var hit = cache.get(key); | ||
| 50 | + if (hit && Date.now() - hit.at < TTL) { return hit.promise; } | ||
| 51 | + var promise = fetchText(key); | ||
| 52 | + cache.set(key, { promise: promise, at: Date.now() }); | ||
| 53 | + promise.catch(function () { | ||
| 54 | + if (cache.get(key) && cache.get(key).promise === promise) { cache.delete(key); } | ||
| 55 | + }); | ||
| 56 | + return promise; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + function placeHash(hash) { | ||
| 60 | + var el = hash.length > 1 ? document.getElementById(hash.slice(1)) : null; | ||
| 61 | + if (el) { el.scrollIntoView(); } | ||
| 62 | + return !!el; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + function swap(html, url, pop, y) { | ||
| 66 | + var doc = new DOMParser().parseFromString(html, 'text/html'); | ||
| 67 | + if (!doc.body) { throw new Error('no body'); } | ||
| 68 | + document.title = doc.title; | ||
| 69 | + var fresh = document.importNode(doc.body, true); | ||
| 70 | + document.body.replaceChildren.apply(document.body, Array.prototype.slice.call(fresh.childNodes)); | ||
| 71 | + if (!pop) { history.pushState(null, '', url.href); } | ||
| 72 | + shown = url.pathname + url.search; | ||
| 73 | + if (!placeHash(url.hash)) { window.scrollTo(0, pop ? y : 0); } | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + function go(url, pop, y) { | ||
| 77 | + var mark = ++latest; | ||
| 78 | + if (!pop) { history.replaceState({ y: window.scrollY }, '', location.href); } | ||
| 79 | + inFlight += 1; | ||
| 80 | + root.setAttribute('aria-busy', 'true'); | ||
| 81 | + prefetch(url.href).then(function (html) { | ||
| 82 | + if (mark === latest) { swap(html, url, pop, y); } | ||
| 83 | + }).catch(function () { | ||
| 84 | + if (mark === latest) { location.assign(url.href); } | ||
| 85 | + }).then(function () { | ||
| 86 | + inFlight -= 1; | ||
| 87 | + if (inFlight === 0) { root.removeAttribute('aria-busy'); } | ||
| 88 | + }); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + document.addEventListener('click', function (e) { | ||
| 92 | + if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.defaultPrevented) { return; } | ||
| 93 | + var a = linkOf(e.target); | ||
| 94 | + if (!a || a.hasAttribute('target') || a.hasAttribute('download')) { return; } | ||
| 95 | + var url = linkUrl(a); | ||
| 96 | + if (!url) { return; } | ||
| 97 | + if (url.pathname + url.search === shown && url.hash !== '') { return; } | ||
| 98 | + e.preventDefault(); | ||
| 99 | + go(url, false, 0); | ||
| 100 | + }); | ||
| 101 | + | ||
| 102 | + document.addEventListener('mouseover', function (e) { | ||
| 103 | + var a = linkOf(e.target); | ||
| 104 | + if (!a || a === hoverLink) { return; } | ||
| 105 | + clearTimeout(hoverTimer); | ||
| 106 | + hoverTimer = null; | ||
| 107 | + hoverLink = a; | ||
| 108 | + var url = linkUrl(a); | ||
| 109 | + if (!url || !canPrefetch()) { return; } | ||
| 110 | + hoverTimer = setTimeout(function () { | ||
| 111 | + hoverTimer = null; | ||
| 112 | + prefetch(url.href).catch(function () {}); | ||
| 113 | + }, HOVER_DELAY); | ||
| 114 | + }, true); | ||
| 115 | + | ||
| 116 | + document.addEventListener('mouseout', function (e) { | ||
| 117 | + var a = linkOf(e.target); | ||
| 118 | + if (!a || a !== hoverLink || (e.relatedTarget && a.contains(e.relatedTarget))) { return; } | ||
| 119 | + clearTimeout(hoverTimer); | ||
| 120 | + hoverTimer = null; | ||
| 121 | + hoverLink = null; | ||
| 122 | + }, true); | ||
| 123 | + | ||
| 124 | + document.addEventListener('touchstart', function (e) { | ||
| 125 | + var a = linkOf(e.target); | ||
| 126 | + var url = a && canPrefetch() ? linkUrl(a) : null; | ||
| 127 | + if (url) { prefetch(url.href).catch(function () {}); } | ||
| 128 | + }, { passive: true, capture: true }); | ||
| 129 | + | ||
| 130 | + window.addEventListener('popstate', function (e) { | ||
| 131 | + var url = new URL(location.href); | ||
| 132 | + if (!local(url)) { location.reload(); return; } | ||
| 133 | + if (url.pathname + url.search === shown) { placeHash(url.hash); return; } | ||
| 134 | + go(url, true, e.state && e.state.y ? e.state.y : 0); | ||
| 135 | + }); | ||
| 136 | +})(); | ||
src/app.rsmodified+3 -2
| @@ -32,7 +32,7 @@ async fn site_header(cx: &Cx) -> Result<impl View> { | |||
| 32 | <nav class="nav" aria-label="Main"> | 32 | <nav class="nav" aria-label="Main"> |
| 33 | <a href=(posts) aria-current=(posts_current.then_some("page"))>"posts"</a> | 33 | <a href=(posts) aria-current=(posts_current.then_some("page"))>"posts"</a> |
| 34 | <a href=(projects) aria-current=(projects_current.then_some("page"))>"projects"</a> | 34 | <a href=(projects) aria-current=(projects_current.then_some("page"))>"projects"</a> |
| 35 | - <a href="https://git.tahinli.com">"forge ↗"</a> | 35 | + <a href="https://git.tahinli.com/Tahinli">"forge ↗"</a> |
| 36 | </nav> | 36 | </nav> |
| 37 | </div> | 37 | </div> |
| 38 | </header> | 38 | </header> |
| @@ -46,7 +46,7 @@ async fn site_footer(cx: &Cx) -> Result<impl View> { | |||
| 46 | <div class="wrap footer-inner"> | 46 | <div class="wrap footer-inner"> |
| 47 | <span>"Don't you dare go Hollow"</span> | 47 | <span>"Don't you dare go Hollow"</span> |
| 48 | <span class="footer-links"> | 48 | <span class="footer-links"> |
| 49 | - <a href="https://git.tahinli.com">"forge"</a> | 49 | + <a href="https://git.tahinli.com/Tahinli">"forge"</a> |
| 50 | <a href="https://github.com/Tahinli">"github"</a> | 50 | <a href="https://github.com/Tahinli">"github"</a> |
| 51 | <a href="https://linkedin.com/in/ahmetkaangumus">"linkedin"</a> | 51 | <a href="https://linkedin.com/in/ahmetkaangumus">"linkedin"</a> |
| 52 | </span> | 52 | </span> |
| @@ -79,6 +79,7 @@ async fn root_layout(cx: &Cx, slot: Slot<'_>) -> Result<impl View> { | |||
| 79 | <meta name="viewport" content="width=device-width, initial-scale=1"> | 79 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 80 | <title>(title)</title> | 80 | <title>(title)</title> |
| 81 | <link rel="stylesheet" href="/main.css"> | 81 | <link rel="stylesheet" href="/main.css"> |
| 82 | + <script src="/nav.js" defer=""></script> | ||
| 82 | <link rel="icon" href="/favicon.ico" type="image/x-icon"> | 83 | <link rel="icon" href="/favicon.ico" type="image/x-icon"> |
| 83 | </head> | 84 | </head> |
| 84 | <body> | 85 | <body> |
src/app/assets.rsmodified+7 -0
| @@ -19,6 +19,13 @@ pub async fn favicon(cx: &Cx) -> Result<http::Response<Body>> { | |||
| 19 | .body(Body::from(&include_bytes!("../../assets/favicon.ico")[..]))?) | 19 | .body(Body::from(&include_bytes!("../../assets/favicon.ico")[..]))?) |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | +#[route(GET "/nav.js")] | ||
| 23 | +pub async fn nav(cx: &Cx) -> Result<http::Response<Body>> { | ||
| 24 | + Ok(http::Response::builder() | ||
| 25 | + .header(CONTENT_TYPE, "text/javascript; charset=utf-8") | ||
| 26 | + .body(Body::from(include_str!("../../assets/nav.js")))?) | ||
| 27 | +} | ||
| 28 | + | ||
| 22 | #[route(GET "/fonts/{file}")] | 29 | #[route(GET "/fonts/{file}")] |
| 23 | pub async fn font(cx: &Cx) -> Result<http::Response<Body>> { | 30 | pub async fn font(cx: &Cx) -> Result<http::Response<Body>> { |
| 24 | let bytes: &'static [u8] = match file(cx) { | 31 | let bytes: &'static [u8] = match file(cx) { |