-
Notifications
You must be signed in to change notification settings - Fork 157
AisInstantSearchSsr does not work well with nuxt i18n translation functions #936
Comments
I can't find the source code easily of vue-instantsearch/src/util/createServerRootMixin.js Lines 71 to 96 in 218238b
It makes a copy of the current component to render intermediary, to get access to the widgets that are its children in |
related to #936, this would allow people to link items to the cloned component themselves
in #937 I'm adding a way to override this manually for the time being (once we release this). In the mean time, it would be really interesting if you could experiment to see what needs to be added to the cloned component to work with nuxt i18n. You can try out this PR by following these instructions: https://ci.codesandbox.io/status/algolia/vue-instantsearch/pr/937/builds/112665 |
Possible that this PR changes something in the behavior of |
Hmm, that PR doesn't actually change anything, it only gives you the possibility to override how the root component is cloned. Do you have a sandbox? I feel like that should behave like this regardless of the change |
Here's how you can use the version from the PR: https://codesandbox.io/s/nowd3 (with the fix for i18n added, which will come after) function $cloneComponent(componentInstance) {
const options = {
serverPrefetch: undefined,
fetch: undefined,
_base: undefined,
name: 'ais-ssr-root-component',
// copy over global Vue APIs
router: componentInstance.$router,
store: componentInstance.$store,
// added this
i18n: componentInstance.$i18n,
};
const Extended = componentInstance.$vnode.componentOptions.Ctor.extend(
options
);
const app = new Extended({
propsData: componentInstance.$options.propsData,
});
// https://stackoverflow.com/a/48195006/3185307
app.$slots = componentInstance.$slots;
app.$root = componentInstance.$root;
app.$options.serverPrefetch = [];
return app;
}
export default {
provide() {
return {
$_ais_ssrInstantSearchInstance: this.instantsearch,
};
},
data() {
// Create it in `data` to access the Vue Router
const mixin = createServerRootMixin({
searchClient,
indexName: 'instant_search',
$cloneComponent,
});
return {
...mixin.data(),
};
},
// ... |
i can confirm that it works fine with that version. There are no SSR errors when using any translation function inside the Thank you very much. |
Yes, it's safe to use in production, just check whenever you update that we didn't change the name of the parameter or something! |
related to #936, this would allow people to link items to the cloned component themselves
Hi.. i updated and it works just fine.. |
Thanks, glad to hear that! |
Not sure if I'm missing something but I'm not clear how I can sovle this issue. Is there a version of the |
@adamchipperfield no... add this function to the page:
the important part here is this: in you data() function where you created your mixin add this:
the important part here is: but currently the library is quit broken in combination with Nuxt. There are a lot workarounds needed to make it really SSR. |
Thanks @podlebar. Where are |
you're not doing the "provide" part of the mixin @adamchipperfield
|
Thanks, that solved the SSR issue! Weirdly though when you visit the page directly (not using the router) then none of the Algolia components work (i.e. the This is since adding the above SSR support. |
maybe post your code.. makes it easier singleIndexMapping needs to be imported: |
This is my bad! I had some other errors which came up due to mismatching nodes. It does seem to work without the singleIndexMapping (or even the whole |
well yes.. if you want the url to change when you click on a facet you need this.. because than you can reload the browser and the url will provide the state for the search.. otherwise your facets will belost after a reload |
@podlebar Where am I importing Also just on the above about |
in nuxt.config.js
|
yep.. see here: #1066 currently it's broken. what i did now (i guess pretty ugly and it's flickering):
there are a lot of workaround at the moment to make Algolia work with Nuxt SSR.. and somehow i feel like nobody cares that it's broken since a long time. |
Hi guys, currently have the same problem with version 4.3.0, since the changes I don't understand where I have to place the $cloneComponent instance with i18n. |
related to algolia/vue-instantsearch#936, this would allow people to link items to the cloned component themselves
Bug 🐞
What is the current behavior?
I use nuxt.js with and for the translation of the whole website i use the official nuxt i18n module (https://i18n.nuxtjs.org/).
The common function to translate strings with nuxt i18n is is
$t()
. When i place this somewhere inside the component i get the error"TypeError: Cannot read property '_t' of undefined"
. Also i use the component withtransform-items
where i translate the labels into the desired language but this leads to the same error.Make a sandbox with the current behavior
Not possible as true SSR with Nuxt is needed..
What is the expected behavior?
The
$t()
function should be usable inside the component without any ugly workaround.My workaround is currently to re-render all components as soo the
mounted()
hook is called.Does this happen only in specific situations?
No.. in al situations where i use a translation function inside a component.
What is the proposed solution?
Make
$t()
available in or find a way to inject$t()
and$tc()
functions into . ($tc()
is used for pluralization.)What is the version you are using?
"nuxt": "^2.14.3"
"vue-instantsearch": "^3.5.0",
"algoliasearch": "^4.8.6",
The text was updated successfully, but these errors were encountered: