fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Error when using route pattern at root #3

Closed sedubois closed 7 years ago

sedubois commented 7 years ago

I'm trying to simplify my webapp by using your package, thanks for providing! 👍

Here is my attempt: https://github.com/relatenow/relate/compare/next-routes

However, I get EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

Maybe it's because I have a route /:slug which, when not handled properly, might also match Next.js' internal routes such as __webpack_hmr etc.

How can this be fixed?

fridays commented 7 years ago

Hey, thanks for using it!

The /:slug route is indeed the problem, you can solve it with something like this in server.js:

const nextHandler = app.getRequestHandler();
const routesHandler = routes.getRequestHandler(app);

app.prepare().then(() => {
  const server = express();

  const nextPaths = [
    '/_next/',
    '/_webpack/',
    '/__webpack_hmr',
    '/static/'
  ];

  server.use((req, res) => {
    if (nextPaths.some(path => req.url.startsWith(path))) {
      nextHandler(req, res)
    } else {
      routesHandler(req, res)
    }
  }).listen(3000)
});
sedubois commented 7 years ago

Thanks 👍 Considering that this package aims to simplify routing, could it be improved to take care of this itself?

fridays commented 7 years ago

You're right, this is now baked in.

Please run:

npm update --save next-routes :beer:

sedubois commented 7 years ago

Thanks, it works!