Improved performance for very large pages

This commit is contained in:
squidfunk 2022-03-06 13:52:57 +01:00
parent 1d9d6782dd
commit e71891f497
5 changed files with 47 additions and 34 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

@ -214,7 +214,7 @@
</script> </script>
{% endblock %} {% endblock %}
{% block scripts %} {% block scripts %}
<script src="{{ 'assets/javascripts/bundle.3900859e.min.js' | url }}"></script> <script src="{{ 'assets/javascripts/bundle.467223ff.min.js' | url }}"></script>
{% for path in config["extra_javascript"] %} {% for path in config["extra_javascript"] %}
<script src="{{ path | url }}"></script> <script src="{{ path | url }}"></script>
{% endfor %} {% endfor %}

View File

@ -28,10 +28,12 @@ import {
defer, defer,
distinctUntilChanged, distinctUntilChanged,
distinctUntilKeyChanged, distinctUntilKeyChanged,
filter,
finalize, finalize,
map, map,
mergeWith, mergeWith,
switchMap, switchMap,
take,
takeLast, takeLast,
takeUntil, takeUntil,
tap tap
@ -40,7 +42,8 @@ import {
import { feature } from "~/_" import { feature } from "~/_"
import { import {
getElementContentSize, getElementContentSize,
watchElementSize watchElementSize,
watchElementVisibility
} from "~/browser" } from "~/browser"
import { renderClipboardButton } from "~/templates" import { renderClipboardButton } from "~/templates"
@ -153,7 +156,9 @@ export function mountCodeBlock(
el: HTMLElement, options: MountOptions el: HTMLElement, options: MountOptions
): Observable<Component<CodeBlock | Annotation>> { ): Observable<Component<CodeBlock | Annotation>> {
const { matches: hover } = matchMedia("(hover)") const { matches: hover } = matchMedia("(hover)")
return defer(() => {
/* Defer mounting of code block - see https://bit.ly/3vHVoVD */
const factory$ = defer(() => {
const push$ = new Subject<CodeBlock>() const push$ = new Subject<CodeBlock>()
push$.subscribe(({ scrollable }) => { push$.subscribe(({ scrollable }) => {
if (scrollable && hover) if (scrollable && hover)
@ -213,4 +218,12 @@ export function mountCodeBlock(
map(state => ({ ref: el, ...state })) map(state => ({ ref: el, ...state }))
) )
}) })
/* Mount code block on first sight */
return watchElementVisibility(el)
.pipe(
filter(visible => visible),
take(1),
switchMap(() => factory$)
)
} }