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

Commit 08b7124

Browse files
Haroenvsarahdayan
andauthored
fix(ssr): extend component correctly if at root (vue2) (#1104)
* fix(ssr): extend component correctly if at root (vue2) fixes #1054 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) * not only * Update src/util/__tests__/createServerRootMixin.test.js Co-authored-by: Sarah Dayan <[email protected]> Co-authored-by: Sarah Dayan <[email protected]>
1 parent 359949c commit 08b7124

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/util/__tests__/createServerRootMixin.test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,82 @@ Array [
719719
},
720720
},
721721
]
722+
`);
723+
});
724+
725+
it('works when component is at root (and therefore has no $vnode)', async () => {
726+
const searchClient = createFakeClient();
727+
let mainIndex;
728+
729+
const app = {
730+
render: renderCompat(h =>
731+
/**
732+
* This code triggers this warning in Vue 3:
733+
* > Non-function value encountered for default slot. Prefer function slots for better performance.
734+
*
735+
* To fix it, replace the third argument
736+
* > [h(...), h(...)]
737+
* with
738+
* > { default: () => [h(...), h(...)] }
739+
*
740+
* but it's not important (and not compatible in vue2), we're leaving it as-is.
741+
*/
742+
h(InstantSearchSsr, {}, [
743+
h(Configure, {
744+
attrs: {
745+
hitsPerPage: 100,
746+
},
747+
}),
748+
h(SearchBox),
749+
])
750+
),
751+
};
752+
753+
const wrapper = createSSRApp({
754+
mixins: [
755+
forceIsServerMixin,
756+
createServerRootMixin({
757+
searchClient,
758+
indexName: 'hello',
759+
}),
760+
],
761+
serverPrefetch() {
762+
return this.instantsearch.findResultsState({
763+
component: this,
764+
renderToString,
765+
});
766+
},
767+
created() {
768+
mainIndex = this.instantsearch.mainIndex;
769+
},
770+
render: renderCompat(h => h(app)),
771+
});
772+
773+
await renderToString(wrapper);
774+
775+
expect(mainIndex.getWidgetState()).toMatchInlineSnapshot(`
776+
Object {
777+
"hello": Object {
778+
"configure": Object {
779+
"hitsPerPage": 100,
780+
},
781+
},
782+
}
783+
`);
784+
785+
expect(searchClient.search).toHaveBeenCalledTimes(1);
786+
expect(searchClient.search.mock.calls[0][0]).toMatchInlineSnapshot(`
787+
Array [
788+
Object {
789+
"indexName": "hello",
790+
"params": Object {
791+
"facets": Array [],
792+
"hitsPerPage": 100,
793+
"query": "",
794+
"tagFilters": "",
795+
},
796+
},
797+
]
722798
`);
723799
});
724800
}

src/util/createServerRootMixin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ function defaultCloneComponent(componentInstance, { mixins = [] } = {}) {
5656

5757
const Extended = componentInstance.$vnode
5858
? componentInstance.$vnode.componentOptions.Ctor.extend(options)
59-
: Vue2.component(Object.assign({}, componentInstance.$options, options));
59+
: Vue2.component(
60+
options.name,
61+
Object.assign({}, componentInstance.$options, options)
62+
);
6063

6164
app = new Extended({
6265
propsData: componentInstance.$options.propsData,

0 commit comments

Comments
 (0)