question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] server(less) middleware

See original GitHub issue

Feature request

Currently there are a lot of users that create a custom server to do middleware behavior, for example server-side parsing of request bodies, cookies etc. Currently Next.js provides no way of handling this without a custom server. Meaning it can’t be done in serverless / other environments.

Describe the solution you’d like

Ideally users would define middleware as a top level export. Meaning that pages can define middleware requirements:

// PageContext being the same values as `getInitialProps`'s context
export async function middleware({req, res}: PageContext) {
  // do something with `req` / `res`
}

function IndexPage() {
  return <h1>Hello world</h1>
}

export default IndexPage

However this also introduces the complexity of having to code-split / tree shake away user imports that are server-only.

So for the initial implementation (and incrementally working towards the implementation above) I’d start with supporting middleware in pages_document.js which is always server-side rendered so it makes sense to have it there.

One thing that was brought up is “why can’t this just be done in getInitialProps of _document”.

The reason that we need a new method is that the lifecycle of calling getInitialProps looks like this:

  • pages/_app.js getInitialProps is called,
  • pages/_app.js getInitialProps calls the page’s getInitialProps
  • pages/_document.js getInitialProps is called
  • pages/_document.js getInitialProps calls renderPage
  • renderPage calls React’s renderToString and returns the html, head tags and styles.
  • renderToStaticMarkup is called to render the _document shell
  • request is ended using send-html.ts, which adds etag etc.

Generally when using middleware it has to be called before getInitialProps because you could be parsing something needed in getInitialProps

So the middleware has to be called earlier.

Meaning the lifecycle would look like:

  • pages/_document.js middleware is called
  • pages/_app.js getInitialProps is called,
  • pages/_app.js getInitialProps calls the page’s getInitialProps
  • pages/_document.js getInitialProps is called
  • pages/_document.js getInitialProps calls renderPage
  • renderPage calls React’s renderToString and returns the html, head tags and styles.
  • renderToStaticMarkup is called to render the _document shell
  • request is ended using send-html.ts, which adds etag etc.

So for the initial implementation we’d want something like:

// pages/_document.js

// PageContext being the same values as `getInitialProps`'s context
export async function middleware({req, res}: PageContext) {
  // do something with `req` / `res`
}

// rest of _document.js

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:126
  • Comments:55 (34 by maintainers)

github_iconTop GitHub Comments

51reactions
isaachinmancommented, Jul 9, 2019

Hey @timneutkens and @Timer - great to see v9 has landed. I do believe that first-class support for API routes is a monumental and vital step for NextJs.

I’ve looked through #7297 and the new documentation, and it seems middleware support is not there yet, right? Would love to be able to rewrite next-i18next and remove the custom server requirement (https://github.com/isaachinman/next-i18next/issues/274).

24reactions
timneutkenscommented, Apr 28, 2020

Current plan is to have the notion of languages / language detection built into Next.js, that way we can make it work with both SSG and SSR and have it work on the edge for the majority of cases. This will be similar to eg preview mode.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 2768 - Network Policy and Services - IETF Datatracker
RFC 2768 A Report of a Workshop on Middleware February 2000 security, and operational tools. The need for a more organized framework for...
Read more >
Tim on Twitter: "@meabed If you have a bunch of middlewares it's ...
Currently a lot of users create a custom server.js to handle creating their API. ... [RFC] server(less) middleware · Issue #7208 · vercel/next.js....
Read more >
Serverless Next.js Component - Serverless Framework: Plugins
js 12 features Features like middleware, bot-aware ISR fallback, AVIF image support, etc. are not yet supported, though with the latest component version,...
Read more >
Redshift Serverless Client - AWS SDK for JavaScript v3
Defined in packages/middleware-retry/dist-types/omitRetryHeadersMiddleware.d.ts:3. Const parseBoolean. parseBoolean: (value: ...
Read more >
2 What's New in Oracle WebLogic Server
Oracle Database 12c Integration. Optimizations for Exalogic Elastic Cloud Software. Fusion Middleware Control. Additional Component-Level Features and Changes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found