fridays / next-routes

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

Koa setup: 404 page between route transitions #114

Closed victorbit closed 6 years ago

victorbit commented 6 years ago

I'm using next-routes in a Koa setup. Between route transitions I (shortly) see a 404 page before entering the new route.

  // on the server
  server.use(async (ctx, next) => {
    ctx.respond = false
    ctx.res.statusCode = 200
    handler(ctx.req, ctx.res)
    await next()
  })
// a example of a failing route
routes.add(
  'event',
  ['/:slug']
)
// use of Link elm
import { Link } from '../routes'

<Link to={`event/1231`} />
// also using the element like this gives the 404
<Link route ='event' params={{slug: '1231'}} />
eghernqvist commented 6 years ago

Getting same behaviour with http server, in development, on routes defined in routes.js.

// server.js
const next = require('next');
const routes = require('./routes');
const app = next({ dir: './app', dev: process.env.NODE_ENV !== 'production' });
const handler = routes.getRequestHandler(app);

const { createServer } = require('http');
app.prepare().then(() => {
  createServer(handler).listen(3000);
});
// routes.js
const nextRoutes = require('next-routes');
const routes = module.exports = nextRoutes();

routes
  .add('/venue/map', 'map');
eghernqvist commented 6 years ago

Appears to have been resolved for me by stopping the dev server, rebuild and start again.

fridays commented 6 years ago

If it happens again, please try to prepend a slash to the linked path:

<Link to={`/event/1231`} />
fridays commented 6 years ago

Also when adding the route, it should look like this:

routes.add('event', '/event/:slug')
laurenskling commented 6 years ago

Same here with an express setup. 404 and page refresh in between transitions

fridays commented 6 years ago

@laurenskling Please open a new issue for that with some sample code. Thanks!