A Harper Plugin for running Next.js apps with Harper.
Note
This package currently supports Next.js v14, v15, and v16 only.
Note
This guide assumes you're already familiar with Harper Components. Please review the documentation, or check out the Harper Next.js Example for more information.
- Install:
npm install @harperfast/nextjs- Wrap your Next.js config with
withHarper(). All Next.js config formats are supported:
// next.config.js (CommonJS)
const { withHarper } = require('@harperfast/nextjs');
module.exports = withHarper({
// your existing Next.js config
});// next.config.mjs (ESM)
import { withHarper } from '@harperfast/nextjs';
export default withHarper({
// your existing Next.js config
});// next.config.ts (TypeScript)
import { withHarper } from '@harperfast/nextjs';
export default withHarper({
// your existing Next.js config
});- Add to
config.yaml:
'@harperfast/nextjs':
package: '@harperfast/nextjs'- Run your app with Harper v5:
harper run nextjs-app- Within any server-side code paths, you can use Harper Globals after importing the
harperpackage:
Just make sure you are using
withHarper()or that you've added theharper(orharper-pro) package to theserverExternalPackageslist in the Next.js config.
// app/actions.js
'use server';
import 'harper';
export async function listDogs() {
const dogs = [];
for await (const dog of tables.Dog.search()) {
dogs.push({ id: dog.id, name: dog.name });
}
return dogs;
}// app/dogs/[id]/page.jsx
import { getDog, listDogs } from '@/app/actions';
export async function generateStaticParams() {
const dogs = await listDogs();
return dogs;
}
export default async function Dog({ params }) {
const dog = await getDog(params.id);
return (
<section>
<h1>{dog.name}</h1>
<p>Breed: {dog.get('breed')}</p>
<p>Woof!</p>
</section>
);
}withHarper(config: NextConfig, harperConfig?: HarperConfig): NextConfig
A configuration helper that wraps your Next.js config. It automatically adds harper and harper-pro to serverExternalPackages so Harper's native dependencies are treated correctly by the bundler.
Example:
// next.config.js
const { withHarper } = require('@harperfast/nextjs');
module.exports = withHarper({
// Any valid Next.js configuration options
});Enables the built-in Harper cache handler. Defaults to false.
export default withHarper(
{
/* Next.js config */
},
{ experimentalHarperCache: true }
);All plugin options are configured in config.yaml under the @harperfast/nextjs key. All options are optional.
Enables Next.js development mode with hot module replacement (HMR). Defaults to false.
Note
Dev mode for Next.js relies on WebSockets. If you encounter an Invalid WebSocket frame: error, disable any other WebSocket services running on the same port.
When enabled, the plugin will look for an existing .next directory and skip the build step. Defaults to false.
Build the Next.js application and then exit (including shutting down Harper). Defaults to false.
Specify a custom HTTP port for the Next.js server. Defaults to the Harper default port (9926).
Specify a custom HTTPS port for the Next.js server. Defaults to the Harper default secure port.
When enabled, the Next.js request handler runs before any other Harper HTTP middleware. Useful for scenarios where Next.js handles authentication directly. Note that enabling this will conflict with Harper's REST API on the same port — consider using a dedicated port to avoid conflicts. Defaults to false.
This custom caching handler is currently a WIP and is actively being developed.
@harperfast/nextjs includes a built-in cache handler for Next.js Incremental Static Regeneration (ISR). Instead of storing cached pages on the file system, cached data is stored in Harper's database, making it available across all nodes in your Harper cluster.
Enable it via the experimentalHarperCache option in withHarper():
export default withHarper(
{
/* Next.js config */
},
{ experimentalHarperCache: true }
);See CONTRIBUTING.md.