fridays / next-routes

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

Ability to add metadata to a route #127

Closed majelbstoat closed 1 year ago

majelbstoat commented 6 years ago

I'd love a generalised way to attach metadata to a route. For example:

.add({ name: 'home', pattern: '/', page: 'index', metadata: { cache: true } })

such that I could then have a custom handler:

const handler = routes.getRequestHandler(app, ({ req, res, route, query }) => {
  if (route.metadata.cache) {
    // check/set pre-rendered HTML cache
  } else {
    app.render(req, res, route.page, query)
  }
})

(Specific use-case is a cache for logged out users who will all see the same thing and which can be cached aggressively - like this: https://github.com/zeit/next.js/blob/canary/examples/ssr-caching/server.js#L48, but I suspect a generalised metadata approach will support other use-cases too.)

fridays commented 6 years ago

Sounds like a good idea! Could you send a PR with this and some docs for the readme?

majelbstoat commented 6 years ago

Would you be up for (in a separate patch) supporting additional complexity like additional pluggable handlers for POST/PUT etc, so this was a uniform routing framework, which could also handle things like Passport routes? I'd really like to be able to define all the routes in one place, but that might be outside the scope of what you want for this project.