fridays / next-routes

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

Middleware support #51

Closed fridays closed 7 years ago

fridays commented 7 years ago

Abandoned in favor of https://github.com/fridays/next-routes/pull/45

HaNdTriX commented 7 years ago

Redirecting and query transformation won't work on client page transitions :flushed:. Nevertheless for renderAndCache middleware is really useful.

I the past I have used composed Higher-Order Components (as universal middleware) for these use cases.

ajoslin commented 7 years ago

I like it. It's a good way to add lots of flexibility to the library.

One gripe: We're getting too many arguments in the route.add function. How about we change to route.add(page, path, options), with options allowing keys {name, middleware}?

With this addition more features could be added at a later point, and backwards compat can be achieved with if (typeof options === 'string') page = options.

Sly777 commented 7 years ago

The code looks great but If I need to do this globally (not route by route) how can I do this?

We need to think about also how to use it globally. Maybe we need to add function like;

// i'm bad on naming :D
//
// it will work on every routes (such as /hi/:id)
//
routes.onRoutePaired = ({app, req, res, route, query}) => {
  return Object.assign(query, {some: 'thing'})

  // or promise
  return Promise.resolve(Object.assign(query, {some: 'thing'}))

  // or custom render
  renderAndCache(app, req, res, route.page, query)

  // or redirect
  res.writeHead(301, {'Location': `/hello/${query.id}`})
  res.end()
};
fridays commented 7 years ago

@HaNdTriX You're right redirects and query transforms should go into getInitialProps where they run on both client and server. @ajoslin does that work in your case?

Actually since the routes file goes into the client bundle, let's forget this and continue with #45 which targets the right place for server handlers like renderAndCache