next-on-netlify is a utility for hosting NextJS applications with Server-Side Rendering on Netlify. It wraps your NextJS application in a tiny compatibility layer, so that pages can be server-side rendered with Netlify functions.
- Demo: https://next-on.netlify.com/
- Example repository: https://github.com/FinnWoelm/next-on-netlify-demo
npm install --save next-on-netlify
We must build our NextJS app as a serverless app. You can read more about serverless NextJS here.
It's super simple. Just create a next.config.js
file and write the following:
// next.config.js
module.exports = {
// Target must be serverless
target: 'serverless'
};
The next-on-netlify package adds the next-on-netlify
command. When we run this command, some magic happens to prepare our NextJS app for hosting on Netlify*.
We want the next-on-netlify command to run after we build our NextJS application. So let's add a postbuild hook to our package.json file:
{
"name": "my-nextjs-app",
"scripts": {
"dev": "next",
"build": "next build",
"postbuild": "next-on-netlify"
},
....
}
*If you're curious about the "magic", check out the well-documented next-on-netlify.js
file.
We're almost done! We just have to tell Netlify how to build our NextJS app, where the functions folder is located, and which folder to upload to its CDN. We do that with a netlify.toml
file and the following instructions:
[build]
command = "npm run build"
functions = "out_functions"
publish = "out_publish"
We're done. Let's deploy 🚀🚀🚀
I recommend you still use next dev
to build and preview your application locally.
But if you want to emulate the Netlify deployment on your computer, you can also run next-on-netlify
locally and then use netlify-cli
to preview the result.
First, install netlify-cli
v2.51.0 or later:
npm install -g netlify-cli
Then, add the following [dev]
block to your netlify.toml
:
# netlify.toml
# [build]
# ...
[dev]
functions = "out_functions"
publish = "out_publish"
# We manually set the framework to static, otherwise Netlify automatically
# detects NextJS and redirects do not work.
# Read more: https://github.com/netlify/cli/blob/master/docs/netlify-dev.md#project-detection
framework = "#static"
Lastly, add the following lines to your .gitignore
:
# .gitignore
# Files generated by next-on-netlify command
/out_publish/
/out_functions/
/404.html
Now you're all set.
From now on, whenever you want to preview your application locally, just run:
npm run build
: This will runnext build
to build your NextJS app andnext-on-netlify
to prepare your NextJS app for compatibility with Netlifynetlify dev
: This will emulate Netlify on your computer and let you preview your app onhttp://localhost:8888
.
You can define custom redirects in the netlify.toml
file.
Routes defined by next-on-netlify
take precedence over routes
defined in netlify.toml
.
In the past, it was possible to define custom redirects in a _redirects
file. This is not possible anymore. Let me know if you have a need for this feature and we can add it back.
Read more about Netlify redirects here.
next-on-netlify
creates one Netlify Function for each of your
SSR pages and API endpoints. It is currently not possible to create custom Netlify Functions. Let me know if you have a need for this feature and we can add it.
NextJS Preview Mode is currently not supported. When you call res.setPreviewData({})
, NextJS tries to set and send two cookies to the user. But Netlify Functions only support setting one cookie per function call at the moment. This is a long-standing bug on Netlify's side. We're discussing work-arounds. See: Issue #10
Fallback pages behave differently with next-on-netlify
than they do with NextJS. On NextJS, when navigating to a path that is not defined in getStaticPaths
, it first displays the fallback page. NextJS then generates the HTML in the background and caches it for future requests.
With next-on-netlify
, when navigating to a path that is not defined in getStaticPaths
, it server-side renders the page and sends it directly to the user. The user never sees the fallback page. The page is not cached for future requests.
For more on this, see: Issue #7
📣 Shoutout to @mottox2 (a pioneer of hosting NextJS on Netlify) and @danielcondemarin (author of serverless-next.js for AWS). The two were big inspirations for this package.
🙌 Big "thank you" to the following people for their contributions, support, and beta testing: