|
| 1 | +import 'dotenv/config' |
| 2 | +import fs from 'fs' |
| 3 | +import md5 from 'md5' |
| 4 | + |
1 | 5 | import { getMeta } from '@/data/meta' |
2 | 6 | import { currentVersion } from '@/data/versions' |
3 | | -import 'dotenv/config' |
4 | 7 |
|
5 | 8 | export default function handler(req, res) { |
6 | | - // Load Search Function |
7 | | - import('@/markdoc/search.mjs').then(({ search }) => { |
| 9 | + // Setup Cache Key |
| 10 | + const hashKey = md5(`hash-${req.query.query}-${req.query.limit || 100}-${req.query.offset || 0}`) |
| 11 | + const cacheFile = process.cwd() + `/.cache/${hashKey}.json` |
| 12 | + |
| 13 | + if (fs.existsSync(cacheFile)) { |
8 | 14 | try { |
9 | | - // Perform Search |
10 | | - const baseURL = process.env.NEXT_PUBLIC_SITE_URL || 'https://sfccdocs.com' |
11 | | - const results = search(req.query.query, { |
12 | | - limit: req.query.limit ? parseInt(req.query.limit, 10) : 100, |
13 | | - offset: req.query.offset ? parseInt(req.query.offset, 10) : 0, |
14 | | - }) |
15 | | - |
16 | | - // Loop through results |
17 | | - results.map((result, index) => { |
18 | | - // Add additional properties |
19 | | - const parts = result.url ? result.url.split('#') : null |
20 | | - const baseUri = parts[0] |
21 | | - const anchor = parts.length > 1 ? `#${parts[1]}` : '' |
22 | | - const deprecated = baseUri.startsWith('/deprecated/') |
23 | | - const meta = getMeta(baseUri, deprecated) |
24 | | - |
25 | | - result.deprecated = deprecated |
26 | | - result.title = meta.title |
27 | | - result.description = meta.description |
28 | | - result.keywords = meta.nav?.alt ? meta.nav.alt.split(' › ') : null |
29 | | - result.url = `${baseURL}${result.url}` |
30 | | - result.embed = `${baseURL}${baseUri}?embed=true${anchor}` |
31 | | - result.content = result.content ? result.content.split('. ')[0] : null |
32 | | - |
33 | | - // Remove unneeded properties |
34 | | - delete result.score |
35 | | - |
36 | | - if (result.pageTitle) { |
37 | | - delete result.pageTitle |
38 | | - } |
39 | | - |
40 | | - // Sort properties alphabetically for easier readability |
41 | | - results[index] = Object.keys(result) |
42 | | - .sort() |
43 | | - .reduce((accumulator, key) => { |
44 | | - accumulator[key] = result[key] |
45 | | - |
46 | | - return accumulator |
47 | | - }, {}) |
48 | | - }) |
49 | | - |
50 | | - // Return results |
51 | | - res.status(200).json({ total: results.length, results, version: currentVersion }) |
| 15 | + const cachedJson = fs.readFileSync(cacheFile, 'utf8') |
| 16 | + res.setHeader('Cache-Control', 's-maxage=86400') |
| 17 | + res.status(200).json(JSON.parse(cachedJson)) |
52 | 18 | } catch (error) { |
53 | 19 | // Return error |
54 | 20 | res.status(400).json({ error: error.message, trace: error.stack }) |
55 | 21 | } |
56 | | - }) |
| 22 | + } else { |
| 23 | + // Load Search Function |
| 24 | + import('@/markdoc/search.mjs').then(({ search }) => { |
| 25 | + try { |
| 26 | + // Perform Search |
| 27 | + const baseURL = process.env.NEXT_PUBLIC_SITE_URL || 'https://sfccdocs.com' |
| 28 | + const results = search(req.query.query, { |
| 29 | + limit: req.query.limit ? parseInt(req.query.limit, 10) : 100, |
| 30 | + offset: req.query.offset ? parseInt(req.query.offset, 10) : 0, |
| 31 | + }) |
| 32 | + |
| 33 | + // Loop through results |
| 34 | + results.map((result, index) => { |
| 35 | + // Add additional properties |
| 36 | + const parts = result.url ? result.url.split('#') : null |
| 37 | + const baseUri = parts[0] |
| 38 | + const anchor = parts.length > 1 ? `#${parts[1]}` : '' |
| 39 | + const deprecated = baseUri.startsWith('/deprecated/') |
| 40 | + const meta = getMeta(baseUri, deprecated) |
| 41 | + |
| 42 | + result.deprecated = deprecated |
| 43 | + result.title = meta.title |
| 44 | + result.description = meta.description |
| 45 | + result.keywords = meta.nav?.alt ? meta.nav.alt.split(' › ') : null |
| 46 | + result.url = `${baseURL}${result.url}` |
| 47 | + result.embed = `${baseURL}${baseUri}?embed=true${anchor}` |
| 48 | + result.content = result.content ? result.content.split('. ')[0] : null |
| 49 | + |
| 50 | + // Remove unneeded properties |
| 51 | + delete result.score |
| 52 | + |
| 53 | + if (result.pageTitle) { |
| 54 | + delete result.pageTitle |
| 55 | + } |
| 56 | + |
| 57 | + // Sort properties alphabetically for easier readability |
| 58 | + results[index] = Object.keys(result) |
| 59 | + .sort() |
| 60 | + .reduce((accumulator, key) => { |
| 61 | + accumulator[key] = result[key] |
| 62 | + |
| 63 | + return accumulator |
| 64 | + }, {}) |
| 65 | + }) |
| 66 | + |
| 67 | + const data = { total: results.length, results, version: currentVersion } |
| 68 | + fs.writeFileSync(cacheFile, JSON.stringify(data)) |
| 69 | + |
| 70 | + // Return results |
| 71 | + res.setHeader('Cache-Control', 's-maxage=86400') |
| 72 | + res.status(200).json(data) |
| 73 | + } catch (error) { |
| 74 | + // Return error |
| 75 | + res.status(400).json({ error: error.message, trace: error.stack }) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
57 | 79 | } |
0 commit comments