I am trying using Koa as server side framework of my project. And I followed the example and got my koa's version below:
server.js:
import Koa from 'koa'
import next from 'next'
import Router from 'koa-router'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev: true })
const handle = app.getRequestHandler()
app.prepare().then(() => {
const server = new Koa
const router = new Router
router.get('*', async (ctx) => {
handle(ctx.req, ctx.res)
ctx.respond = false
})
server.use(router.routes())
server.listen(3000)
}).catch((e) => {
console.log(e.stack)
})
But when load static files 'main.js' and 'common.js' in .next, the browser complains that these files are not found with status 404, but the content of the files are actually correct.

I read the source code and found that next.js always tried to load gzip version of these files and if not found it would load the normal version.
So it actually run the serveStatic function twice. But the first time it tried to load gzip version but it failed then the res object got 404 statusCode, and the second the status code still retained in res object of koa for some unknown reasons.
Reseting status code in serveStaticWithGzip function fixed this problem:
export async function serveStaticWithGzip (req, res, path) {
const encoding = accepts(req).encodings(['gzip'])
if (encoding !== 'gzip') {
return serveStatic(req, res, path)
}
try {
const gzipPath = `${path}.gz`
res.setHeader('Content-Encoding', 'gzip')
await serveStatic(req, res, gzipPath)
} catch (ex) {
if (ex.code === 'ENOENT') {
res.statusCode = 200 // code added
res.removeHeader('Content-Encoding')
return serveStatic(req, res, path)
}
throw ex
}
}
I am using koa@2 and next@beta.
(Poor English, feel free to correct.)
I am trying using Koa as server side framework of my project. And I followed the example and got my koa's version below:
server.js:
But when load static files 'main.js' and 'common.js' in
.next, the browser complains that these files are not found with status 404, but the content of the files are actually correct.I read the source code and found that next.js always tried to load gzip version of these files and if not found it would load the normal version.
So it actually run the serveStatic function twice. But the first time it tried to load gzip version but it failed then the
resobject got 404statusCode, and the second the status code still retained inresobject of koa for some unknown reasons.Reseting status code in
serveStaticWithGzipfunction fixed this problem:I am using koa@2 and next@beta.
(Poor English, feel free to correct.)