next-routes icon indicating copy to clipboard operation
next-routes copied to clipboard

add metadata to route

Open iherem opened this issue 8 years ago • 5 comments

When I can't specify additional values and send them to custom handlers. Then I add the metadata into the constuctor to specify the value of what will be added to the custom handler.

Suppose I want to specify the locale of route routes.add({ name: 'home', pattern: '/home', page: 'home', metadata: { locale: 'en' } })

The custom handlers receive the metadata

const handler = routes.getRequestHandler(app, ({ req, res, route, query }) => {
  const { metadata: { locale }, page } = route;
  renderWithCache(req, res, page, locale, query);
});

and the simple case if you want to specify some of route for cache just add the { cache: true } in the metadata

reference issue #127

iherem avatar Mar 13 '18 07:03 iherem

Coverage Status

Coverage increased (+0.08%) to 96.186% when pulling 8f434b0d28519bc83529343e5b62f839a8d4b979 on iherem:master into 4630d1961dbf65772fc957bb36a38cbfcb072318 on fridays:master.

coveralls avatar Mar 13 '18 07:03 coveralls

Naive question perhaps, but why can’t the locale be part of the pattern?

trotzig avatar Mar 13 '18 11:03 trotzig

Actually, I used it with my silly case. I don't know how other people do. for example: I have the base route /landing if i go straight to url /uk/landing or /ph/landing the content is changing depend on the route country. but the metadata can be everything you want to send to the server

iherem avatar Mar 13 '18 15:03 iherem

but the metadata can be everything you want to send to the server

What part is sending stuff to the server? I'm a little confused 🙂

trotzig avatar Mar 14 '18 12:03 trotzig

Also, you should update documentation to reflect these changes, and bump the library minor version according to https://semver.org/.

thomasdom avatar Apr 26 '19 21:04 thomasdom