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

Commit 4b0d295

Browse files
Haroenvsarahdayan
andauthored
fix(server): extend component correctly if at root (#1105)
* fix(server): extend component correctly if at root Essentially the problem is that $vnode is usually available, but not when the this is a root Vue instance. In that case we are in the "Vue.component" case, which before now always was wrong (it takes two arguments, not one) Same as #1104 see also #1054 * Update src/util/createServerRootMixin.js * make the funky new test pass * update test name, as in #1104 * Update src/util/__tests__/createServerRootMixin.test.js Co-authored-by: Sarah Dayan <[email protected]> Co-authored-by: Sarah Dayan <[email protected]>
1 parent 7fb85d0 commit 4b0d295

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/util/__tests__/createServerRootMixin.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,78 @@ Array [
682682
expect(instantsearch.helper).toEqual(expect.any(AlgoliaSearchHelper));
683683
expect(instantsearch.mainHelper).toEqual(expect.any(AlgoliaSearchHelper));
684684
});
685+
686+
it('works when component is at root (and therefore has no $vnode)', async () => {
687+
const searchClient = createFakeClient();
688+
let mainIndex;
689+
690+
const app = {
691+
render: h =>
692+
/**
693+
* This code triggers this warning in Vue 3:
694+
* > Non-function value encountered for default slot. Prefer function slots for better performance.
695+
*
696+
* To fix it, replace the third argument
697+
* > [h(...), h(...)]
698+
* with
699+
* > { default: () => [h(...), h(...)] }
700+
*
701+
* but it's not important (and not compatible in vue2), we're leaving it as-is.
702+
*/
703+
h(InstantSearchSsr, {}, [
704+
h(Configure, {
705+
attrs: {
706+
hitsPerPage: 100,
707+
},
708+
}),
709+
h(SearchBox),
710+
]),
711+
};
712+
713+
const wrapper = new Vue({
714+
mixins: [
715+
forceIsServerMixin,
716+
createServerRootMixin({
717+
searchClient,
718+
indexName: 'hello',
719+
}),
720+
],
721+
serverPrefetch() {
722+
return this.instantsearch.findResultsState(this);
723+
},
724+
created() {
725+
mainIndex = this.instantsearch.mainIndex;
726+
},
727+
render: h => h(app),
728+
});
729+
730+
await renderToString(wrapper);
731+
732+
expect(mainIndex.getWidgetState()).toMatchInlineSnapshot(`
733+
Object {
734+
"hello": Object {
735+
"configure": Object {
736+
"hitsPerPage": 100,
737+
},
738+
},
739+
}
740+
`);
741+
742+
expect(searchClient.search).toHaveBeenCalledTimes(1);
743+
expect(searchClient.search.mock.calls[0][0]).toMatchInlineSnapshot(`
744+
Array [
745+
Object {
746+
"indexName": "hello",
747+
"params": Object {
748+
"facets": Array [],
749+
"hitsPerPage": 100,
750+
"query": "",
751+
"tagFilters": "",
752+
},
753+
},
754+
]
755+
`);
756+
});
685757
});
686758

687759
describe('__forceRender', () => {

src/util/createServerRootMixin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ function defaultCloneComponent(componentInstance) {
5353

5454
const Extended = componentInstance.$vnode
5555
? componentInstance.$vnode.componentOptions.Ctor.extend(options)
56-
: Vue.component(Object.assign({}, componentInstance.$options, options));
56+
: Vue.component(
57+
options.name,
58+
Object.assign({}, componentInstance.$options, options)
59+
);
5760

5861
const app = new Extended({
5962
propsData: componentInstance.$options.propsData,

0 commit comments

Comments
 (0)