• Stars
    star
    575
  • Rank 77,622 (Top 2 %)
  • Language
    TypeScript
  • Created about 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Next.js Runtime allows Next.js to run on Netlify with zero configuration

Next.js Runtime

Next.js Runtime

Next.js is supported natively on Netlify, and in most cases you will not need to install or configure anything. This repo includes the packages used to support Next.js on Netlify.

Deploying

If you build on Netlify, the Next.js Runtime will work with no additional configuration. However if you are building and deploying locally using the Netlify CLI, you must deploy using netlify deploy --build. Running the build and deploy commands separately will not work, because the Next.js Runtime will not generate the required configuration.

Using next/image

If you use next/image, your images will be automatically optimized at runtime, ensuring that they are served at the best size and format. The image will be processed on the first request which means it may take longer to load, but the generated image is then cached and served as a static file to future visitors. By default, Next.js will deliver WebP images if the browser supports it. WebP is a modern image format with wide browser support that will usually generate smaller files than PNG or JPG. Additionally, you can enable AVIF format, which is often even smaller in filesize than WebP. The drawback is that with particularly large images, AVIF images may take too long to generate, and the function times-out. You can configure the supported image formats in your next.config.js file.

Enabling Edge Images

It is possible to run image content negotiation on the edge. This allows images to be processed on the first request, and then, in future loads, served from cache on the edge.

In order to deliver the correct format to a visitor's browser, this uses a Netlify Edge Function. In some cases your site may not support Edge Functions, in which case it will instead fall back to delivering the original file format.

To turn on Edge image handling for Next/Image, set the environment variable NEXT_FORCE_EDGE_IMAGES to true

Returning custom response headers on images handled by ipx

Should you wish to return custom response headers on images handled by the netlify-ipx package, you can add them within your project's netlify.toml by targeting the /_next/image/* route:

[[headers]]
  for = "/_next/image/*"

  [headers.values]
    Strict-Transport-Security = "max-age=31536000"
    X-Test = 'foobar'

Disabling included image loader

If you wish to disable the use of the image loader which is bundled into the runtime by default, set the DISABLE_IPX environment variable to true.

This should only be done if the site is not using next/image or is using a different loader (such as Cloudinary or Imgix).

See the Next.js documentation for image loader options.

Next.js Middleware on Netlify

Next.js Middleware works out of the box on Netlify. By default, middleware runs using Netlify Edge Functions. For legacy support for running Middleware at the origin, set the environment variable NEXT_DISABLE_NETLIFY_EDGE to true. Be aware that this will result in slower performance, as all pages that match middleware must use SSR.

For more details on Next.js Middleware with Netlify, see the middleware docs.

Limitations

Due to how the site configuration is handled when it's run using Netlify Edge Functions, data such as locale and defaultLocale will be missing on the req.nextUrl object when running netlify dev.

However, this data is available on req.nextUrl in a production environment.

Monorepos

If you are using a monorepo you will need to change publish to point to the full path to the built .next directory, which may be in a subdirectory. If you have changed your distDir then it will need to match that.

If you are using Nx, then you will need to point publish to the folder inside dist, e.g. dist/apps/myapp/.next.

Incremental Static Regeneration (ISR)

The Next.js Runtime fully supports ISR on Netlify. For more details see the ISR docs.

Note that Netlify has a minimum TTL of 60 seconds for revalidation.

Disable Static 404 on Dynamic Routes with fallback:false

Currently when hitting a non-prerendered path with fallback=false it will default to a 404 page. You can now change this default setting by using the environemnt variable LEGACY_FALLBACK_FALSE=true. With the environment variable set, those non-prerendered paths will now be routed through using the ISR Handler and will allow you to add redirects for those non-prerendered paths.

Use with next export

If you are using next export to generate a static site, you do not need most of the functionality of this Next.js Runtime and you can remove it. Alternatively you can set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to true and the Next.js Runtime will handle caching but won't generate any functions for SSR support. See demos/next-export for an example.

Asset optimization

Netlify asset optimization should not be used with Next.js sites. Assets are already optimized by Next.js at build time, and doing further optimization can break your site. Ensure that it is not enabled at Site settings > Build & deploy > Post processing > Asset optimization.

Generated functions

The Next.js Runtime works by generating three Netlify functions that handle requests that haven't been pre-rendered. These are ___netlify-handler (for SSR and API routes), ___netlify-odb-handler (for ISR and fallback routes), and _ipx (for images). You can see the requests for these in the function logs. For ISR and fallback routes you will not see any requests that are served from the edge cache, just actual rendering requests. These are all internal functions, so you won't find them in your site's own functions directory.

The Next.js Runtime will also generate a Netlify Edge Function called 'ipx' to handle image content negotiation, and if Edge runtime or middleware is enabled it will also generate edge functions for middleware and edge routes.

Manually installing the Next.js Runtime

The Next.js Runtime installs automatically for new Next.js sites on Netlify. You can also install it manually in the following ways:

From the UI (Recommended):

You can go to the UI and choose the site to install the Next.js Runtime on. This method is recommended because you will benefit from auto-upgrades to important fixes and feature updates.

From npm:

npm install -D @netlify/plugin-nextjs

...then add the following to your netlify.toml file:

[[plugins]]
  package = "@netlify/plugin-nextjs"

This method is recommended if you wish to pin the Next.js Runtime to a specific version.

Manually upgrading from an older version of the Next.js Runtime

If you previously set these values, they're no longer needed and should be removed:

  • distDir in your next.config.js
  • node_bundler = "esbuild" in netlify.toml
  • external_node_modules in netlify.toml
  • The environment variable NEXT_USE_NETLIFY_EDGE can be removed as this is now the default

The serverless and experimental-serverless-trace targets are deprecated in Next.js 12, and all builds with this Next.js Runtime will now use the default server target. If you previously set the target in your next.config.js, you should remove it.

If you currently use redirects or rewrites on your site, see the Rewrites and Redirects guide for information on changes to how they are handled in this version. In particular, note that _redirects and _headers files must be placed in public, not in the root of the site.

Using with pnpm

If your site uses pnpm to manage dependencies, currently you must enable public hoisting. The simplest way to do this is to create a .npmrc file in the root of your project with the content:

public-hoist-pattern[]=*

Running the tests

To run the tests, ensure that the dependencies are installed as follows:

npm install

Then run the tests:

npm test

End-to-end tests

In order to run the end-to-end (E2E) tests, you'll need to be logged in to Netlify. You can do this using the Netlify CLI with the command:

netlify login

Alternatively, you can set an environment variable NETLIFY_AUTH_TOKEN to a valid Netlify personal access token. This can be obtained from the Netlify UI.

Then run the E2E tests if logged in:

npm run test:next

Or if using an access token:

NETLIFY_AUTH_TOKEN=your-token-here npm run test:next

Note: The E2E tests will be deployed to a Netlify owned site. To deploy to your own site then set the environment variable NETLIFY_SITE_ID to your site ID.

Feedback

If you think you have found a bug in Next.js on Netlify, please open an issue. If you have comments or feature requests, see the discussion board

More Repositories

1

netlify-cms

A Git-based CMS for Static Site Generators
JavaScript
16,192
star
2

gotrue

An SWT based API for managing users and issuing SWT tokens.
Go
3,530
star
3

staticgen

StaticGen.com, A leaderboard of top open-source static site generators
JavaScript
2,471
star
4

cli

Netlify Command Line Interface
TypeScript
1,576
star
5

gocommerce

A headless e-commerce for JAMstack sites.
Go
1,465
star
6

netlify-identity-widget

A zero config, framework free Netlify Identity widget
JavaScript
735
star
7

next-on-netlify

Build and deploy Next.js applications with Server-Side Rendering on Netlify!
JavaScript
720
star
8

headlesscms.org

Source for headlesscms.org
JavaScript
628
star
9

netlify-lambda

Helps building and serving lambda functions locally and in CI environments
JavaScript
601
star
10

functions.netlify.com

Tutorials, examples, workshops and a playground for serverless with Netlify Functions
SCSS
515
star
11

build-image

This is the build image used for running automated builds
Shell
498
star
12

gotrue-js

JavaScript client library for GoTrue
JavaScript
457
star
13

create-react-app-lambda

JavaScript
414
star
14

netlify-faunadb-example

Using FaunaDB with netlify functions
JavaScript
388
star
15

actions

Shell
360
star
16

git-gateway

A Gateway to Git APIs
Go
355
star
17

zip-it-and-ship-it

Intelligently prepare Node.js Lambda functions for deployment
JavaScript
305
star
18

gotell

Netlify Comments is an API and build tool for handling large amounts of comments for JAMstack products
Go
276
star
19

explorers

JavaScript
262
star
20

million-devs

Microsite for the 1 Million Developers announcement.
Vue
250
star
21

netlify-statuskit

Netlify StatusKit is a template to deploy your own Status pages on Netlify.
HTML
237
star
22

open-api

Open API specification of Netlify's API
Go
234
star
23

build

Netlify Build (node process) runs the build command, Build Plugins and bundles Netlify Functions. Can be run in Buildbot or locally using Netlify CLI
JavaScript
216
star
24

js-client

A Open-API derived JS + Node.js API client for Netlify
JavaScript
214
star
25

netlify-plugin-lighthouse

Netlify Plugin to run Lighthouse on each build
JavaScript
198
star
26

netlifyctl

Go
178
star
27

netlify-dev-plugin

Local dev server with functions, rules engine and add-on support
JavaScript
176
star
28

framework-info

Framework detection utility
JavaScript
137
star
29

jekyll-srcset

Dead simple responsive images for jekyll
Ruby
136
star
30

gocommerce-js

A gocommerce client library
JavaScript
130
star
31

jekyll-gdrive

Access a Google Drive Spreadsheet from your Jekyll templates
Ruby
116
star
32

plugins

Netlify plugins directory.
JavaScript
95
star
33

prerender

Automatically rendering JS-driven pages for crawlers and social sharing
JavaScript
94
star
34

netlify-playground

89
star
35

netlify-plugin-gatsby

A build plugin to integrate Gatsby seamlessly with Netlify
TypeScript
88
star
36

code-examples

Code snippets for customers
HTML
87
star
37

labs

Documentation and samples for Netlify Labs features.
76
star
38

templates

This is board to showcase templates and boilerplates https://templates.netlify.com
Nunjucks
76
star
39

vue-cli-plugin-netlify-lambda

Netlify Lambda plugin for Vue CLI
JavaScript
76
star
40

remix-template

Deploy your Remix site to Netlify Edge Functions
JavaScript
73
star
41

netlify-cms-widget-starter

A boilerplate for creating Netlify CMS widgets.
JavaScript
73
star
42

classnames-template-literals

Small utility to format long classnames with template literals
JavaScript
70
star
43

edge-functions-examples

Explore a library of reference examples for learning about Edge Functions on Netlify.
JavaScript
70
star
44

gotiator

A tiny JWT based API gateway
Go
70
star
45

matterday.netlify.com

A site that asks us what we could do with more time.
CSS
70
star
46

react-server-components-demo

Minimal implementation on server components via Netlify functions
JavaScript
67
star
47

binrc

Binrc is a command line application to manage different versions of binaries stored on GitHub releases.
Makefile
52
star
48

next-on-netlify-demo

Demo of a Next.js app with Server-Side Rendering on Netlify
JavaScript
52
star
49

go-functions-example

Go
49
star
50

petsofnetlify

pets of netlifiers
Nunjucks
47
star
51

full-react-server-demo

JavaScript
45
star
52

rust-functions-example

Deploy Rust lambda functions on Netlify
Rust
45
star
53

gojoin

Mini API wrapping Stripes Subscriptions for Single Page Aps and JAMstack sites
Go
41
star
54

netlify-git-api

Go
41
star
55

gatsby-parallel-runner

JavaScript
38
star
56

functions

JavaScript and TypeScript utilities for Netlify Functions.
TypeScript
38
star
57

elastinats

Go
36
star
58

netlify-photo-gallery

HTML
35
star
59

gocommerce-admin

Admin UI for Netlify Commerce
JavaScript
35
star
60

addons

Netlify add-on documentation
34
star
61

ask-netlify

A place to submit questions for Netlify to answer in tutorials, podcasts and blog posts
HTML
34
star
62

netlify-auth-demo

Demo for integrating GitHub OAuth with a Netlify site
HTML
33
star
63

explorers-up-and-running-with-serverless-functions

Free resource for learning how to use serverless functions!
HTML
31
star
64

hydrogen-netlify-starter

Get started with Hydrogen on Netlify
JavaScript
31
star
65

build-plugin-template

Template repository to create new Netlify Build plugins.
JavaScript
30
star
66

twickr

Twickr lets you send tweets of interest from Twitter to Slack
Go
30
star
67

next-edge-middleware

JavaScript
29
star
68

remix-compute

Remix adapter and server runtime for Netlify
TypeScript
29
star
69

www-post-scheduler

This is a serverless function to auto publish blog posts
JavaScript
28
star
70

postcss-fout-with-a-class

Rewrite all selectors that will trigger a font load to be scoped under a class
JavaScript
28
star
71

micro-api-client

Small library for talking to micro REST APIs (not related to Netlify's main API)
JavaScript
28
star
72

vue-lambda-starter

Starter Template for Vue + AWS Lambda with Netlify
Vue
27
star
73

netlify-browser-extension

netlify-chrome-extension
JavaScript
26
star
74

make-wp-epic

Migration tool for moving from WordPress to Victor Hugo
JavaScript
26
star
75

hydrogen-platform

Hydrogen support for Netlify Edge Functions
TypeScript
26
star
76

netlify-redirect-parser

Library for parsing Netlify redirects
JavaScript
23
star
77

next-react-server-components

JavaScript
22
star
78

netlify-auth-providers

JS library to use Netlify's OAuth providers
JavaScript
22
star
79

explorers-composition-api

Learn how the Composition API works in this Jamstack Explorers mission!
Vue
20
star
80

vite-plugin-netlify-edge

Netlify Edge Function support for Vite
TypeScript
19
star
81

angular-runtime

The Angular Runtime allows Angular to run on Netlify with zero configuration
JavaScript
18
star
82

mailme

MailMe sends mails with stylish templates
Go
18
star
83

eslint-config-node

ESLint, Prettier and Editorconfig shared by Netlify's Node.js projects
JavaScript
18
star
84

slate-markdown-serializer

JavaScript
17
star
85

delta-action

A GitHub Action for capturing benchmark data and tracking its variation against a baseline
JavaScript
17
star
86

netlify-credential-helper

Git credential helper to use Netlify's API as authentication backend
Go
16
star
87

blobs

A TypeScript client for Netlify Blobs
TypeScript
16
star
88

netlify-cms-www

Former repo for netlifycms.org. Moved to the code repo at
CSS
15
star
89

go-client

Depreciated repo: home of the old go client. See netlify/open-api for the new home of the go client
Go
15
star
90

verify-okta

Small Lambda function for verifying and gating content with Okta
Go
14
star
91

netlify-oauth-example

JavaScript
14
star
92

godoc-static

Generates static HTML of documentation of Go libraries
Go
13
star
93

fauna-one-click

Moved https://github.com/netlify/netlify-faunadb-example
JavaScript
13
star
94

screenshot

Take screenshots of websites
Shell
13
star
95

netlify-comments-starter

Start project for Netlify Comments
13
star
96

node-template

Netlify's Node.js repository template
Python
13
star
97

streamer

tail files and send them to nats
Go
12
star
98

edge-bundler

Intelligently prepare Netlify Edge Functions for deployment
TypeScript
12
star
99

ruby-client

Netlify API client for Ruby
Ruby
11
star
100

gatsby-plugin-netlify

Gatsby plugin. Automatically generates a _headers file and a _redirects file at the root of the public folder to configure HTTP headers and redirects on Netlify.
TypeScript
11
star