Skip to content

Commit 3047ea2

Browse files
committed
Route search to same-origin API and pass X-MS-USER-ID header
1 parent e595f14 commit 3047ea2

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// Read Meilisearch project URL and key. Fallback to values used in Docusaurus config if envs are not set.
15+
const host = process.env.MEILI_HOST || process.env.NEXT_PUBLIC_MEILI_HOST || 'https://ms-47f23e4f6fb9-30446.fra.meilisearch.io';
16+
const apiKey = process.env.MEILI_API_KEY || process.env.NEXT_PUBLIC_MEILI_API_KEY || '45326fd7e6278ec3fc83af7a5c20a2ab4261f8591bd186adf8bf8f962581622b';
17+
18+
// Forward X-MS-USER-ID if present from the browser request (same-origin; no CORS issue)
19+
const userId = req.headers['x-ms-user-id'];
20+
21+
const url = new URL(`/indexes/${encodeURIComponent(indexUid)}/search`, host);
22+
const upstream = await fetch(url.toString(), {
23+
method: 'POST',
24+
headers: {
25+
'Content-Type': 'application/json',
26+
'Authorization': `Bearer ${apiKey}`,
27+
'X-Meilisearch-Client': req.headers['x-meilisearch-client'] || 'StrapiDocs Proxy',
28+
...(userId ? { 'X-MS-USER-ID': String(userId) } : {}),
29+
},
30+
body: JSON.stringify(req.body || {}),
31+
});
32+
33+
const body = await upstream.text();
34+
// Pass through status and JSON body
35+
res.status(upstream.status);
36+
try {
37+
res.setHeader('Content-Type', 'application/json');
38+
res.send(body);
39+
} catch {
40+
res.json({ error: 'Invalid upstream response' });
41+
}
42+
} catch (e) {
43+
res.status(500).json({ error: e.message || 'Proxy error' });
44+
}
45+
}
46+

docusaurus/src/theme/SearchBar/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ function SearchBarContent() {
160160
]).then(([{ docsearch }]) => {
161161
const baseOptions = {
162162
container: searchButtonRef.current,
163-
host: siteConfig.customFields.meilisearch.host,
163+
// Route through same-origin API to add headers server-side without CORS
164+
host: `${window.location.origin}/api`,
164165
apiKey: siteConfig.customFields.meilisearch.apiKey,
165166
indexUid: siteConfig.customFields.meilisearch.indexUid,
166167

@@ -244,9 +245,8 @@ function SearchBarContent() {
244245
},
245246
};
246247

247-
// Prefer official header wiring if library supports it
248+
// Send X-MS-USER-ID to same-origin API; no CORS preflight restrictions
248249
if (userId) {
249-
// Some versions accept requestConfig.headers, others accept headers; set both safely
250250
baseOptions.requestConfig = {
251251
...(baseOptions.requestConfig || {}),
252252
headers: { 'X-MS-USER-ID': userId }

0 commit comments

Comments
 (0)