|
1 | 1 | # Add search to Gridsome |
2 | 2 |
|
3 | | -_Contribute..._ |
| 3 | +## Using Algolia |
| 4 | + |
| 5 | +Currently [Algolia](https://www.algolia.com) offers up to 10,000 search requests for free, which should be enough for any site with low traffic - especially if search isn't used as often. |
| 6 | + |
| 7 | +### Setup |
| 8 | + |
| 9 | +1. Create a free Algolia account. |
| 10 | +2. Install [gridsome-plugin-algolia](https://gridsome.org/plugins/gridsome-plugin-algolia): |
| 11 | +```sh |
| 12 | +# NPM |
| 13 | +npm install --save gridsome-plugin-algolia |
| 14 | + |
| 15 | +# Yarn |
| 16 | +yarn add gridsome-plugin-algolia |
| 17 | +``` |
| 18 | + |
| 19 | +3. Follow the setup instructions on the plugin page for setting up the Algolia indexing process. |
| 20 | +4. Create the UI using `algoliasearch` and `vue-instantsearch`: |
| 21 | +```sh |
| 22 | +# NPM |
| 23 | +npm install --save algoliasearch vue-instantsearch |
| 24 | + |
| 25 | +#Yarn |
| 26 | +yarn add algoliasearch vue-instantsearch |
| 27 | +``` |
| 28 | + |
| 29 | +### Creating the UI |
| 30 | + |
| 31 | +Create a Search component that you can include anywhere in your site: |
| 32 | + |
| 33 | +```html |
| 34 | +<template> |
| 35 | + <ClientOnly> |
| 36 | + <ais-instant-search |
| 37 | + :search-client="searchClient" |
| 38 | + index-name="posts" |
| 39 | + > |
| 40 | + <ais-configure |
| 41 | + :hits-per-page.camel="100" |
| 42 | + :distinct="true" |
| 43 | + /> |
| 44 | + <ais-search-box placeholder="Search" :show-loading-indicator="true" ref="search"></ais-search-box> |
| 45 | + <ais-hits> |
| 46 | + <div class="results" slot-scope="{ items }" @click="toggle(false)"> |
| 47 | + <template v-for="item in items"> |
| 48 | + <g-link :to="item.path" class="card"> |
| 49 | + <g-image :src="item.image" width="200"></g-image> |
| 50 | + <p>{{item.title}}</p> |
| 51 | + </g-link> |
| 52 | + </template> |
| 53 | + <ais-pagination /> |
| 54 | + </div> |
| 55 | + </ais-hits> |
| 56 | + <ais-powered-by /> |
| 57 | + </ais-instant-search> |
| 58 | + </ClientOnly> |
| 59 | +</template> |
| 60 | + |
| 61 | +<script> |
| 62 | +import algoliasearch from 'algoliasearch/lite' |
| 63 | +
|
| 64 | +function onCatch(err) { |
| 65 | + console.warn(err) |
| 66 | +} |
| 67 | +
|
| 68 | +export default { |
| 69 | + components: { |
| 70 | + AisInstantSearch: () => |
| 71 | + import ('vue-instantsearch') |
| 72 | + .then(a => a.AisInstantSearch) |
| 73 | + .catch(onCatch), |
| 74 | + AisConfigure: () => |
| 75 | + import ('vue-instantsearch') |
| 76 | + .then(a => a.AisConfigure) |
| 77 | + .catch(onCatch), |
| 78 | + AisSearchBox: () => |
| 79 | + import ('vue-instantsearch') |
| 80 | + .then(a => a.AisSearchBox) |
| 81 | + .catch(onCatch), |
| 82 | + AisHits: () => |
| 83 | + import ('vue-instantsearch') |
| 84 | + .then(a => a.AisHits) |
| 85 | + .catch(onCatch), |
| 86 | + AisPagination: () => |
| 87 | + import ('vue-instantsearch') |
| 88 | + .then(a => a.AisPagination) |
| 89 | + .catch(onCatch), |
| 90 | + AisPoweredBy: () => |
| 91 | + import ('vue-instantsearch') |
| 92 | + .then(a => a.AisPoweredBy) |
| 93 | + .catch(onCatch) |
| 94 | + } |
| 95 | +} |
| 96 | +</script> |
| 97 | + |
| 98 | +<style> |
| 99 | +.results { |
| 100 | + display: flex; |
| 101 | + flex-wrap: wrap; |
| 102 | + align-items: stretch; |
| 103 | + justify-content: space-between; |
| 104 | +} |
| 105 | +
|
| 106 | +.card { |
| 107 | + min-width: 200px; |
| 108 | + max-width: 300px; |
| 109 | + flex: 1; |
| 110 | + text-align: center; |
| 111 | + padding: 10px; |
| 112 | +} |
| 113 | +
|
| 114 | +.card img { |
| 115 | + width: 100%; |
| 116 | +} |
| 117 | +</style> |
| 118 | +``` |
| 119 | + |
| 120 | +### Using the Search Component |
| 121 | + |
| 122 | +When including the search component on your page, use [LazyHydrate](https://github.com/maoberlehner/vue-lazy-hydration) to only import it when needed. eg. |
| 123 | + |
| 124 | +```html |
| 125 | +<template> |
| 126 | + <div> |
| 127 | + <h1>Search</h1> |
| 128 | + <LazyHydrate on-interaction> |
| 129 | + <Search /> |
| 130 | + </LazyHydrate> |
| 131 | + </div> |
| 132 | +</template> |
| 133 | +<script> |
| 134 | +export default { |
| 135 | + components: { |
| 136 | + Search: () import('~/components/Search.vue') |
| 137 | + } |
| 138 | +} |
| 139 | +</script> |
| 140 | +``` |
0 commit comments