Installation
First, install the plugin:JavaScript
JavaScript
autocomplete-theme-classic
package.
If you don’t use a package manager, add the following script
elements to your HTML:
HTML
Examples
createRedirectUrlPlugin
function.
This will handle all Algolia rules that have redirect consequences without the need for any extra configuration.
Note that this plugin requires a source to be provided to autocomplete
with the sourceId
, templates
, getItemInputValue
, and getItems
parameters to work.
By default, redirect data is mapped to response.renderingContent?.redirect?.url
from Algolia’s response.
If you’ve defined redirects differently in your Rules,
you can change the mapping with the transformResponse
parameter.
Redirects are stored into the state’s context with the following form:
JavaScript
state.context.redirectUrlPlugin
will look like:
JavaScript
data
now has two items in the array, which correspond to each source.
Each entry has the sourceId
provided to maintain that mapping.
If you have two sources and one of them lacks a redirect,
then note that rather than be listed with an empty urls
array,
it will not be listed in state.context.redirectUrlPlugin
at all.
If you go a step further and have multiple sources configured with multiple queries,
and redirects are found for each, then state.context.redirectUrlPlugin
may now look like:
JavaScript
urls
array.
These correspond to two queries that were made in the source configuration.
The second parameter provides access to the following properties,
{ event, navigator, state }
. navigator
handles the actual redirection with the provided URL.
For more information, see Integrating keyboard navigation.
Redirects are triggered when users either submits the form or selects the redirect item from the drop-down menu.
By default, the first possible redirect is chosen
(the first source in state.context.redirectUrlPlugin.data
and its first URL),
but this behavior can be overridden with the onRedirect
parameter.
This callback function gets passed the values directly from state.context.redirectUrlPlugin.data
.
Parameters
The function that runs when triggering a redirect—either when submitting the form or when selecting an item.By default, the first redirect URL is used.
When a search might trigger a redirect from multiple sources,
you can use
onRedirect
to control which redirect URL to use.JavaScript
A callback function for mapping a response to a redirect URL.
- This is useful if you have a non-Algolia source.
- Can be used with Algolia sources as well if using custom user data instead of a redirect consequence.
- Gets passed the redirect data this plugin processes and stores in
state.context.redirectUrlPlugin.data
. - Overrides mapping the redirect URL provided from a rule with a redirect consequence.
JavaScript
Change the default template for rendering redirect items.