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
1
Create Vue app with SSR plugin
Use the Vue CLI to create the app and add the SSR plugin:
2
Start the development server
Run
npm run ssr:serve.3
Install Vue InstantSearch
4
Add Vue InstantSearch to your app
Add the following to the
src/main.js file:5
Build a search interface
Create a new page
src/views/Search.vue and build a search interface:Vue
6
Add route to search interface page
Add route to this page in
src/router.js:JavaScript
7
Update the header
Update in
src/App.vue:Vue
8
Apply styling
Add
instantsearch.css to public/index.html:HTML
9
Add modules to configuration
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 At this point, Vue.js renders the app on the server.
But when you go to
vue.config.js configuration file:JavaScript
/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.10
Create a backend instance
To perform searches on the backend as well, you need to create a backend instance in The Vue app can now inject the backend instance of Vue InstantSearch.
src/main.js:11
Replace InstantSearch with SSR InstantSearch
In the main file, replace
ais-instant-search with ais-instant-search-ssr.
You can also remove its props since they’re now passed to the createServerRootMixin function.Vue
12
Add the InstantSearch state
To save the results on the backend, add the InstantSearch state to the
context using the getState function:JavaScript
13
Rehydrate the app
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 in public/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.1
Create Nuxt app with Vue InstantSearch
Create a Nuxt app and add
vue-instantsearch:2
Add modules to configuratio
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
nuxt.config.js configuration file:JavaScript
3
Build a search interface
Create a new page
pages/search.vue and build a Vue InstantSearch interface:Vue
4
Apply styling
Add the component declarations and the style sheet:
5
Configure Vue InstantSearch for SSR
- Add
createServerRootMixinto create a reusable search instance. - Add
findResultsStateinserverPrefetchto perform a search query in the backend. - Call the
hydratemethod inbeforeMount. - Replace
ais-instant-searchwithais-instant-search-ssr
6
Provide the instance to the component
Add the
createRootMixin: