next-drupal-starterkit icon indicating copy to clipboard operation
next-drupal-starterkit copied to clipboard

NEX-179: Move the redirect logic to middleware

Open vermario opened this issue 8 months ago • 0 comments

WORK IN PROGRESS

  • console.log messages left on purpose
  • one call to drupal for each page + prefetch links -> not great

The issue

In page router, we could check for the existence of a redirect when rendering a page with getStaticProps: the redirect response would them be cached an we could live happily.

In the app router, we cannot return a redirect from a static page in any case. (This was hard to spot, since when running in dev mode it works happily)

So the solutions as far as I understand are:

  1. marking the page as dynamic -> then it cannot be cached anymore, kind of makes the whole idea of incremental static revalidation moot
  2. moving the logic to middleware -> this is what we try to do in this PR

Why it's not great

The solution here seems to work:

  1. before showing a page, middleware is run
  2. middleware calls drupal to check if there is a redirect at the given path, and if so it redirects to another path as instructed by drupal
  3. (then this is again checked, to see if there's a redirect)

This works I think, but if the idea is to protect the backend from too many queries, this means again that for one page view there is at least one more query to Drupal. Also, when there are links on the page and those are prefetched, those are also all checked (I left the console.log messages so they can be seen).

I wonder if this is a necessary evil, or if there are ways to improve this.

For example can we cache on the frontend these redirect calls? but using which logic and how do we invalidate them? (maybe in-memory cache and using our usual REVALIDATE_LONG env var?)

vermario avatar Apr 29 '25 07:04 vermario