Is there any way to share Next.js pages across multiple apps in a Turborepo? #86968
Replies: 2 comments
-
|
Yes, sharing Next.js pages across Turborepo apps is possible, but the constraints you hit are real — and the solution depends on what you are trying to share. The core constraintNext.js APIs ( What works: shared source packages (no build step)For components and utilities that use Next.js APIs, do not bundle the shared package. Export raw TypeScript/TSX source and let each Next.js app transpile it: // packages/common-ui/package.json
{
"name": "@repo/common-ui",
"exports": {
".": "./src/index.ts"
}
}// apps/app1/next.config.ts
const nextConfig = {
transpilePackages: ["@repo/common-ui"],
};With What about TSUP?TSUP is the right choice for utility packages that do not use Next.js APIs (pure logic, types, constants, generic UI with no server-only imports). For anything that touches
Sharing actual pagesNext.js page files ( The practical pattern for near-identical pages across
// packages/common-ui/src/pages/PostsPage.tsx
export async function PostsPage() {
// data fetching, Next.js APIs — all fine here with transpilePackages
}// apps/app1/app/posts/page.tsx
export { PostsPage as default } from "@repo/common-ui/pages/PostsPage";This keeps routing in each app while sharing 100% of the logic. TL;DR
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for details. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I am exploring whether it is possible to share Next.js pages across multiple apps inside a Turborepo.
Context
My repository structure looks like this:
Both
app1andapp2have almost identical modules and functionality. To avoid duplication, I created acommon-uipackage and tried moving reusable pages and components into these packages.Issues I Encountered
However, when I attempted to import Next.js pages or modules from these shared packages, I hit several limitations:
Next.js APIs (cookies, headers, server-only modules) cannot be used inside shared packages.
I received tracing and runtime errors such as: “Cookies can only be used inside a Next.js App Router environment”.
Server Components and Client Components behave inconsistently when located in external workspaces.
Next.js seems to isolate each app at the project root, so external packages lose access to Next.js internals.
The Turborepo was originally created using a ShadCN template, which heavily couples the structure to a single app.
The repository feels heavier than expected and difficult to extend for multi-app sharing.
Additional Context: TSUP Exploration
I also started exploring TSUP to optimise and bundle my shared packages.
However, I am not sure whether TSUP is the appropriate choice in this scenario:
At this stage, I am unsure where TSUP fits into a multi-app Next.js monorepo, especially when the goal is to reuse App Router components.
What I Want to Achieve
app1andapp2.Questions
Is it currently possible to share Next.js pages or App Router modules (layouts, routes, server components) across apps in a Turborepo?
Are there recommended patterns for:
How should TSUP be used (or avoided) in a monorepo where shared packages need to integrate tightly with Next.js?
If anyone has an example repository with such a setup, could you share guidance or best practices?
If recreating the repo from scratch is easier, I am open to step-by-step help.
Any suggestions or sample structures would be greatly appreciated.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions