Skip to content

Commit

Permalink
Fixed invalid anchor offsets when using instant loading
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Aug 7, 2021
1 parent 51d4920 commit adafd84
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion material/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ <h1>{{ page.title | d(config.site_name, true)}}</h1>
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.806c4996.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.8fb3741f.min.js' | url }}"></script>
{% for path in config["extra_javascript"] %}
<script src="{{ path | url }}"></script>
{% endfor %}
Expand Down
47 changes: 30 additions & 17 deletions src/assets/javascripts/integrations/instant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,23 @@ export function setupInstantLoading(
/* Handle HTML and SVG elements */
if (ev.target instanceof Element) {
const el = ev.target.closest("a")
if (el && !el.target && urls.includes(el.href)) {
ev.preventDefault()
return of({
url: new URL(el.href)
})
if (el && !el.target) {
const url = new URL(el.href)

/* Canonicalize URL */
url.search = ""
url.hash = ""

/* Check if URL should be intercepted */
if (
url.pathname !== location.pathname &&
urls.includes(url.toString())
) {
ev.preventDefault()
return of({
url: new URL(el.href)
})
}
}
}
return NEVER
Expand Down Expand Up @@ -252,18 +264,6 @@ export function setupInstantLoading(
)
.subscribe(document$)

/* Emit history state change */
merge(push$, pop$)
.pipe(
sample(document$)
)
.subscribe(({ url, offset }) => {
if (url.hash && !offset)
setLocationHash(url.hash)
else
setViewportOffset(offset || { y: 0 })
})

/* Replace meta tags and components */
document$
.pipe(
Expand Down Expand Up @@ -324,6 +324,19 @@ export function setupInstantLoading(
)
.subscribe()

/* Emit history state change */
merge(push$, pop$)
.pipe(
sample(document$),
)
.subscribe(({ url, offset }) => {
if (url.hash && !offset) {
setLocationHash(url.hash)
} else {
setViewportOffset(offset || { y: 0 })
}
})

/* Debounce update of viewport offset */
viewport$
.pipe(
Expand Down

0 comments on commit adafd84

Please sign in to comment.