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

Next.js Integration Guide #316

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Next.js Integration Guide #316

wants to merge 13 commits into from

Conversation

chuck-dbos
Copy link
Contributor

No description provided.

@chuck-dbos chuck-dbos marked this pull request as draft March 6, 2025 22:15
@maxdml maxdml marked this pull request as ready for review March 6, 2025 22:21
# Introduction

## Why Add DBOS To a Next.js Application?
[Next.js](https://nextjs.org/) is a solid choice for full-stack applications, but popular Next.js hosting options focus on serverless, CDN-heavy deployments that do not allow long-running tasks or other “heavy lifting” in the server. By adding DBOS Transact, and running in a suitable hosting environment (such as [DBOS Cloud](https://www.dbos.dev/dbos-cloud)), the following additional features are available:
Copy link
Member

Choose a reason for hiding this comment

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

I think it'll look much cleaner if we keep this list short. We can also point to the "Why DBOS" page.

  • Reliable long-running tasks through lightweight durable execution. (point to workflow tutorial)
  • Built-in observability and debugging support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The "Why DBOS" page doesn't really say "why DBOS"... but I made this a lot shorter.

- Marking Modules as External – Configuring webpack to treat DBOS modules and files as external, ensuring they are not bundled.
- Using globalThis – Accessing code and data through [`globalThis`](https://ja-visser.medium.com/globalreferences-in-nodejs-75f095962596) instead of import, effectively bypassing the bundler.

### webpack Configuration in `next.config.ts`
Copy link
Member

Choose a reason for hiding this comment

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

Maybe explicitly call the webpack config "Option 1: ..." and then the globalThis "Option 2:...". It's unclear whether a user needs both, or either one of those would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One, the other, both, or maybe options not listed here...

Copy link
Member

Choose a reason for hiding this comment

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

Let's try to be more prescriptive. I read this section and frankly I don't know what I need to do. Do I have to configure webpack? Do I have to do it just for the dbos-sdk library code or for other code as well? (can we fix Transact so it survives bundling automatically?) When do I need globalThis? Is it for any DBOS-decorated class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Transact is not broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to make more prescriptive.

If your project does not currently have a `server.ts` file or similar, or if you are using the default `server.js` provided by Next.js, the first step is to create one. This file can be created in the top level of your project, inside `src/`, or in any sensible place within your app structure.

The DBOS demo apps contain example `server.ts` files to copy:
- [DBOS Next.js Template](https://github.com/dbos-inc/dbos-demo-apps/blob/main/typescript/dbos-nextjs-starter/src/server.ts): This project uses a basic `server.ts` file.
Copy link
Member

@kraftp kraftp Mar 7, 2025

Choose a reason for hiding this comment

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

I would include a simple copy-pasteable server.ts in a details block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

K

- [DBOS Task Scheduler](https://github.com/dbos-inc/dbos-demo-apps/blob/main/typescript/nextjs-calendar/src/server.ts): This project uses a more sophisticated `server.ts` file with WebSocket support, etc.

### Compilation Settings For DBOS Code
Using DBOS with Next.js requires some TypeScript compiler settings that do not match Next.js defaults. These can either be added to your existing `tsconfig.json`, or if they are incompatible with the existing `tsconfig.json` settings, a separate `tsconfig.server.json` file can be created for use with the server builds.
Copy link
Member

@kraftp kraftp Mar 7, 2025

Choose a reason for hiding this comment

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

This could be softened a bit, right now it sounds scary ("uncommon settings") when it's really quite mild.

By default, `DBOS.launch()` will read [`dbos-config.yaml`](../typescript/reference/configuration) to get key information, such as database settings. To configure DBOS programatically, see [`DBOS.setConfig`](../typescript/reference/transactapi/dbos-class#setting-the-application-configuration).

## Calling DBOS Code From Next.js
The DBOS Transact library and application your workflow functions are available in all server-side request handling, including page loads, server actions, and route handlers.
Copy link
Member

Choose a reason for hiding this comment

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

Missing some words?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better yet, it has extra. (Good catch.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants