Search code examples
javascripttypescriptnext.jsfrontendvercel

how to make a standalone build when using nextJS?


I'm working on a project where they chose to use nextJS as a client side tool, but I want to deploy the client as static code somewhere else.

Doing a build creates a folder with all the nextJS proprietary server stuff.

Is there a way to make a normal build to just static react files? I don't want SSR or any of their EEE stuff.

I know Vercel is a VC backed mousetrap, so it's not really in their interest to make it easy to extract yourself, but hoping we don't have to rebuild the site to get out of this.


Solution

  • Update your build script in package.json to use next export:

    "scripts": {
      "build": "next build && next export"
    }
    

    Note: this feature does not support the following:

    • Image Optimization (default loader)
    • Internationalized Routing
    • API Routes
    • Rewrites
    • Redirects
    • Headers
    • Middleware
    • Incremental Static Regeneration
    • fallback: true
    • getServerSideProps

    If you are using any of the features listed above, you will have to stick to prerendering individual pages, which Next automatically does for you.

    Source: Docs


    UPDATE February 2024: the configuration to for static exports has changed, slightly. Refer to the latest Next.js docs for details.