Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): support building and serving production server #2792

Merged
merged 1 commit into from
Apr 4, 2020

Conversation

jaysoo
Copy link
Member

@jaysoo jaysoo commented Apr 2, 2020

This PR adds the ability to build a build a Next.js app that can be served locally, as well as easily deployable to platforms that support Node.

Why?

If you want proper SSR then the dist/apps/myapp folder must follow normal convention (e.g. have a .next folder, etc.). And there must be a package.json file that contains all the necessary deps. We also need a builder for running the compiled app in production mode.

Changes

  • Changed output folder structure for @nrwl/next:build so that it has a .next folder and package.json file with correct dependencies. The package versions are read out from workspace package.json file.

  • Renamed @nrwl/next:dev-server to @nrwl/next:server because it is meant to run the app for production as well.

  • The server builder no longer runs a build automatically before serving. Instead we will display an error message that follows the Next error message (e.g. Could not start production server. Try building your app with 'nx build my-app'.)

  • Removed environment.ts from being generate by app schematic, as well as the corresponding fileReplacements entry for build -- the understanding is the build is always for production so you'd never do nx build myapp --prod. Usually React devs use NODE_ENV to maintain different configurations.

Example

nx g @nrwl/next:app demo
nx build demo
nx serve demo --prod --host=0.0.0.0 --port=3000

This will start the prod server and listen on 0.0.0.0:3000. I've also verified that the compiled app in dist can be deployed to Heroku (see: https://hackmd.io/US2eky6lSMecuHxegFZCbA?view).

@jaysoo jaysoo force-pushed the feat-nextjs-serve-builder branch 3 times, most recently from dc49c9b to 0d7b386 Compare April 3, 2020 01:10
@jaysoo jaysoo marked this pull request as ready for review April 3, 2020 01:16
Copy link
Collaborator

@jdpearce jdpearce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me.

Are you missing a test for the createPackageJson function though? Something in the e2e tests, maybe?

@jaysoo jaysoo force-pushed the feat-nextjs-serve-builder branch from 0d7b386 to 942fe51 Compare April 4, 2020 18:12
@jaysoo jaysoo force-pushed the feat-nextjs-serve-builder branch from 942fe51 to 0fa9a33 Compare April 4, 2020 18:15
@jaysoo jaysoo merged commit 36e1d40 into nrwl:master Apr 4, 2020
@jaysoo jaysoo deleted the feat-nextjs-serve-builder branch April 4, 2020 18:57
@egdelwonk
Copy link

egdelwonk commented Apr 14, 2020

Will this land in 9.2.3? cc @jdpearce @jaysoo

@mcalhoun
Copy link

mcalhoun commented May 7, 2020

Very keen to try this out and I notice it was excluded from v9.2.3 and v9.2.4. Could you share the release plan?

@dabbeg
Copy link

dabbeg commented May 8, 2020

Very keen to try this out and I notice it was excluded from v9.2.3 and v9.2.4. Could you share the release plan?

It will be included in the next minor release, you can try it out on v9.3.0-beta.3.

@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants