Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using koa as server side framework, status 404 occurs when load static files. #677

Closed
livoras opened this issue Jan 6, 2017 · 4 comments

Comments

@livoras
Copy link

livoras commented Jan 6, 2017

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.

image

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.)

@sedubois
Copy link
Contributor

Would be great indeed to have a working Next-Koa example 🙂

@impronunciable
Copy link
Contributor

Added example https://github.com/zeit/next.js/pull/800/files

closing this

@aga5tya
Copy link
Contributor

aga5tya commented Jan 17, 2017

The issue of serving static assets over Koa still exits on the latest master. I checked it through the example added by @impronunciable and can be reproduced.

screen shot 2017-01-18 at 12 32 47 am

@arunoda
Copy link
Contributor

arunoda commented Jan 18, 2017

Yep it is. Here's the fix: #815

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants