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 enhances existing search inputs 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@0/dist/algoliasearchNetlify.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@algolia/algoliasearch-netlify-frontend@0/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>',
autocomplete: {
inputSelector: 'input[type=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>',
analytics: true,
autocomplete: {
hitsPerPage: 5,
inputSelector: 'input[type=search]',
},
color: '#3c4fe0',
debug: false,
silenceWarnings: false,
poweredBy: true,
});
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,
});
Scripts
yarn dev
: run dev environmentyarn release
: build & publish the library
Development
From this folder:
yarn dev
Or from the root of the repository:
yarn dev:frontend
This runs a webpack-dev-server
on port 9100.
Meant to be used in conjunction with the test website.