Skip to content

Commit e66fa6c

Browse files
committed
Initial commit
0 parents  commit e66fa6c

File tree

13 files changed

+3912
-0
lines changed

13 files changed

+3912
-0
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# build output
2+
dist/
3+
# generated types
4+
.astro/
5+
6+
# dependencies
7+
node_modules/
8+
9+
# logs
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
pnpm-debug.log*
14+
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store

.vscode/extensions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recommendations": ["astro-build.astro-vscode"],
3+
"unwantedRecommendations": []
4+
}

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"command": "./node_modules/.bin/astro dev",
6+
"name": "Development server",
7+
"request": "launch",
8+
"type": "node-terminal"
9+
}
10+
]
11+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Demonstrates the issue with server rendering and Algolia Autocomplete Astro
2+
3+
```
4+
yarn dev # start dev server with all 3 components rendering
5+
yarn build # will error if the server only search input is present
6+
7+
```
8+
To get yarn build to work slash out the `Server render` component
9+
10+
* `Server render` component renders on the server and mounts with `client:idle`
11+
causing an issue
12+
* `Hot replace` has a fake input which renders on the server and then mounts the
13+
Algolia autocomplete input over the top
14+
* `Client render` only renders on the client and shows CLS issues as there is no
15+
server rendered input

astro.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'astro/config';
2+
import react from "@astrojs/react";
3+
4+
// https://astro.build/config
5+
export default defineConfig({
6+
integrations: [react()],
7+
});

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "astro-algolia",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "astro dev",
7+
"start": "astro dev",
8+
"build": "astro build",
9+
"preview": "astro preview",
10+
"astro": "astro"
11+
},
12+
"dependencies": {
13+
"@algolia/autocomplete-core": "^1.11.1",
14+
"@algolia/autocomplete-preset-algolia": "^1.11.1",
15+
"@astrojs/react": "^3.0.3",
16+
"@types/react": "^18.2.29",
17+
"@types/react-dom": "^18.2.14",
18+
"algoliasearch": "^4.20.0",
19+
"astro": "^3.3.2",
20+
"react": "^18.2.0",
21+
"react-dom": "^18.2.0"
22+
}
23+
}

public/favicon.svg

Lines changed: 9 additions & 0 deletions
Loading

src/components/ClientSearch.astro

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
import Search from './Search.jsx'
3+
---
4+
5+
<div class="wrapper">
6+
<h1>Server render fake input</h1>
7+
<div className="aa-Autocomplete">
8+
<!--
9+
this is a fake input rendered on the server which has the appearance of
10+
being the agolia input before it has been initialized.
11+
-->
12+
<input className="aa-Input" style={{ background: 'red' }} />
13+
</div>
14+
<div class="react">
15+
<Search title="Server render fake input" client:only />
16+
</div>
17+
</div>
18+
<style>
19+
:global(h1) { margin-top: 0}
20+
.wrapper {
21+
position: relative;
22+
}
23+
.react {
24+
position: absolute;
25+
top: 0;
26+
left: 0;
27+
}
28+
</style>

src/components/Search.jsx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { useState, useMemo } from "react";
2+
import algoliasearch from "algoliasearch/lite";
3+
import { createAutocomplete } from "@algolia/autocomplete-core";
4+
import { getAlgoliaResults } from "@algolia/autocomplete-preset-algolia";
5+
6+
export default function ({ title }) {
7+
const searchClient = algoliasearch(
8+
"latency",
9+
"6be0576ff61c053d5f9a3225e2a90f76"
10+
);
11+
12+
// (1) Create a React state.
13+
const [autocompleteState, setAutocompleteState] = useState({});
14+
const autocomplete = useMemo(
15+
() =>
16+
createAutocomplete({
17+
onStateChange({ state }) {
18+
// (2) Synchronize the Autocomplete state with the React state.
19+
setAutocompleteState(state);
20+
},
21+
id: 'Search thing',
22+
getSources() {
23+
return [
24+
// (3) Use an Algolia index source.
25+
{
26+
sourceId: "products",
27+
getItemInputValue({ item }) {
28+
return item.query;
29+
},
30+
getItems({ query }) {
31+
return getAlgoliaResults({
32+
searchClient,
33+
queries: [
34+
{
35+
indexName: "instant_search",
36+
query,
37+
params: {
38+
hitsPerPage: 4,
39+
highlightPreTag: "<mark>",
40+
highlightPostTag: "</mark>",
41+
},
42+
},
43+
],
44+
});
45+
},
46+
getItemUrl({ item }) {
47+
return item.url;
48+
},
49+
},
50+
];
51+
},
52+
}),
53+
[]
54+
);
55+
56+
return (
57+
<div>
58+
<h1>{title}</h1>
59+
<div className="aa-Autocomplete" {...autocomplete.getRootProps({})}>
60+
<input className="aa-Input" {...autocomplete.getInputProps({})} />
61+
<div className="aa-Panel" {...autocomplete.getPanelProps({})}>
62+
{autocompleteState.isOpen &&
63+
autocompleteState.collections.map((collection, index) => {
64+
const { source, items } = collection;
65+
return (
66+
<div key={`source-${index}`} className="aa-Source">
67+
{items.length > 0 && (
68+
<ul className="aa-List" {...autocomplete.getListProps()}>
69+
{items.map((item) => (
70+
<li
71+
key={item.objectID}
72+
className="aa-Item"
73+
{...autocomplete.getItemProps({
74+
item,
75+
source,
76+
})}
77+
>
78+
{item.name}
79+
</li>
80+
))}
81+
</ul>
82+
)}
83+
</div>
84+
);
85+
})}
86+
</div>
87+
</div>
88+
</div>
89+
);
90+
}

src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="astro/client" />

0 commit comments

Comments
 (0)