Skip to content

nuxt3 #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

nuxt3 #430

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vue-instantsearch/nuxt3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
*.log*
.nuxt
.nitro
.cache
.output
.env
dist
47 changes: 47 additions & 0 deletions vue-instantsearch/nuxt3/components/search.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<ais-instant-search-ssr>
<ais-index
index-name="instant_search_demo_query_suggestions"
index-id="querySuggestions"
>
<ais-search-box />
<ais-configure :hits-per-page.camel="5" />
<ais-hits>
<template #item="{ item }">
<ais-highlight attribute="query" :hit="item" />
</template>
</ais-hits>
<ais-pagination />
</ais-index>
<ais-search-box />
<ais-stats />
<ais-index index-id="refinement" index-name="instant_search">
<ais-refinement-list attribute="brand" />
</ais-index>
<ais-hits>
<template #item="{ item }">
<p>
<ais-highlight attribute="name" :hit="item" />
</p>
<p>
<ais-highlight attribute="brand" :hit="item" />
</p>
</template>
</ais-hits>
<ais-pagination />
</ais-instant-search-ssr>
</template>

<script setup>
import {
AisInstantSearchSsr,
AisIndex,
AisConfigure,
AisRefinementList,
AisHits,
AisHighlight,
AisSearchBox,
AisStats,
AisPagination,
} from 'vue-instantsearch/vue3/es';
</script>
15 changes: 15 additions & 0 deletions vue-instantsearch/nuxt3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"dependencies": {
"algoliasearch": "4.14.3",
"nuxt": "3.0.0",
"vue-instantsearch": "4.7.1"
}
}
11 changes: 11 additions & 0 deletions vue-instantsearch/nuxt3/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<h2>
Go to the search page: <nuxt-link to="/search"> "/search" </nuxt-link>
</h2>
</template>

<style>
h2 {
text-align: center;
}
</style>
70 changes: 70 additions & 0 deletions vue-instantsearch/nuxt3/pages/search.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<template>
<Search />
</template>

<script setup>
import { h, onServerPrefetch, onMounted, provide } from 'vue';
import { createServerRootMixin } from 'vue-instantsearch/vue3/es';
import algoliasearch from 'algoliasearch/lite';
import { renderToString } from '@vue/server-renderer';
import Search from '../components/search.vue';
import { useNuxtApp } from '#app';

const mixin = createServerRootMixin({
searchClient: algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76'),
indexName: 'instant_search',
initialUiState: {
instant_search: {
query: 'iphone',
page: 3,
},
refinement: {
refinementList: {
brand: ['Apple'],
},
},
querySuggestions: {
query: 'k',
page: 2,
configure: {
hitsPerPage: 5,
},
},
},
});

const app = useNuxtApp();

const { instantsearch } = mixin.data();

provide('$_ais_ssrInstantSearchInstance', instantsearch);

onServerPrefetch(async () => {
console.log('pre the load', this)
app.ssrContext.payload.algolia = await instantsearch.findResultsState({
// todo: what is the component? i don't see how to access the instance we currently are in
component: h(Search),
Comment on lines +45 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't the component instance, it doesn't have .render as expected, so the temporary render done by findResultsState doesn't include the widgets

// this relies on a patch in createServerRootMixin, as the component would be a child likely
instantsearch,
renderToString,
});
console.log('past the load', app.ssrContext.payload.algolia)
});

onMounted(async () => {
console.log('mounting', app.ssrContext.payload.algolia)
instantsearch.hydrate(app.ssrContext.payload.algolia);
});
</script>

<style>
.ais-Hits-list {
text-align: left;
}
.ais-Hits-list:empty {
margin: 0;
}
.ais-InstantSearch {
margin: 1em;
}
</style>
Loading