Skip to content

Commit 04a51ce

Browse files
committed
Revert "Simplify: remove proxy; keep monthly msUserId only"
This reverts commit 0a0080b.
1 parent 0a0080b commit 04a51ce

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

docusaurus/api/meili-search.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export default async function handler(req, res) {
2+
if (req.method !== 'POST') {
3+
res.status(405).json({ error: 'Method Not Allowed' });
4+
return;
5+
}
6+
7+
try {
8+
const { indexUid } = req.query || {};
9+
if (!indexUid) {
10+
res.status(400).json({ error: 'Missing indexUid' });
11+
return;
12+
}
13+
14+
const host = process.env.MEILI_HOST || process.env.NEXT_PUBLIC_MEILI_HOST || '';
15+
const apiKey = process.env.MEILI_API_KEY || process.env.NEXT_PUBLIC_MEILI_API_KEY || '';
16+
if (!host || !apiKey) {
17+
res.status(500).json({ error: 'Meilisearch host or API key not configured' });
18+
return;
19+
}
20+
21+
const userIdFromCookie = (() => {
22+
try {
23+
const cookie = req.headers['cookie'] || '';
24+
const match = cookie.match(/(?:^|;\s*)msUserId=([^;]+)/);
25+
return match ? decodeURIComponent(match[1]) : undefined;
26+
} catch {
27+
return undefined;
28+
}
29+
})();
30+
31+
const url = new URL(`/indexes/${encodeURIComponent(indexUid)}/search`, host);
32+
const meiliRes = await fetch(url.toString(), {
33+
method: 'POST',
34+
headers: {
35+
'Content-Type': 'application/json',
36+
'Authorization': `Bearer ${apiKey}`,
37+
'X-Meilisearch-Client': req.headers['x-meilisearch-client'] || 'StrapiDocs Proxy',
38+
...(userIdFromCookie ? { 'X-MS-USER-ID': userIdFromCookie } : {}),
39+
},
40+
body: JSON.stringify(req.body || {}),
41+
});
42+
43+
const data = await meiliRes.json();
44+
res.status(meiliRes.status).json(data);
45+
} catch (e) {
46+
res.status(500).json({ error: e.message || 'Proxy error' });
47+
}
48+
}
49+

docusaurus/src/theme/SearchBar/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ function SearchBarContent() {
3939
const uuid = (typeof crypto !== 'undefined' && crypto.randomUUID) ? crypto.randomUUID() : Math.random().toString(36).slice(2) + Date.now().toString(36);
4040
const value = JSON.stringify({ id: uuid, month: monthKey });
4141
window.localStorage.setItem(key, value);
42+
try {
43+
// Also set a same‑origin cookie so serverless proxy can read it
44+
const ttlDays = 45; // longer than a month for safety; we rotate monthly client‑side
45+
const maxAge = ttlDays * 24 * 60 * 60;
46+
document.cookie = `msUserId=${uuid}; Max-Age=${maxAge}; Path=/; SameSite=Lax` + (window.location.protocol === 'https:' ? '; Secure' : '');
47+
} catch {}
4248
return uuid;
4349
} catch (_) {
4450
return null;
@@ -159,8 +165,8 @@ function SearchBarContent() {
159165
import('meilisearch-docsearch/css')
160166
]).then(([{ docsearch }]) => {
161167
const meiliHost = siteConfig.customFields.meilisearch.host;
162-
// Always call Meili directly (no proxy) since env config isn’t accessible
163-
const useProxy = false;
168+
// Use proxy on non-localhost to avoid CORS limits and inject server-side header
169+
const useProxy = typeof window !== 'undefined' && window.location && window.location.hostname !== 'localhost';
164170
const baseOptions = {
165171
container: searchButtonRef.current,
166172
host: useProxy ? `${window.location.origin}/api` : meiliHost,

0 commit comments

Comments
 (0)