leecade / koa-webpack-middleware

webpack dev&hot middleware for koa2
214 stars 27 forks source link

Crash when loading page before bundle ready #46

Open phiresky opened 7 years ago

phiresky commented 7 years ago

The browser gets a Not Found response, the server crashes with

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:504:11)
    at ServerResponse.setHeader (_http_outgoing.js:511:3)
    at Object.set (./node_modules/koa/lib/response.js:440:16)
    at Object.proto.(anonymous function) [as set] (./node_modules/delegates/index.js:40:31)
    at Object.setHeader (./node_modules/koa-webpack-middleware/lib/devMiddleware.js:29:23)
    at Object.handleRangeHeaders (./node_modules/webpack-dev-middleware/lib/Shared.js:61:8)
    at processRequest (./node_modules/webpack-dev-middleware/middleware.js:63:21)
    at continueBecauseBundleAvailable (./node_modules/webpack-dev-middleware/lib/Shared.js:122:6)
    at Array.forEach (native)
    at ./node_modules/webpack-dev-middleware/lib/Shared.js:121:9
    at _combinedTickCallback (internal/process/next_tick.js:95:7)
    at process._tickDomainCallback (internal/process/next_tick.js:198:9)

Even though webpack says webpack: wait until bundle finished: /

I've included the middleware as follows:

app.use(devMiddleware(webpack([config]), {
    watchOptions: {
        aggregateTimeout: 300,
        poll: true
    },
    historyApiFallback: true
}));

I'm using

koa-webpack-middleware@^1.0.5:
  version "1.0.5"
akatebi commented 7 years ago

I switched to the "webpack-koa2-middleware" package for the "devMiddleware". The usage is exactly the same, it's a simple drop in. This fixed my issue. I noticed that the new package does not respond to requests until Webpack has finished building. I suggest that this package should do the same to avoid this issue. Thanks!