next-boost / next-boost

Add a cache layer for server-side-rendered pages with stale-while-revalidate. Can be considered as an implementation of next.js's Incremental Static Regeneration which works with getServerSideProps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting status 504 when request is aborted during caching and a new request is sent

k4rli opened this issue · comments

commented

I'm using next-boost with redis cache adapter. It appears that 504 can easily be produced in uncached pages that have getServerSideProps taking more than 1 sec (this probably doesn't matter but for me getServerSideProps can take 5 seconds easily).

Using next-boost request handler in a standard way

// nextRequestHandlerMiddleware.js

const CachedHandler = require("@next-boost/next-boost").default;

const getNextRequestHandler = async (dev) => {
  const args = { dev };

  let handler;
  if (dev) {
    const init = require("./init").default;
    handler = await init(args);
  } else {
    const script = require.resolve("./init");
    const cached = await CachedHandler(
      {
        script,
        args,
      },
      {
        quiet: true,
      },
    );
    handler = cached.handler;
  }
  return handler;
};

module.exports = getNextRequestHandler;
// server.js

...

  const handler = await getNextRequestHandler(IS_DEV);

  server.get("*", handler);

How to reproduce:

# initial request is sent to the page without existing cache and aborted before a response is given with ctrl+c
curl "https://myapp/en/page-without-cache" -v 

# new request is sent right after aborting previous request, response is waited and will have status 504
curl "https://myapp/en/page-without-cache" -v

# final request is sent after 504, response to this will be 200 as the page will be cached at this point
curl "https://myapp/en/page-without-cache" -v
commented

Might have been the same case as "'get /slow-10100'" test, however I discovered that it would be best to have this library cache 308 nextjs redirects (return { redirect: { permanent: true, destination: url }). I included status code (in case of 200 or 308 from render result) in cached data in my fork and it got better.