Serverless Next.js 9 on Netlify

Hey everyone,

Fellow NextJS- and Netlify-fan here! :heart:

I’ve been looking for a way to bring serverless NextJS to Netlify for a while. And I’m very happy to share that I’ve found a solution! :grin:

I’ve packed that solution up into a small npm package called next-on-netlify. It’s just a compatibility wrapper for hosting NextJS apps on Netlify. Check out this Medium article on getting started in 5 quick steps.

Basically, the package does three things:

  1. Copy each NextJS pages into either the public directory (if it uses .getInitialProps and does not need to be SSRed) OR the functions directory (if it uses .getInitialProps and does need to be SSRed)
  2. Create a Netlify function called nextRouter that is responsible for server-side rendering those pages that use .getInitialProps
  3. Set up a Netlify _redirects file and proxy all requests for SSR pages to the nextRouter Netlify function

You can check out a demo here: next-on.netlify.com/
And you can check out the main script of the package here: next-on-netlify/next-on-netlify.js at master · netlify/next-on-netlify · GitHub

Let me know what you think, whether you run into any issues, any ideas for improvements, etc… And if you end up building anything with next-on-netlify, I would love to hear about it, too :slight_smile: