-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.server.js
44 lines (42 loc) · 1.37 KB
/
next.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
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const path = require('path')
const { join } = require('path')
const port = parseInt(process.env.PORT, 10) || 8000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const rootStaticFiles = {
// paths where files lies
'/logo.png': 'public/images/logos',
'/robots.txt': 'public',
'/favicon.ico': 'public/images/icons',
// '/pdf.worker.js': '.next/static/chunks',
// '/service-worker.js': '.next',
'/sitemap.xml': 'public',
'/static/sitemap.xml': '',
'/public/sitemap.xml': '',
}
if (
rootStaticFiles[parsedUrl.pathname] ||
path.extname(parsedUrl.pathname) === '.xml'
) {
const fileFolder =
path.extname(parsedUrl.pathname) !== '.xml' &&
!rootStaticFiles[parsedUrl.pathname]
? '.next'
: rootStaticFiles[parsedUrl.pathname]
const pathToFile = join(__dirname, fileFolder, parsedUrl.pathname)
app.serveStatic(req, res, pathToFile)
} else {
handle(req, res, parsedUrl)
}
}).listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})