Skip to content

Commit

Permalink
feat(ssr): forward propsData to recreated component (algolia/vue-inst…
Browse files Browse the repository at this point in the history
…antsearch#865)

* feat(ssr): forward propsData to recreated component

TODO: this throws a warning (but works) because of:

https://github.com/vuejs/vue/blob/7912f75c5eb09e0aef3e4bfd8a3bb78cad7540d7/src/core/util/options.js#L34-L44

I'm not fully sure how to do `new Vue` or pass this parameter correctly. If I `Object.assign` it for `app = new Vue({...extended, propsData})`, it also doesn't work :/

* fix

co-authored-by: Eunjae Lee <eunjae-lee@users.noreply.github.com>

Co-authored-by: Eunjae Lee <eunjae-lee@users.noreply.github.com>
  • Loading branch information
Haroenv and eunjae-lee authored Sep 30, 2020
1 parent 6bb8e08 commit 77d0cc4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,57 @@ Array [

await renderToString(wrapper);
});

it('forwards props', async () => {
const searchClient = createFakeClient();

// there are two renders of App, each with an assertion
expect.assertions(2);

const someProp = { data: Math.random() };

const App = Vue.component('App', {
mixins: [
forceIsServerMixin,
createServerRootMixin({
searchClient,
indexName: 'hello',
}),
],
props: {
someProp: {
required: true,
type: Object,
validator(value) {
expect(value).toBe(someProp);
return value === someProp;
},
},
},
render(h) {
return h(InstantSearchSsr, {}, [
h(Configure, {
attrs: {
hitsPerPage: 100,
},
}),
h(SearchBox),
]);
},
serverPrefetch() {
return this.instantsearch.findResultsState(this);
},
});

const wrapper = new Vue({
mixins: [forceIsServerMixin],
render(h) {
return h(App, { props: { someProp } });
},
});

await renderToString(wrapper);
});
});

describe('hydrate', () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/vue-instantsearch/src/util/createServerRootMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) {
store: componentInstance.$store,
};

const extended = componentInstance.$vnode
const Extended = componentInstance.$vnode
? componentInstance.$vnode.componentOptions.Ctor.extend(options)
: Object.assign({}, componentInstance.$options, options);
: Vue.component(
Object.assign({}, componentInstance.$options, options)
);

app = new Vue(extended);
app = new Extended({
propsData: componentInstance.$options.propsData,
});

app.$options.serverPrefetch = [];

Expand Down

0 comments on commit 77d0cc4

Please sign in to comment.