Coder Social home page Coder Social logo

joseocabarcas / next-on-netlify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from netlify/next-on-netlify

0.0 0.0 0.0 18 KB

Wrapper for hosting NextJS applications with Server-Side Rendering on Netlify

License: MIT License

JavaScript 100.00%

next-on-netlify's Introduction

README

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.

Installation

npm install --save next-on-netlify

Setup

1. Set NextJS target to serverless

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',
  // distDir already has .next as default
  distDir: '.next',
};

2. Add postbuild hook

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.

3. Configure Netlify

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 = "functions"
  publish   = "public"

We're done. Let's deploy ๐Ÿš€๐Ÿš€๐Ÿš€

Optional Extras

Preview Locally

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.

To do that, first install serve and netlify-cli:

npm install --save-dev serve
npm install -g netlify-cli

Then add the following [dev] block to your netlify.toml:

[dev]
  command   = "serve public -p 3000"
  functions = "functions"
  publish   = "public"

And add the following lines to your .gitignore:

# .gitignore

# Files generated by next-on-netlify command
functions/nextRouter
public/_next
public/_redirects

Now you're all set.

From now on, whenever you want to preview your application locally, just run:

  1. next build to build your NextJS app
  2. then next-on-netlify to prepare it for compatibility with Netlify
  3. and then netlify-cli dev to preview your app as if it was hosted on Netlify

Add Custom Redirects

next-on-netlify defines redirects in public/_redirects. Do not manually add redirects there or they will be overwritten the next time you run next-on-netlify.

Instead, you can define redirects in a _redirects file at the root level. These will be merged into the public/_redirects when you run next-on-netlify.

Or you can define custom redirects in the netlify.toml file.

Read more about Netlify redirects here.

Limitations

next-on-netlify has only been tested on NextJS version 9 and above.

next-on-netlify does not yet support catch-all routes (yet).

Credits

๐Ÿ“ฃ 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.

next-on-netlify's People

Contributors

finnwoelm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.