Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Cache panel contents after first load
Browse files Browse the repository at this point in the history
Previously:

1) Panels are by default conditionally rendered,
so each time a panel is closed and reopened,
its contents are recreated.

2) Else, panels can be preloaded if explicitly wanted.
However, this adds extra rendering time.

Now:

Case 2 remains the same.
Authors can still choose to preload their panels if they wish.

For Case 1, we cache the rendered content so as to prevent re-rendering,
which
  a) prevents multiple HTTP requests if using the src attribute
  b) solves issues that stem from the destruction and recreation of
     components:
     MarkBind/markbind#868
     MarkBind/markbind#483

Implementation:

Store an additional `isCached` property, which is set to true
whenever the content is fetched.

When `isCached` is true, we switch from
v-if (conditional rendering)
to
v-show (conditional display)
More info: https://vuejs.org/v2/guide/conditional.html#v-show

As a side effect, since once the panel is loaded v-show is always used,
the v-else part of the template is not needed anymore.

Future work:

Investigate the use of keep-alive:
https://vuejs.org/v2/guide/components-dynamic-async.html

Tried to surround the content with <keep-alive></keep-alive>
but did not seem to be cached.
  • Loading branch information
openorclose committed May 22, 2019
1 parent 386e3f0 commit 3a0a681
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/Panel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</slot>
</div>
</div>
<template v-if="preloadBool">
<template v-if="preloadBool || isCached">
<div class="card-collapse"
ref="panel"
v-show="localExpanded"
Expand All @@ -57,21 +57,6 @@
<hr v-show="isSeamless" />
</div>
</template>
<template v-else>
<div class="card-collapse"
ref="panel"
v-if="localExpanded"
>
<div class="card-body">
<slot></slot>
<retriever v-if="hasSrc" ref="retriever" :src="src" :fragment="fragment" delay></retriever>
<panel-switch v-show="isExpandableCard && bottomSwitchBool" :is-open="localExpanded"
@click.native.stop.prevent="collapseThenScrollIntoViewIfNeeded()"
@is-open-event="retrieveOnOpen"></panel-switch>
</div>
<hr v-show="isSeamless" />
</div>
</template>
</div>
</span>
</template>
Expand Down Expand Up @@ -229,7 +214,8 @@
return {
onHeaderHover: false,
localExpanded: false,
localMinimized: false
localMinimized: false,
isCached: false
}
},
methods: {
Expand Down Expand Up @@ -270,6 +256,15 @@
},
watch: {
'localExpanded': function (val, oldVal) {
if (this.isCached) {
return;
}
/* When either one of the values is true,
* it means that the data is/will be cached.
*/
if (val || oldVal) {
this.isCached = true;
}
this.$nextTick(function () {
this.retrieveOnOpen(this, val);
})
Expand Down

0 comments on commit 3a0a681

Please sign in to comment.