Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 986a992

Browse files
authored
feat(ssr): forward vuex instance to findResultsState clone (#864)
Regardless of whether you _need_ to access it, it's commonly done for otherwise prerendered state. Since Vuex is a core plugin, we must support it like this
1 parent 84f79eb commit 986a992

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"vue-server-renderer": "^2.6.11",
108108
"vue-slider-component": "3.0.15",
109109
"vue-template-compiler": "2.5.18",
110-
"vuetify": "1.5.3"
110+
"vuetify": "1.5.3",
111+
"vuex": "3.5.1"
111112
},
112113
"bundlesize": [
113114
{

src/util/__tests__/createServerRootMixin.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue';
22
import { mount } from '@vue/test-utils';
33
import _renderToString from 'vue-server-renderer/basic';
44
import Router from 'vue-router';
5+
import Vuex from 'vuex';
56
import { createServerRootMixin } from '../createServerRootMixin';
67
import InstantSearchSsr from '../../components/InstantSearchSsr';
78
import Configure from '../../components/Configure';
@@ -262,6 +263,54 @@ Array [
262263

263264
await renderToString(wrapper);
264265
});
266+
267+
it('forwards vuex', async () => {
268+
const searchClient = createFakeClient();
269+
270+
Vue.use(Vuex);
271+
272+
const store = new Vuex.Store();
273+
274+
// there are two renders of App, each with an assertion
275+
expect.assertions(2);
276+
277+
const App = Vue.component('App', {
278+
mixins: [
279+
forceIsServerMixin,
280+
createServerRootMixin({
281+
searchClient,
282+
indexName: 'hello',
283+
}),
284+
],
285+
data() {
286+
expect(this.$store).toBe(store);
287+
return {};
288+
},
289+
render(h) {
290+
return h(InstantSearchSsr, {}, [
291+
h(Configure, {
292+
attrs: {
293+
hitsPerPage: 100,
294+
},
295+
}),
296+
h(SearchBox),
297+
]);
298+
},
299+
serverPrefetch() {
300+
return this.instantsearch.findResultsState(this);
301+
},
302+
});
303+
304+
const wrapper = new Vue({
305+
mixins: [forceIsServerMixin],
306+
store,
307+
render(h) {
308+
return h(App);
309+
},
310+
});
311+
312+
await renderToString(wrapper);
313+
});
265314
});
266315

267316
describe('hydrate', () => {

src/util/createServerRootMixin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) {
7575
name: 'ais-ssr-root-component',
7676
// copy over global Vue APIs
7777
router: componentInstance.$router,
78+
store: componentInstance.$store,
7879
};
7980

8081
const extended = componentInstance.$vnode

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15676,6 +15676,11 @@ [email protected]:
1567615676
resolved "https://registry.yarnpkg.com/vuetify/-/vuetify-1.5.3.tgz#4bdc0a72ade1cd3521dc0c08a363ea40b914443e"
1567715677
integrity sha512-4V3i4HwKdt//1vAEiex3DpHsLwX3oHCIx/gpukSYvBGJUgHILUZ5JOEOAQJ1yvMV1w3P+dsmvlmRdthjci5VRg==
1567815678

15679+
15680+
version "3.5.1"
15681+
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.5.1.tgz#f1b8dcea649bc25254cf4f4358081dbf5da18b3d"
15682+
integrity sha512-w7oJzmHQs0FM9LXodfskhw9wgKBiaB+totOdb8sNzbTB2KDCEEwEs29NzBZFh/lmEK1t5tDmM1vtsO7ubG1DFw==
15683+
1567915684
walker@~1.0.5:
1568015685
version "1.0.7"
1568115686
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"

0 commit comments

Comments
 (0)