-
-
Notifications
You must be signed in to change notification settings - Fork 246
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: run Toolpad apps as React server components #3125
Draft
Janpot
wants to merge
26
commits into
mui:master
Choose a base branch
from
Janpot:nextjs-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b023ac9
WIP
Janpot 97fe280
remove
Janpot 9aefb49
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot e279572
make server work
Janpot aa364fa
bindings
Janpot 7e31453
Update cli.mjs
Janpot f3f9708
apphost
Janpot f80b0e6
lkej
Janpot 02aa79e
Update ToolpadApp.spec.tsx
Janpot 2804ffb
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot b06c42b
WIP
Janpot b357bda
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot d9227cc
fwrw
Janpot 0c034b2
Update next.tsx
Janpot d270b22
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot 409e727
WIP
Janpot a064161
rtyt
Janpot 8a096f3
Update functionsRuntime.ts
Janpot 0bd13b4
frwfr
Janpot 1c10530
ferger
Janpot c4576fd
browserruntime
Janpot 24ea712
dewfrew
Janpot 203a134
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot 00e59c6
Update page.tsx
Janpot e51424b
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot b20f090
Merge remote-tracking branch 'upstream/master' into nextjs-integration
Janpot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/custom-server-nextjs/app/my-toolpad/[[...path]]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as React from 'react'; | ||
import { ToolpadApp } from '@mui/toolpad/next'; | ||
|
||
export default function ToolpadPage() { | ||
return <ToolpadApp base="/my-next-app/my-toolpad" dir="./toolpad" />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
/** @type {import('next').NextConfig} */ | ||
export default { | ||
webpack: (config) => { | ||
config.externals.push({ | ||
fsevents: 'commonjs fsevents', | ||
chokidar: 'commonjs chokidar', | ||
}); | ||
config.resolve.alias['@mui/icons-material'] = ['@mui/icons-material/esm']; | ||
return config; | ||
}, | ||
basePath: '/my-next-app', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
apiVersion: v1 | ||
kind: application | ||
spec: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
examples/custom-server-nextjs/toolpad/resources/functions2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Toolpad handlers file. | ||
*/ | ||
|
||
export default async function handler(message: string) { | ||
return `Hello ${message}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import ToolpadApp, { ToolpadAppProps } from '../runtime/ToolpadApp'; | ||
import { AppHostContext, AppHost } from '../runtime/AppHostContext'; | ||
|
||
const appContext: AppHost = { | ||
isPreview: false, | ||
isCustomServer: true, | ||
}; | ||
|
||
export default function ToolpadAppClient(props: ToolpadAppProps) { | ||
return ( | ||
<AppHostContext.Provider value={appContext}> | ||
<ToolpadApp {...props} /> | ||
</AppHostContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use server'; | ||
|
||
import * as React from 'react'; | ||
import ToolpadAppClient from './next-client'; | ||
import { loadDom } from '../server/localMode'; | ||
import createRuntimeState from '../runtime/createRuntimeState'; | ||
|
||
export interface ToolpadAppServerProps { | ||
base: string; | ||
dir: string; | ||
} | ||
|
||
export async function ToolpadApp({ base, dir = './toolpad' }: ToolpadAppServerProps) { | ||
const dom = await loadDom(dir); | ||
const initialState = createRuntimeState({ dom }); | ||
|
||
return <ToolpadAppClient basename={base} state={initialState} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { | ||
Stack, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does the app decide which page to run? If it's the URL path concerning
base
, is it possible to abstract the URL from the app and modify the props as follows?It could help us dynamically render any page without worrying about the paths, giving user more control IMO. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense. This is somewhat on hold because it depends on a bunch of low-level work that needs to happen first. We'll also have to abstract Toolpad routing to be able to plug in the Next.js router.
Another option I'm thinking of is to expose another component that renders a single page instead of the whole app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be even more useful for my use case where I have a backend to pull the MDX file and I can transform it on the fly render the page without even shell. If we can pass
components
andresources
as well, the applications can utilize Toolpad's application model to render the generated pages in our app.