Skip to content

Commit 2ea5a0c

Browse files
0;276;0cSetup kapa.ai docs search (#599)
Should be much better than what algolia provides, I hope
1 parent e775ec4 commit 2ea5a0c

15 files changed

Lines changed: 118 additions & 647 deletions

.env.development

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/publish.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,3 @@ jobs:
6161
build-args: |
6262
NEXT_PUBLIC_ROOT_URL="https://docs.plural.sh"
6363
NEXT_PUBLIC_GA_ID=${{ vars.NEXT_PUBLIC_GA_ID }}
64-
NEXT_PUBLIC_ALGOLIA_APP_ID=${{ vars.NEXT_PUBLIC_ALGOLIA_APP_ID }}
65-
NEXT_PUBLIC_ALGOLIA_APP_ID_KEY=${{ vars.NEXT_PUBLIC_ALGOLIA_APP_ID_KEY }}
66-
NEXT_PUBLIC_ALGOLIA_INDEX_NAME=${{ vars.NEXT_PUBLIC_ALGOLIA_INDEX_NAME }}

Dockerfile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ COPY . .
1313

1414
ARG NEXT_PUBLIC_ROOT_URL
1515
ARG NEXT_PUBLIC_GA_ID
16-
ARG NEXT_PUBLIC_ALGOLIA_APP_ID
17-
ARG NEXT_PUBLIC_ALGOLIA_APP_ID_KEY
18-
ARG NEXT_PUBLIC_ALGOLIA_INDEX_NAME
1916

2017
ENV NEXT_PUBLIC_ROOT_URL=$NEXT_PUBLIC_ROOT_URL
2118
ENV NEXT_PUBLIC_GA_ID=$NEXT_PUBLIC_GA_ID
22-
ENV NEXT_PUBLIC_ALGOLIA_APP_ID=$NEXT_PUBLIC_ALGOLIA_APP_ID
23-
ENV NEXT_PUBLIC_ALGOLIA_APP_ID_KEY=$NEXT_PUBLIC_ALGOLIA_APP_ID_KEY
24-
ENV NEXT_PUBLIC_ALGOLIA_INDEX_NAME=$NEXT_PUBLIC_ALGOLIA_INDEX_NAME
2519

2620
RUN yarn build
2721

docsearch.json

Lines changed: 0 additions & 48 deletions
This file was deleted.

middleware.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
/** Kapa widget backend; browser/iframe may send this Origin when requesting docs assets. */
5+
const KAPA_WIDGET_PROXY = 'https://kapa-widget-proxy-la7dkmplpq-uc.a.run.app'
6+
7+
function corsAllowedOrigin(origin: string | null): string | null {
8+
if (!origin) {
9+
return null
10+
}
11+
if (origin === KAPA_WIDGET_PROXY) {
12+
return origin
13+
}
14+
if (origin === 'https://docs.plural.sh') {
15+
return origin
16+
}
17+
if (/^https:\/\/docs-pr-\d+\.plural\.sh$/.test(origin)) {
18+
return origin
19+
}
20+
if (origin.startsWith('http://localhost:')) {
21+
return origin
22+
}
23+
24+
return null
25+
}
26+
27+
export function middleware(request: NextRequest) {
28+
const origin = request.headers.get('origin')
29+
const allowOrigin = corsAllowedOrigin(origin)
30+
31+
if (request.method === 'OPTIONS' && allowOrigin) {
32+
return new NextResponse(null, {
33+
status: 204,
34+
headers: {
35+
'Access-Control-Allow-Origin': allowOrigin,
36+
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
37+
'Access-Control-Allow-Headers':
38+
request.headers.get('access-control-request-headers') || '*',
39+
'Access-Control-Max-Age': '86400',
40+
Vary: 'Origin',
41+
},
42+
})
43+
}
44+
45+
const res = NextResponse.next()
46+
47+
if (allowOrigin) {
48+
res.headers.set('Access-Control-Allow-Origin', allowOrigin)
49+
res.headers.set('Vary', 'Origin')
50+
}
51+
52+
return res
53+
}
54+
55+
export const config = {
56+
matcher: ['/((?!_next/static|_next/image).*)'],
57+
}

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"dependencies": {
2525
"@apidevtools/swagger-parser": "^12.1.0",
2626
"@apollo/client": "3.7.15",
27-
"@docsearch/react": "3.5.1",
2827
"@emotion/react": "11.11.1",
2928
"@emotion/styled": "11.11.0",
3029
"@graphql-codegen/add": "5.0.0",
@@ -43,7 +42,6 @@
4342
"@react-stately/list": "3.8.1",
4443
"@react-stately/select": "3.5.1",
4544
"@react-stately/selection": "3.19.0",
46-
"algoliasearch": "4.17.2",
4745
"chroma-js": "2.4.2",
4846
"classnames": "2.3.2",
4947
"deep-freeze": "0.0.1",

pages/_app.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import styled, { ThemeProvider as StyledThemeProvider } from 'styled-components'
3030
import { SWRConfig } from 'swr'
3131

3232
import { BreakpointProvider } from '@src/components/Breakpoints'
33-
import DocSearchStyles from '@src/components/DocSearchStyles'
3433
import ExternalScripts from '@src/components/ExternalScripts'
3534
import { FullNav } from '@src/components/FullNav'
3635
import {
@@ -155,7 +154,6 @@ function App({ Component, pageProps = {}, swrConfig }: MyAppProps) {
155154
<CssBaseline />
156155
<PluralGlobalStyle />
157156
<GlobalStyles />
158-
<DocSearchStyles />
159157
<PagePropsContext.Provider value={pageProps}>
160158
<HtmlHead {...headProps} />
161159
<PageHeader />

pages/_document.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class MyDocument extends Document {
3535
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap"
3636
rel="stylesheet"
3737
/>
38-
<link
39-
rel="stylesheet"
40-
href="https://cdn.jsdelivr.net/npm/@docsearch/css@3"
41-
/>
4238
<script
4339
id="Cookiebot"
4440
src="https://consent.cookiebot.com/uc.js"

src/components/DocSearchStyles.tsx

Lines changed: 0 additions & 143 deletions
This file was deleted.

src/components/ExternalScripts.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,63 @@ function HubSpot() {
6060
)
6161
}
6262

63+
// function safeKapaUnmount() {
64+
// const kapa = window.Kapa
65+
66+
// if (typeof kapa !== 'function') {
67+
// return
68+
// }
69+
70+
// try {
71+
// kapa('unmount')
72+
// } catch {
73+
// // Widget may not have mounted yet
74+
// }
75+
// }
76+
77+
function KapaWidget() {
78+
// useEffect(() => {
79+
// const onCookiePrefChange = () => {
80+
// if (!window.Cookiebot?.consent?.statistics) {
81+
// safeKapaUnmount()
82+
// }
83+
// }
84+
85+
// window.addEventListener('CookiebotOnAccept', onCookiePrefChange)
86+
// window.addEventListener('CookiebotOnDecline', onCookiePrefChange)
87+
88+
// return () => {
89+
// window.removeEventListener('CookiebotOnAccept', onCookiePrefChange)
90+
// window.removeEventListener('CookiebotOnDecline', onCookiePrefChange)
91+
// }
92+
// }, [])
93+
94+
return (
95+
<Script
96+
// id="kapa-widget-script"
97+
// type="text/plain"
98+
// data-cookieconsent="statistics"
99+
strategy="afterInteractive"
100+
async
101+
defer
102+
src="https://widget.kapa.ai/kapa-widget.bundle.js"
103+
data-website-id="8288b978-8aca-4409-9cbd-de12c446cf07"
104+
data-project-name="Plural"
105+
data-view-mode="sidebar"
106+
data-modal-open-on-command-k="true"
107+
data-search-mode-enabled="true"
108+
data-mcp-enabled="true"
109+
data-project-color="#5D63F4"
110+
data-project-logo="https://docs.plural.sh/favicon-192.png"
111+
/>
112+
)
113+
}
114+
63115
export default function ExternalScripts() {
64-
return <HubSpot />
116+
return (
117+
<>
118+
<HubSpot />
119+
<KapaWidget />
120+
</>
121+
)
65122
}

0 commit comments

Comments
 (0)