|
| 1 | +<template> |
| 2 | + <ais-instant-search-ssr> |
| 3 | + <ais-index |
| 4 | + index-name="instant_search_demo_query_suggestions" |
| 5 | + index-id="querySuggestions" |
| 6 | + > |
| 7 | + <ais-search-box /> |
| 8 | + <ais-configure :hits-per-page.camel="5" /> |
| 9 | + <ais-hits> |
| 10 | + <template v-slot:item="{ item }"> |
| 11 | + <ais-highlight |
| 12 | + attribute="query" |
| 13 | + :hit="item" |
| 14 | + /> |
| 15 | + </template> |
| 16 | + </ais-hits> |
| 17 | + <ais-pagination /> |
| 18 | + </ais-index> |
| 19 | + <ais-search-box /> |
| 20 | + <ais-stats /> |
| 21 | + <ais-index |
| 22 | + index-id="refinement" |
| 23 | + index-name="instant_search" |
| 24 | + > |
| 25 | + <ais-refinement-list attribute="brand" /> |
| 26 | + </ais-index> |
| 27 | + <ais-hits> |
| 28 | + <template v-slot:item="{ item }"> |
| 29 | + <p> |
| 30 | + <ais-highlight |
| 31 | + attribute="name" |
| 32 | + :hit="item" |
| 33 | + /> |
| 34 | + </p> |
| 35 | + <p> |
| 36 | + <ais-highlight |
| 37 | + attribute="brand" |
| 38 | + :hit="item" |
| 39 | + /> |
| 40 | + </p> |
| 41 | + </template> |
| 42 | + </ais-hits> |
| 43 | + <ais-pagination /> |
| 44 | + </ais-instant-search-ssr> |
| 45 | +</template> |
| 46 | + |
| 47 | +<script> |
| 48 | +import { |
| 49 | + AisInstantSearchSsr, |
| 50 | + AisIndex, |
| 51 | + AisConfigure, |
| 52 | + AisRefinementList, |
| 53 | + AisHits, |
| 54 | + AisHighlight, |
| 55 | + AisSearchBox, |
| 56 | + AisStats, |
| 57 | + AisPagination, |
| 58 | + createServerRootMixin, |
| 59 | +} from 'vue-instantsearch'; // eslint-disable-line import/no-unresolved |
| 60 | +import algoliasearch from 'algoliasearch/lite'; |
| 61 | +import _renderToString from 'vue-server-renderer/basic'; |
| 62 | +
|
| 63 | +function renderToString(app) { |
| 64 | + return new Promise((resolve, reject) => { |
| 65 | + _renderToString(app, (err, res) => { |
| 66 | + if (err) reject(err); |
| 67 | + resolve(res); |
| 68 | + }); |
| 69 | + }); |
| 70 | +} |
| 71 | +
|
| 72 | +const searchClient = algoliasearch( |
| 73 | + 'latency', |
| 74 | + '6be0576ff61c053d5f9a3225e2a90f76' |
| 75 | +); |
| 76 | +
|
| 77 | +export default { |
| 78 | + mixins: [ |
| 79 | + createServerRootMixin({ |
| 80 | + searchClient, |
| 81 | + indexName: 'instant_search', |
| 82 | + initialUiState: { |
| 83 | + instant_search: { |
| 84 | + query: 'iphone', |
| 85 | + page: 3, |
| 86 | + }, |
| 87 | + refinement: { |
| 88 | + refinementList: { |
| 89 | + brand: ['Apple'], |
| 90 | + }, |
| 91 | + }, |
| 92 | + querySuggestions: { |
| 93 | + query: 'k', |
| 94 | + page: 2, |
| 95 | + configure: { |
| 96 | + hitsPerPage: 5, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }), |
| 101 | + ], |
| 102 | + serverPrefetch() { |
| 103 | + return this.instantsearch |
| 104 | + .findResultsState({ component: this, renderToString }) |
| 105 | + .then(algoliaState => { |
| 106 | + this.$ssrContext.nuxt.algoliaState = algoliaState; |
| 107 | + }); |
| 108 | + }, |
| 109 | + beforeMount() { |
| 110 | + const results = window.__NUXT__.algoliaState; |
| 111 | +
|
| 112 | + this.instantsearch.hydrate(results); |
| 113 | + }, |
| 114 | + components: { |
| 115 | + AisInstantSearchSsr, |
| 116 | + AisIndex, |
| 117 | + AisConfigure, |
| 118 | + AisRefinementList, |
| 119 | + AisHits, |
| 120 | + AisHighlight, |
| 121 | + AisSearchBox, |
| 122 | + AisStats, |
| 123 | + AisPagination, |
| 124 | + }, |
| 125 | + head() { |
| 126 | + return { |
| 127 | + link: [ |
| 128 | + { |
| 129 | + rel: 'stylesheet', |
| 130 | + href: |
| 131 | + 'https://unpkg.com/[email protected]/themes/algolia-min.css', |
| 132 | + }, |
| 133 | + ], |
| 134 | + }; |
| 135 | + }, |
| 136 | +}; |
| 137 | +</script> |
| 138 | + |
| 139 | +<style> |
| 140 | +.ais-Hits-list { |
| 141 | + text-align: left; |
| 142 | +} |
| 143 | +.ais-Hits-list:empty { |
| 144 | + margin: 0; |
| 145 | +} |
| 146 | +.ais-InstantSearch { |
| 147 | + margin: 1em; |
| 148 | +} |
| 149 | +</style> |
0 commit comments