algoliasearch-netlify-frontend
algoliasearch-netlify-frontend
is the front-end bundle we recommend to use with our Netlify plugin.
It's designed to be compatible with the index structure extracted by the plugin.
It creates a new search input in your website with an autocomplete menu providing search as you type results.
Usage
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@algolia/algoliasearch-netlify-frontend@1/dist/algoliasearchNetlify.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@algolia/algoliasearch-netlify-frontend@1/dist/algoliasearchNetlify.js"></script>
<script type="text/javascript">
algoliasearchNetlify({
appId: '<YOUR_ALGOLIA_APP_ID>',
apiKey: '<YOUR_ALGOLIA_API_KEY>',
siteId: '<YOUR_NETLIFY_SITE_ID>',
branch: '<YOUR_TARGET_GIT_BRANCH>',
selector: 'div#search',
});
</script>
Available options
Here's the full list of options with their default value.
algoliasearchNetlify({
appId: '<YOUR_ALGOLIA_APP_ID>',
apiKey: '<YOUR_ALGOLIA_API_KEY>',
siteId: '<YOUR_NETLIFY_SITE_ID>',
branch: '<YOUR_TARGET_GIT_BRANCH>',
selector: 'div#search',
analytics: true,
hitsPerPage: 5,
poweredBy: true,
placeholder: 'Search...';
openOnFocus: true;
theme: {
mark: '#fff',
background: '#23263b',
selected: '#111432',
text: '#d6d6e7'
}
});
Using multiple branches
If you've setup the plugin to index multiple branches using the branches
plugin input, each configured branch has a dedicated index.
You'll also need to pass the information of which index you want to search in using the branch
parameter of the integration.
To get access to the currently building branch, you can configure your build tool to forward the HEAD
environment variable.
For instance, with webpack
's environment plugin configured to forward HEAD
, you would pass branch: process.env.HEAD
.
If you've configured your plugin to index only specific branches, you'll need to duplicate the logic here so that it picks the correct branch only when appropriate.
For instance, with branches = ['main', 'develop', 'feat/*']
, and using webpack's environment plugin to inject HEAD
, here's how the snippet could look like:
const currentBranch = process.env.HEAD;
let targetBranch = 'main';
if (currentBranch === 'develop' || currentBranch.startsWith('feat/')) {
targetBranch = currentBranch;
}
algoliasearchNetlify({
branch: targetBranch,
});
Theme
You can theme the input and the autocomplete by using the theme
property.
{
theme: {
mark: '#fff',
background: '#23263b',
selected: '#111432',
text: '#d6d6e7'
}
}
To go further you should take a look at the autocomplete.js documentation, or implement your own search with InstantSearch.js.
Development & Release
See CONTRIBUTING.md.