This is an example of how to use @harperfast/nextjs to develop a Next.js application with Harper.
The Next.js application can interact with the database through the Resource API directly instead of relying on network operations. This is significantly more efficient and enables a better application development experience.
- Clone this repo locally
- Run
npm install - Run
npm run dev - Open http://localhost:9926 🎉
The easiest way to demonstrate this application remotely is to use the prebuilt: true option and the Harper deploy CLI command.
- Locally or in a CI environment, create a build using
npm run build - Modify
config.yamlto includeprebuilt: trueunder the@harperfast/nextjscomponent - Then deploy the prebuilt application using the Harper CLI:
harperdb deploy \
target="<operations api url>" \
username="<username>" \
password='<password>' \
project=nextjs-example \
package=HarperFast/nextjs-example \
skip_node_modules=false \
replicated=true \
restart=trueCheck out the included build and deploy workflow for an example of how to automate this process.
This example in and of itself is a Harper Component, and is reliant on the @harperfast/nextjs plugin in order to access the Harper Resource API. The globals are only available on server-side code paths such as server actions and server components. Any code paths using Harper globals must first import the harper package (i.e. import('harper')).
Based on Next.js best practices, it is recommended to use this in server actions so that server and client components can both access the same functions. This example demonstrates this pattern by defining two server actions, listDogs and getDog (located in ./app/actions.js). These are then used throughout the application, in both Client and Server components!