Skip to content

Commit

Permalink
feat: introduce routing
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-giraudet committed Sep 10, 2023
1 parent 867d105 commit 2713ff6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm install svelte-algolia-instantsearch algoliasearch
const searchClient = algoliasearch("<YOUR_API_KEY>", "<YOUR_SEARCH_KEY>");
</script>
<InstantSearch indexName="<YOUR_INDEX_NAME>" {searchClient}>
<InstantSearch indexName="<YOUR_INDEX_NAME>" routing {searchClient}>
<SearchBox />
<Hits let:hit>
Expand Down Expand Up @@ -60,7 +60,7 @@ import { getServerState } from "svelte-algolia-instantsearch";

import Page from "./+page.svelte";

export const load = () => getServerState(Page);
export const load = ({ url }) => getServerState(Page, url);
```

Now you can check in your network tab that the page containing hits and facets is fully rendered on the server 😁
Expand Down
20 changes: 20 additions & 0 deletions src/lib/InstantSearch.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import type { SearchClient } from "algoliasearch/lite";
import InstantSearch from "instantsearch.js/es/lib/InstantSearch";
import { history } from "instantsearch.js/es/lib/routers";
import { setInstantSearchContext } from "./instantSearchContext";
import { tick } from "svelte";
import { getServerContext } from "./getServerState";
Expand All @@ -11,6 +12,7 @@
export let indexName: string;
export let searchClient: SearchClient;
export let stalledSearchDelay: number | undefined = undefined;
export let routing = false;
let search: InstantSearch;
Expand All @@ -21,6 +23,24 @@
indexName,
searchClient,
stalledSearchDelay,
routing: routing && {
router: history({
getLocation() {
if (typeof window === "undefined") {
if (serverContext) {
return new URL(serverContext.serverUrl) as unknown as Location;
} else {
let serverUrl;
page.subscribe(({ url }) => {
serverUrl = url;
});
return new URL(serverUrl!) as unknown as Location;
}
}
return window.location;
},
}),
},
});
setInstantSearchContext(search);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/getServerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { getContext } from "svelte";
const serverContext = Symbol("InstantSearch:serverContext");

export const getServerContext = () =>
getContext<{ notifyServer: (search: InstantSearch) => void }>(serverContext);
getContext<{ notifyServer: (search: InstantSearch) => void; serverUrl: URL }>(serverContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getServerState(component: any): Promise<Record<string, any>> {
export function getServerState(component: any, serverUrl: URL): Promise<Record<string, any>> {
return new Promise((resolve) => {
const notifyServer = async (search: InstantSearch) => {
await waitForResults(search);
const results = getInitialResults(search.mainIndex);
search.dispose();
resolve(results);
};
component.render({}, { context: new Map([[serverContext, { notifyServer }]]) });
component.render({}, { context: new Map([[serverContext, { notifyServer, serverUrl }]]) });
});
}
3 changes: 2 additions & 1 deletion src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Page from "./+page.svelte";
import { getServerState } from "$lib";
import type { PageServerLoad } from "./$types";

export const load = () => getServerState(Page);
export const load: PageServerLoad = ({ url }) => getServerState(Page, url);
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<title>svelte-algolia-instantsearch | Demo</title>
</svelte:head>

<InstantSearch indexName="instant_search" {searchClient}>
<InstantSearch indexName="instant_search" routing {searchClient}>
<div class="Container">
<div>
<Panel header="Brands"
Expand Down

1 comment on commit 2713ff6

@vercel
Copy link

@vercel vercel bot commented on 2713ff6 Sep 10, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.