-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (59 loc) · 1.98 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Polyfill Node with `Intl` that has data for all locales.
// See: https://formatjs.io/guides/runtime-environments/#server
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
const { readFileSync } = require('fs');
const { basename } = require('path');
const accepts = require('accepts');
const glob = require('glob');
const express = require('express');
const path = require('path');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dir: './src', dev });
const handle = app.getRequestHandler();
const PORT = process.env.PORT || 3000;
const supportedLanguages = glob.sync('./src/lang/*.json').map(f => basename(f, '.json'));
const localeDataCache = new Map();
const getLocaleDataScript = locale => {
const lang = locale.split('-')[0];
if (!localeDataCache.has(lang)) {
const localeDataFile = require.resolve(`react-intl/locale-data/${lang}`);
const localeDataScript = readFileSync(localeDataFile, 'utf8');
localeDataCache.set(lang, localeDataScript);
}
return localeDataCache.get(lang);
};
// eslint-disable-next-line
const getMessages = (locale = 'en') => require(`./src/lang/${locale}.json`);
const setLocale = req => {
const accept = accepts(req);
const locale = accept.language(supportedLanguages);
req.locale = locale;
req.localeDataScript = getLocaleDataScript(locale);
req.messages = getMessages(locale);
return req;
};
app
.prepare()
.then(_ => {
const server = express();
server.get('*', (req, res) => {
setLocale(req);
// todo: burada neler oluyor incele
if (req.url === '/sw.js' || req.url.startsWith('/precache-manifest')) {
app.serveStatic(req, res, path.join(__dirname, '.next', req.url));
} else {
handle(req, res);
}
});
server.listen(PORT, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${PORT}`);
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});