Improved anchor jumps after instant loading and fixed Google Analytics

This commit is contained in:
squidfunk 2020-02-29 20:57:11 +01:00
parent ab3ef19498
commit 067df2f6cf
9 changed files with 31 additions and 30 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"assets/javascripts/bundle.js": "assets/javascripts/bundle.4de2b008.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.4de2b008.min.js.map",
"assets/javascripts/bundle.js": "assets/javascripts/bundle.b70c6ac3.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.b70c6ac3.min.js.map",
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.926ffd9e.min.js",
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.926ffd9e.min.js.map",
"assets/stylesheets/main.css": "assets/stylesheets/main.1e07d700.min.css",

View File

@ -195,7 +195,7 @@
{% endblock %}
</div>
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.4de2b008.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.b70c6ac3.min.js' | url }}"></script>
{%- set translations = {} -%}
{%- for key in [
"clipboard.copy",

View File

@ -2,5 +2,5 @@
This file was automatically generated - do not edit
-#}
{% set analytics = config.google_analytics %}
<script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","{{ analytics[0] }}","{{ analytics[1] }}"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){if(this.value){var e=document.location.pathname;ga("send","pageview",e+"?q="+this.value)}})})</script>
<script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","{{ analytics[0] }}","{{ analytics[1] }}"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){if(this.value){var e=document.location.pathname;ga("send","pageview",e+"?q="+this.value)}})}),document.addEventListener("DOMContentSwitch",function(){ga("send","pageview")})</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>

View File

@ -462,6 +462,9 @@ export function initialize(config: unknown) {
.subscribe(([{ url, data }, { title, head }]) => {
console.log("Done", url.href, data)
// trigger custom event
document.dispatchEvent(new CustomEvent("DOMContentSwitch"))
// setDocumentTitle
document.title = title
@ -481,21 +484,20 @@ export function initialize(config: unknown) {
}
}
// // TODO: this doesnt work as expected
// if (!data) {
// const { hash } = new URL(href)
// if (hash) {
// const el = getElement(hash)
// if (typeof el !== "undefined") {
// el.scrollIntoView()
// return
// }
// }
// }
// search drawer close
useToggle("search").subscribe(el => {
setToggle(el, false)
})
// console.log(ev)
// if (!data)
setViewportOffset(data || { y: 0 }) // push state!
// // TODO: this doesnt work as expected
if (url.hash) {
console.log("hash data?", data)
const a = document.createElement("a")
a.href = url.hash
a.click()
} else {
setViewportOffset(data || { y: 0 }) // push state!
}
})
// internal$.subscribe(({ url }) => {

View File

@ -74,16 +74,10 @@ export function patchScripts(
/* Evaluate all scripts via replacement */
els$.subscribe(els => {
for (const el of els) {
if (
el.src || !el.type ||
/^(application|text)\/javascript$/i.test(el.type)
) {
if (el.src || /(^|\/javascript)$/i.test(el.type)) {
const script = document.createElement("script")
if (el.src) {
script.src = el.src
} else {
script.innerText = el.innerText
}
const key = el.src ? "src" : "innerText"
script[key] = el[key]
el.replaceWith(script)
}
}

View File

@ -45,5 +45,10 @@
})
}
})
/* Send page view on document switch (instant loading, custom event) */
document.addEventListener("DOMContentSwitch", function() {
ga("send", "pageview")
})
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>