Skip to content

Commit

Permalink
refactor: only read index on server start
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobcolyvan committed Aug 19, 2024
1 parent 38c6b65 commit f1f36d5
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions api/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const startServer = async () => {
app.disable('x-powered-by');
await AppService(app);

const indexPath = path.join(app.locals.paths.dist, 'index.html');
const indexHTML = fs.readFileSync(indexPath, 'utf8');

app.get('/health', (_req, res) => res.status(200).send('OK'));

// Middleware
Expand Down Expand Up @@ -106,19 +109,10 @@ const startServer = async () => {
app.use('/api/tags', routes.tags);

app.use((req, res) => {
// Check for preferred language in cookies, else check accept-language header
const preferredLanguage =
req.cookies.langcode || req.headers['accept-language']?.split(',')[0] || 'en-US';
const indexPath = path.join(app.locals.paths.dist, 'index.html');

fs.readFile(indexPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading index file:', err);
return res.status(500).send('Internal Server Error');
}
const updatedIndexHtml = data.replace(/lang="en-US"/g, `lang="${preferredLanguage}"`);
res.send(updatedIndexHtml);
});
// Replace lang attribute in index.html with lang from cookies or accept-language header
const lang = req.cookies.lang || req.headers['accept-language']?.split(',')[0] || 'en-US';
const updatedIndexHtml = indexHTML.replace(/lang="en-US"/g, `lang="${lang}"`);
res.send(updatedIndexHtml);
});

app.listen(port, host, () => {
Expand Down

0 comments on commit f1f36d5

Please sign in to comment.