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

Commit 84f79eb

Browse files
authored
feat(ssr): forward router instance to findResultsState clone (#863)
* feat(ssr): forward router instance to findResultsState clone * test: assert that $router is accessible * chore(refactor): smaller * pin dependency
1 parent 5b90352 commit 84f79eb

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"vue-jest": "2.6.0",
104104
"vue-json-tree": "0.3.3",
105105
"vue-loader": "14.2.2",
106+
"vue-router": "3.4.3",
106107
"vue-server-renderer": "^2.6.11",
107108
"vue-slider-component": "3.0.15",
108109
"vue-template-compiler": "2.5.18",

src/util/__tests__/createServerRootMixin.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue';
22
import { mount } from '@vue/test-utils';
33
import _renderToString from 'vue-server-renderer/basic';
4+
import Router from 'vue-router';
45
import { createServerRootMixin } from '../createServerRootMixin';
56
import InstantSearchSsr from '../../components/InstantSearchSsr';
67
import Configure from '../../components/Configure';
@@ -213,6 +214,54 @@ Array [
213214
]
214215
`);
215216
});
217+
218+
it('forwards router', async () => {
219+
const searchClient = createFakeClient();
220+
221+
const router = new Router({});
222+
223+
// there are two renders of App, each with an assertion
224+
expect.assertions(2);
225+
226+
const App = Vue.component('App', {
227+
mixins: [
228+
forceIsServerMixin,
229+
createServerRootMixin({
230+
searchClient,
231+
indexName: 'hello',
232+
}),
233+
],
234+
data() {
235+
expect(this.$router).toBe(router);
236+
return {};
237+
},
238+
render(h) {
239+
return h(InstantSearchSsr, {}, [
240+
h(Configure, {
241+
attrs: {
242+
hitsPerPage: 100,
243+
},
244+
}),
245+
h(SearchBox),
246+
]);
247+
},
248+
serverPrefetch() {
249+
return this.instantsearch.findResultsState(this);
250+
},
251+
});
252+
253+
Vue.use(Router);
254+
255+
const wrapper = new Vue({
256+
mixins: [forceIsServerMixin],
257+
router,
258+
render(h) {
259+
return h(App);
260+
},
261+
});
262+
263+
await renderToString(wrapper);
264+
});
216265
});
217266

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

src/util/createServerRootMixin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) {
7373
fetch: undefined,
7474
_base: undefined,
7575
name: 'ais-ssr-root-component',
76+
// copy over global Vue APIs
77+
router: componentInstance.$router,
7678
};
7779

7880
const extended = componentInstance.$vnode

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15612,6 +15612,11 @@ vue-property-decorator@^8.0.0:
1561215612
dependencies:
1561315613
vue-class-component "^7.0.1"
1561415614

15615+
15616+
version "3.4.3"
15617+
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.4.3.tgz#fa93768616ee338aa174f160ac965167fa572ffa"
15618+
integrity sha512-BADg1mjGWX18Dpmy6bOGzGNnk7B/ZA0RxuA6qedY/YJwirMfKXIDzcccmHbQI0A6k5PzMdMloc0ElHfyOoX35A==
15619+
1561515620
vue-server-renderer@^2.6.11:
1561615621
version "2.6.11"
1561715622
resolved "https://registry.yarnpkg.com/vue-server-renderer/-/vue-server-renderer-2.6.11.tgz#be8c9abc6aacc309828a755c021a05fc474b4bc3"

0 commit comments

Comments
 (0)