fridays / next-routes

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

Use with SSR Caching #40

Closed dariye closed 7 years ago

dariye commented 7 years ago

Thanks for this lib! I'm trying to implement SSR caching . Seems not to be working as expected. I'm unable to get the right page path to use the caching function.

HaNdTriX commented 7 years ago

Could you provide some more information? Example? etc.?

dariye commented 7 years ago

Hey @HaNdTriX

I have the following:

...
function renderAndCache(req, res, pagePath, queryParams) {
  const key = getCacheKey(req)

  if (ssrCache.has(key)) {
    console.log(`CACHE HIT: ${key}`)
    res.send(ssrCache.get(key))
    return
  }

  app.renderToHTML(req, res, pagePath, queryParams)
    .then((html) => {
      if ( !dev ) {
        console.log(`CACHE MISS: ${key}`)
        ssrCache.set(key, html)
        console.log(`CACHE SET: ${key}`)
      }
      res.send(html)
    })
  .catch((error) => {
    app.renderError(error, req, res, pagePath, queryParams)
  })
}
...

I guess the question is how do I get the requests to named routes using next-routes and pass the right arguments to the renderAndCache? An option is to server.get() for all the routes but wondering if there's a cleaner way to go about it.

fridays commented 7 years ago

Please see https://github.com/fridays/next-routes/issues/4

dariye commented 7 years ago

thanks @fridays. Will close this and look into #4