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

Migrate to nextjs app router #695

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

Conversation

essential-breads
Copy link

Updates the project starter to use the latest version of Nextjs with app router and no src directory. Removes the js project option. Closes
#583
#400

@essential-breads essential-breads marked this pull request as ready for review September 19, 2024 17:29
Copy link
Contributor

@45930 45930 left a comment

Choose a reason for hiding this comment

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

The point of using app router is to enable server-side rendering right? Do users need to be aware of which routes and components use SSR and avoid o1js in them?

'--import-alias "@/*"',
'--no-app',
'--app',
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add this change to the changelog?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1


export default function Home() {
useEffect(() => {
(async () => {
const { Mina, PublicKey } = await import('o1js');
const { Add } = await import('../../../contracts/build/src/');
const { Add } = await import('../../contracts/build/src/');

// Update this to use the address (public key) for your zkApp account.
// To try it out, you can try this address for an example "Add" smart contract that we've deployed to
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you test if uncommenting this code works with your changes?

Copy link
Author

@essential-breads essential-breads Sep 19, 2024

Choose a reason for hiding this comment

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

The code doesn't error, but it's also not really doing anything. Looking at this more closely it seems like that zkapp address might be out of date. Maybe we should either remove the example code or update it to really interact with the network. What do you think?

const { Mina, PublicKey,fetchAccount } = await import('o1js');  
const { Add } = await import('../../contracts/build/src/');  
// connect Mina to testnet
const Network = Mina.Network(  
    'https://api.minascan.io/node/devnet/v1/graphql'  
);  
Mina.setActiveInstance(Network);  
// address of deployed Add contract on testnet https://minascan.io/devnet/account/B62qnTDEeYtBHBePA4yhCt4TCgDtA4L2CGvK7PirbJyX4pKH8bmtWe5
const zkAppAddress = `B62qnTDEeYtBHBePA4yhCt4TCgDtA4L2CGvK7PirbJyX4pKH8bmtWe5`;  
await fetchAccount({publicKey: zkAppAddress});  
const zkApp = new Add(PublicKey.fromBase58(zkAppAddress));  
console.log("Current num: ", zkApp.num.get().toString());

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, sounds like a different PR though 👍

We can reconsider what the right format is for demoing o1js in a boilerplate app.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The contract is currently deployed on devnet. https://minascan.io/devnet/account/B62qnTDEeYtBHBePA4yhCt4TCgDtA4L2CGvK7PirbJyX4pKH8bmtWe5?type=zk-acc

This example code was originally added to demonstrate how to import a contract into a UI. The goal was to eventually add further interactions in this example boilerplate.

Copy link
Author

Choose a reason for hiding this comment

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

B62qnTDEeYtBHBePA4yhCt4TCgDtA4L2CGvK7PirbJyX4pKH8bmtWe5 is the one that I deployed during testing, I will replace the old address with this one in all of the cli files!

@essential-breads
Copy link
Author

The point of using app router is to enable server-side rendering right? Do users need to be aware of which routes and components use SSR and avoid o1js in them?

One of the reasons Next moved to app router was to better support React's server components. Info explaining usage with server components may be well suited to a Next.js specific section in the docs where we also call out things like loading the SDK in useEffect. The user will not be able to call useEffect in a server component but could attempt to load the SDK as a top level import and run into issues if we don't guide them on best practices there.

Copy link
Collaborator

@ymekuria ymekuria left a comment

Choose a reason for hiding this comment

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

Nice work! I cloned your repo and was able to successfully generate a project using the app router. I'm approving in advance, but could you change the copy on the UI landing page to match the updated app router file path?

Get started by editing
<code className={styles.code}> src/pages/index.js</code> or <code className={styles.code}> src/pages/index.tsx</code>

'--import-alias "@/*"',
'--no-app',
'--app',
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1


export default function Home() {
useEffect(() => {
(async () => {
const { Mina, PublicKey } = await import('o1js');
const { Add } = await import('../../../contracts/build/src/');
const { Add } = await import('../../contracts/build/src/');

// Update this to use the address (public key) for your zkApp account.
// To try it out, you can try this address for an example "Add" smart contract that we've deployed to
Copy link
Collaborator

Choose a reason for hiding this comment

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

The contract is currently deployed on devnet. https://minascan.io/devnet/account/B62qnTDEeYtBHBePA4yhCt4TCgDtA4L2CGvK7PirbJyX4pKH8bmtWe5?type=zk-acc

This example code was originally added to demonstrate how to import a contract into a UI. The goal was to eventually add further interactions in this example boilerplate.

Copy link

codecov bot commented Sep 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (86e13f7) to head (077e45b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #695   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines         1491      1475   -16     
  Branches       312       300   -12     
=========================================
- Hits          1491      1475   -16     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ymekuria
Copy link
Collaborator

The point of using app router is to enable server-side rendering right? Do users need to be aware of which routes and components use SSR and avoid o1js in them?

Vercel is also pushing the app router as the new standard for NextJS. A user can chose only to use client side components with the app router if they want.

fs.writeFileSync(path.join('ui', 'next.config.mjs'), newNextConfig);

const indexFileName = useTypescript ? 'index.tsx' : 'index.js';
const pageFileName = 'page.tsx';

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like there are some legacy create-next-app boilerplate styles and fonts leftover in the app folder when a project is generated. I think it is a good idea to remove these so they do not conflict with the other assets. You could empty the app directory with something like fs.emptyDirSync or similar before adding the customNextPage and any other files to the directory.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, cleared out the whole directory

);

// Removes create-next-app assets
fs.emptyDirSync(path.join('ui', 'public'));
if (fs.existsSync(path.join('ui', 'app', 'favicon.ico'))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar comment to above.
It looks like there are some legacy create-next-app boilerplate styles and fonts leftover in the app folder when a project is generated. You could empty the entire app directory instead of just removing the favicon here with something like fs.emptyDirSync or similar before adding the customNextPage and any other files to the directory.

'utf8'
);

appFile = appFile.replace(
pageFile = pageFile.replace(
'export default function',
`import './reactCOIServiceWorker';

export default function`
Copy link
Collaborator

Choose a reason for hiding this comment

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

@essential-breads were you able to test if the deploy to GitHub Pages flow works correctly with the appRouter changes? It might be out of scope for this PR, but I will add an issue so we don't forget to verify that everything works.

Copy link
Author

Choose a reason for hiding this comment

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

Yep GH pages is fully tested!

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