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

Commit acda293

Browse files
authored
fix(nuxt3): forward $nuxt on the instance by default (#1112)
* fix(nuxt3): forward $nuxt on the instance by default This is similar to how we forward $router and $store, but in this case it needs to happen before the beforeCreate of nuxt happens * conditionnel présent
1 parent 71c25d4 commit acda293

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/util/__tests__/createServerRootMixin.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,60 @@ Array [
660660
await renderToString(wrapper);
661661
});
662662

663+
it('forwards nuxt', async () => {
664+
const searchClient = createFakeClient();
665+
666+
let nuxt = 0;
667+
// every time the function gets called, we get a different "nuxt"
668+
// this can be used to assert both "nuxt" objects are equal
669+
const getNuxtCounter = () => ++nuxt;
670+
671+
// there are two renders of App, each with an assertion
672+
expect.assertions(2);
673+
674+
const App = {
675+
mixins: [
676+
{
677+
beforeCreate() {
678+
this.$nuxt = getNuxtCounter();
679+
},
680+
},
681+
forceIsServerMixin,
682+
createServerRootMixin({
683+
searchClient,
684+
indexName: 'hello',
685+
}),
686+
],
687+
data() {
688+
expect(this.$nuxt).toEqual(1);
689+
return {};
690+
},
691+
render: renderCompat(h =>
692+
h(InstantSearchSsr, {}, [
693+
h(Configure, {
694+
attrs: {
695+
hitsPerPage: 100,
696+
},
697+
}),
698+
h(SearchBox),
699+
])
700+
),
701+
serverPrefetch() {
702+
return this.instantsearch.findResultsState({
703+
component: this,
704+
renderToString,
705+
});
706+
},
707+
};
708+
709+
const wrapper = createSSRApp({
710+
mixins: [forceIsServerMixin],
711+
render: renderCompat(h => h(App)),
712+
});
713+
714+
await renderToString(wrapper);
715+
});
716+
663717
it('searches only once', async () => {
664718
const searchClient = createFakeClient();
665719
const app = {

src/util/createServerRootMixin.js

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

4242
if (isVue3) {
4343
const appOptions = Object.assign({}, componentInstance.$options, options);
44-
appOptions.mixins = [...appOptions.mixins, ...mixins];
44+
appOptions.mixins = [...mixins, ...appOptions.mixins];
4545
app = createSSRApp(appOptions);
4646
if (componentInstance.$router) {
4747
app.use(componentInstance.$router);
@@ -104,6 +104,13 @@ function augmentInstantSearch(instantSearchOptions, cloneComponent) {
104104
app = cloneComponent(component, {
105105
mixins: [
106106
{
107+
beforeCreate() {
108+
if (component.$nuxt) {
109+
// In case of Nuxt (3), we ensure the context is shared between
110+
// the real and cloned component
111+
this.$nuxt = component.$nuxt;
112+
}
113+
},
107114
created() {
108115
instance = this.instantsearch;
109116

0 commit comments

Comments
 (0)