Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with ssr-caching #4

Closed
sedubois opened this issue Feb 13, 2017 · 11 comments
Closed

Integration with ssr-caching #4

sedubois opened this issue Feb 13, 2017 · 11 comments

Comments

@sedubois
Copy link

Next.js provides a great SSR caching example which makes use of Next.js' renderToHTML and renderError. Can next-routes somehow be used in conjunction with this or could this be implemented?

https://github.com/zeit/next.js/tree/master/examples/ssr-caching

@fridays
Copy link
Owner

fridays commented Feb 13, 2017

I didn't try it yet, but it should work like this with the caching example:

const express = require('express')
const next = require('next')
const routes = require('./routes')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dir: '.', dev })
const handle = app.getRequestHandler()
const LRUCache = require('lru-cache')

const ssrCache = new LRUCache({
  max: 100,
  maxAge: 1000 * 60 * 60 // 1hour
})

app.prepare().then(() => {
  express().use(renderAndCache).listen(3000)
})

function renderAndCache (req, res) {
  if (ssrCache.has(req.url)) {
    return res.send(ssrCache.get(req.url))
  }

  // Match route + parse params
  const {route, params} = routes.match(req.url)
  if (!route) return handle(req, res)

  app.renderToHTML(req, res, route.page, params).then((html) => {
    ssrCache.set(req.url, html)
    res.send(html)
  })
  .catch((err) => {
    app.renderError(err, req, res, route.page, params)
  })
}

@sedubois
Copy link
Author

Ah nice, will try 🙂 It might be helpful to document in README the routes.match() API.

@fridays
Copy link
Owner

fridays commented Feb 14, 2017

I'll do that! Let me know if you encounter any issues. Closing this for now

@fridays fridays closed this as completed Feb 14, 2017
@sedubois
Copy link
Author

I tried in this branch: https://github.com/relatenow/relate/tree/ssr-caching

But I get in the server:

CACHE MISS: /__webpack_hmr
CACHE MISS: /_next-prefetcher.js
CACHE HIT: /__webpack_hmr

And in the console: EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

The __webpack_hmr requests shouldn't hit the cache I guess, but don't know how to configure that using next-routes. Any help appreciated :)

@sedubois
Copy link
Author

sedubois commented Feb 14, 2017

I tried adding this before checking the cache:

    if (req.url === '/__webpack_hmr') {
      return handle(req, res);
    }    

But still get

The script has an unsupported MIME type ('text/html').
Failed to load resource: net::ERR_INSECURE_RESPONSE

@sedubois
Copy link
Author

sedubois commented Feb 14, 2017

I also think that somehow, the route('profile', '/:slug') shouldn't match on /__webpack_hmr. /__webpack_hmr shouldn't match on any user route.

@sedubois
Copy link
Author

OK for some reason the console error doesn't appear any more (although I thought I had restarted the server to clear the cache).

The next issue is that it doesn't match on all the prefetcher routes, although this is the most important load that the server will get (most of the time, the server will only need to render when the user visits the home page, then the rest of SSR will only be triggered by the SSR requests):

Route not matched: /static/nprogress.css
Route not matched: /_next/-/commons.js
Route not matched: /_next/-/main.js
Route not matched: /_next/-/pages/
Route not matched: /_next/-/pages/discover
Route not matched: /_next/-/pages/about
Route not matched: /_next/-/pages/auth/login
Route not matched: /static/homepage/bg_TimMarshall.jpg

@sedubois
Copy link
Author

I asked about this issue in the original PR: vercel/next.js#497 (comment)

@sedubois
Copy link
Author

sedubois commented Feb 14, 2017

And I replied to myself 😄

Oh I keep forgetting, next-prefetcher doesn't do a pre-render, it just downloads the code.

@fridays
Copy link
Owner

fridays commented Feb 15, 2017

route('profile', '/:slug') shouldn't match on /__webpack_hmr

It doesn't since the catchall update from the last version, can you check?

I just updated /_next/ to /_next in the ignored paths, so prefetcher urls won't be matched by catchall routes anymore too.

Also you can now use routes.isNextPath(req.url) to test against the list of ignored paths. Maybe that can be helpful when integrating with the caching system.

@sedubois
Copy link
Author

It doesn't since the catchall update from the last version, can you check?

@fridays created #6

Also you can now use routes.isNextPath(req.url)

Seems to work 👍 (sedubois/relate@f8032ba#diff-bba6db1dd9623c6662b43cc660a5975cR20)

elliottsj referenced this issue in Yolk-HQ-old/next-routes Oct 15, 2018
SeanDemps pushed a commit to SeanDemps/next-routes that referenced this issue Dec 13, 2018
Update dependency concurrently to v3.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants