chore: add data and lunr via plugins

this way lunr is only set up once, which speeds up the loading of the search page. also makes sure the data doesn't increadse the bundle sizes of the various pages that use it.
This commit is contained in:
2018-09-17 18:09:42 -07:00
parent 3154b1e014
commit 8c45a533b0
5 changed files with 53 additions and 17 deletions

View File

@@ -1,6 +1,23 @@
import Vue from 'vue'; import Vue from 'vue';
import Router from 'vue-router'; import Router from 'vue-router';
import './filters.mjs'; import './filters.mjs';
import strainData from './plugins/strainData.mjs';
import lunr from './plugins/lunr.mjs';
Vue.use(strainData);
Vue.use(
lunr(function lunrSetup() {
// lunr search index setup
this.ref('id');
this.field('name');
this.field('effects');
this.field('uses');
this.field('conditions');
this.field('flavors');
Vue.prototype.$strainData.strains.forEach(doc => this.add(doc));
})
);
Vue.use(Router); Vue.use(Router);

View File

@@ -19,7 +19,6 @@
</template> </template>
<script> <script>
import data from '../../../scraper/db.json';
import store from '../lib/store.mjs'; import store from '../lib/store.mjs';
import StrainList from '../components/StrainList.vue'; import StrainList from '../components/StrainList.vue';
@@ -32,13 +31,12 @@ export default {
const favorites = store.get('favorites') || []; const favorites = store.get('favorites') || [];
return { return {
...data,
favorites favorites
}; };
}, },
computed: { computed: {
favoriteStrains() { favoriteStrains() {
return this.strains.filter(strain => this.favorites.indexOf(strain.id) !== -1); return this.$strainData.strains.filter(strain => this.favorites.indexOf(strain.id) !== -1);
} }
}, },
} }

View File

@@ -24,7 +24,6 @@
import lunr from 'lunr'; import lunr from 'lunr';
import SearchForm from '../components/SearchForm.vue'; import SearchForm from '../components/SearchForm.vue';
import StrainList from '../components/StrainList.vue'; import StrainList from '../components/StrainList.vue';
import data from '../../../scraper/db.json';
import emitter from '../lib/emitter.mjs'; import emitter from '../lib/emitter.mjs';
import store from '../lib/store.mjs'; import store from '../lib/store.mjs';
@@ -36,7 +35,6 @@ export default {
}, },
data() { data() {
return { return {
...data,
matches: [], matches: [],
requirements: { requirements: {
name: '', name: '',
@@ -48,6 +46,21 @@ export default {
}; };
}, },
computed: { computed: {
strains() {
return this.$strainData.strains;
},
effects() {
return this.$strainData.effects;
},
uses() {
return this.$strainData.uses;
},
conditions() {
return this.$strainData.conditions;
},
flavors() {
return this.$strainData.flavors;
},
hasName() { hasName() {
return this.requirements.name.length > 0; return this.requirements.name.length > 0;
}, },
@@ -84,7 +97,7 @@ export default {
} }
try { try {
const hits = this.idx.search(this.searchParams); const hits = this.$lunr.search(this.searchParams);
// .slice(0, limit); // .slice(0, limit);
const refs = hits.map(({ ref }) => parseInt(ref, 10)); const refs = hits.map(({ ref }) => parseInt(ref, 10));
@@ -110,17 +123,6 @@ export default {
}, },
}, },
created() { created() {
// lunr setup
this.idx = lunr(function lunrSetup() {
this.ref('id');
this.field('name');
this.field('effects');
this.field('uses');
this.field('conditions');
this.field('flavors');
data.strains.forEach(doc => this.add(doc));
});
// function to handle search form submissions // function to handle search form submissions
this.searchListener = reqs => { this.searchListener = reqs => {
this.requirements = reqs; this.requirements = reqs;
@@ -142,6 +144,7 @@ export default {
store.set('favorites', favs.concat(id)); store.set('favorites', favs.concat(id));
} }
}); });
this.updateMatches(); // set initial match list this.updateMatches(); // set initial match list
}, },
beforeDestroy() { beforeDestroy() {

View File

@@ -0,0 +1,10 @@
/* eslint no-param-reassign: 0 */
import lunr from 'lunr';
export default function(lunrSetup) {
return {
install(Vue) {
Vue.prototype.$lunr = lunr(lunrSetup);
},
};
}

View File

@@ -0,0 +1,8 @@
/* eslint no-param-reassign: 0 */
import data from '../../../scraper/db.json';
export default {
install(Vue) {
Vue.prototype.$strainData = data;
},
};