Skip to content

Commit

Permalink
exclude route components from keep-alive
Browse files Browse the repository at this point in the history
  • Loading branch information
TeodoraPavlova committed Feb 1, 2024
1 parent 033f4cf commit 15f53f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
27 changes: 6 additions & 21 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</nav>
<AlertDisplay/>
<div class="container mt-2">
<router-view v-slot="{Component}" :key="routerViewKey">
<keep-alive>
<router-view v-slot="{Component}">
<keep-alive :exclude="nonCachedRouteComponents">
<component :is="Component"/>
</keep-alive>
</router-view>
Expand Down Expand Up @@ -60,8 +60,7 @@ export default {
return {
activeSchema: null,
schemaDetails: {},
apiInfo: null,
routerViewKey: null,
apiInfo: null
}
},
provide() {
Expand All @@ -84,8 +83,8 @@ export default {
}
return _avail_schemas
},
nonCachedRouteNames () {
return this.$router.options.routes.filter(route => !route.cached).map(route => route.name);
nonCachedRouteComponents () {
return this.$router.options.routes.filter(route => !route.cached).map(route => route.component.name);
},
},
methods: {
Expand All @@ -103,25 +102,11 @@ export default {
}
this.activeSchema = this.schemaDetails[schemaSlug];
},
async setRouterViewKey() {
// We are binding routerViewKey to <router-view> component in order to trigger an instance replacement
// on route change. We are doing this only for routes we don't want to get cached.
if (this.nonCachedRouteNames.includes(this.$route.name)) {
this.routerViewKey = this.$route.fullPath;
} else if (this.routerViewKey !== null) {
this.routerViewKey = null;
}
}
},
watch: {
$route: {
async handler(oldValue, newValue) {
if (oldValue !== newValue) {
await this.setRouterViewKey(); // run this first in order to not trigger unnecessary reloads.
await this.getSchemaFromRoute();
}
},
handler: "getSchemaFromRoute",
immediate: true, // runs immediately with mount() instead of calling method on mount hook
},
},
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/Entity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export default {
if (newValue?.name) {
document.title = newValue.name;
}
},
async "$route.params.entitySlug"() {
await this.updateEntity();
},
$route: {
handler: "updateEntity",
immediate: true
},
}
};
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/EntityList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export default {
return this.numSelected === 1 ? 'entity' : 'entities'
}
},
activated() {
this.getEntities({resetPage: false});
},
watch: {
currentPage(oldPage, newPage) {
if (oldPage !== newPage) {
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/components/inputs/ReferencedEntitySelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export default {
text: "Apply",
callback: this.onSelect
})
]
],
currentEntitySlug: this.$route.params.entitySlug,
}
},
activated() {
Expand All @@ -78,6 +79,11 @@ export default {
if (!this.modelValue) {
return;
}
if (this.currentEntitySlug !== this.$route.params.entitySlug) {
this.selected.length = 0;
this.currentEntitySlug = this.$route.params.entitySlug;
}
const preselectedIds = this.selected.map(x => x.id);
let toQuery = this.modelValue;
if (!Array.isArray(this.modelValue)) {
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/layout/ModalDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ export default {
default: ""
}
},
deactivated() {
Array.from(document.querySelectorAll('.modal-backdrop')).forEach(function (el) {
el.classList.remove('modal-backdrop');
});
},
computed: {
uniqueId() {
return this.modalId || `modal-${randomUUID()}`;
Expand Down

0 comments on commit 15f53f2

Please sign in to comment.