You’re reading the documentation for Vue InstantSearch v4.
Read the migration guide to learn how to upgrade from v3 to v4.
You can still find the v3 documentation for this page.
- Request search results from Algolia
- Render the Vue app with the results of the request
- Store the search results in the page
- Return the HTML page as a string
- Read the search results from the page
- Render (or hydrate) the Vue app with the search results
With Vue CLI
-
Create a Vue app with Vue CLI and add the SSR plugin:
-
Start the development server by running:
npm run ssr:serve
. - Install Vue InstantSearch
-
Add Vue InstantSearch to your app in the
src/main.js
file: -
Create a new page
src/views/Search.vue
and build a search interface:Vue -
Add a route to this page in
src/router.js
:JavaScript -
Update the header in
src/App.vue
:Vue -
For styling, add
instantsearch.css
topublic/index.html
:HTML -
Vue InstantSearch uses ES modules, but the app runs on the server, with Node.js.
That’s why you need to add these modules to the
vue.config.js
configuration file:At this point, Vue.js renders the app on the server. But when you go toJavaScript/search
in your browser, you won’t see the search results on the page. That’s because, by default, Vue InstantSearch starts searching and showing results after the page is rendered for the first time. -
To perform searches on the backend as well, you need to create a backend instance in
src/main.js
:The Vue app can now inject the backend instance of Vue InstantSearch. -
In the main file, replace
ais-instant-search
withais-instant-search-ssr
. You can also remove its props since they’re now passed to thecreateServerRootMixin
function.Vue -
To save the results on the backend, add the InstantSearch state to the
context
using thegetState
function:JavaScript -
Rehydrate the app with the initial request once you start searching.
For this, you need to save the data on the page.
Vue CLI provides a way to read the value on the
context
and save it inpublic/index.html
:HTML
With Nuxt 2
The following section describes how to set up server-side rendering with Vue InstantSearch and Nuxt 2. Check the community for solutions for Nuxt 3. First, create a Nuxt app and addvue-instantsearch
:
nuxt.config.js
configuration file:
JavaScript
pages/search.vue
and build a Vue InstantSearch interface:
Vue
-
Add
createServerRootMixin
to create a reusable search instance. -
Add
findResultsState
inserverPrefetch
to perform a search query in the backend. -
Call the
hydrate
method inbeforeMount
. -
Replace
ais-instant-search
withais-instant-search-ssr
-
Add the
createRootMixin
to provide the instance to the component.