Skip to content

Commit

Permalink
Merge pull request #176 from vilhelmjosander/feature/add-fetch-transp…
Browse files Browse the repository at this point in the history
…orter

Add option to enable requester-fetch instead of requester-node-http for edge-environments like Cloudflare Workers
  • Loading branch information
Baroshem authored Oct 27, 2023
2 parents e5bd791 + a9b9d24 commit d5a84ac
Show file tree
Hide file tree
Showing 7 changed files with 2,403 additions and 3,050 deletions.
8 changes: 7 additions & 1 deletion docs/content/1.getting-started/2.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Defaults:
globalIndex: '',
lite: true,
cache: false,
instantSearch: true,
instantSearch: true,
useFetch: false,
crawler: {
apiKey: '<YOUR_API_KEY>',
indexName: '<YOUR_INDEX_NAME>',
Expand Down Expand Up @@ -94,6 +95,11 @@ By default set to false. The client caches requests to Algolia and their respons
More information available in the official [Algolia docs](https://www.algolia.com/doc/api-client/getting-started/customize/javascript/?client=javascript#caching-requests-and-responses).
::

### `useFetch`

By default set to false. If set to true, it will use @algolia/requester-fetch instead of @algolia/requester-node-http. This enables SSR-support for using this module in V8-based environments like Vercel Edge, Cloudflare Workers etc.


### `instantSearch`

By default set to false. Indicates whether to install the official [vue-instantsearch](https://github.com/algolia/vue-instantsearch) plugin. This option can also be an object (see below).
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"lint": "eslint --ext .js,.ts,.vue"
},
"dependencies": {
"@algolia/requester-fetch": "^4.20.0",
"@algolia/cache-in-memory": "^4.14.2",
"@algolia/recommend": "^4.12.2",
"@nuxt/kit": "^3.7.0",
Expand Down
7 changes: 5 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default defineNuxtModule<ModuleOptions>({
cache: false,
instantSearch: false,
docSearch: {},
useFetch: false,
crawler: {
apiKey: '',
indexName: '',
Expand Down Expand Up @@ -113,7 +114,8 @@ export default defineNuxtModule<ModuleOptions>({
instantSearch: options.instantSearch,
docSearch: options.docSearch,
recommend: options.recommend,
globalIndex: options.globalIndex
globalIndex: options.globalIndex,
useFetch: options.useFetch
})
}
// Nuxt 3
Expand All @@ -128,7 +130,8 @@ export default defineNuxtModule<ModuleOptions>({
instantSearch: options.instantSearch,
docSearch: options.docSearch,
recommend: options.recommend,
globalIndex: options.globalIndex
globalIndex: options.globalIndex,
useFetch: options.useFetch
})

if (options.instantSearch) {
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/composables/useAlgoliaSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export function useAlgoliaSearch (indexName?: string) {
const search = async ({ query, requestOptions }: SearchParams) => {
if (process.server) {
const nuxtApp = useNuxtApp()
nuxtApp.$algolia.transporter.requester = (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()
if(config.public.algolia.useFetch) {
nuxtApp.$algolia.transporter.requester = (await import("@algolia/requester-fetch").then((lib) => lib.default || lib)).createFetchRequester();
} else {
nuxtApp.$algolia.transporter.requester = (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()
}
}

const searchResult = await algoliaIndex.search(query, requestOptions)
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/composables/useAsyncAlgoliaSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export async function useAsyncAlgoliaSearch ({ query, requestOptions, indexName,
const result = await useAsyncData(`${index}-async-search-result-${key ?? ''}`, async () => {
if (process.server) {
const nuxtApp = useNuxtApp()
nuxtApp.$algolia.transporter.requester = (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()
if(config.public.algolia.useFetch) {
nuxtApp.$algolia.transporter.requester = (await import("@algolia/requester-fetch").then((lib) => lib.default || lib)).createFetchRequester();
} else {
nuxtApp.$algolia.transporter.requester = (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()
}
}
return await algoliaIndex.search(query, requestOptions)
})
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,4 +1002,5 @@ export interface ModuleBaseOptions {
recommend?: boolean;
docSearch?: Partial<DocSearchOptions>;
indexer?: Indexer;
useFetch?: boolean;
}
Loading

0 comments on commit d5a84ac

Please sign in to comment.