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

🐛 BUG: top level await breaks next.js with typescript template #7665

Open
beratbayram opened this issue Jan 4, 2025 · 3 comments · May be fixed by #7854
Open

🐛 BUG: top level await breaks next.js with typescript template #7665

beratbayram opened this issue Jan 4, 2025 · 3 comments · May be fixed by #7854
Labels
bug Something that isn't working c3 Relating to C3 (create-cloudflare) package

Comments

@beratbayram
Copy link

beratbayram commented Jan 4, 2025

Which Cloudflare product(s) does this pertain to?

C3 (npm create cloudflare)

What versions are you using?

2.35.1 [C3 - create-cloudflare@latest]

What operating system and version are you using?

Mac Sequoia 15.2

Please provide a link to a minimal reproduction

No response

Describe the Bug

npm create cloudflare@latest . --framework=next

create-cloudflare creates a next.js project with a top-level await in the config file. If typescript is selected, it causes a problem because top-level await can only be used inside a .mts or .mjs file, but the Next config is a .ts file and can't be a .mts file(1). A simple npm run dev causes:

ReferenceError: await is not defined

There is no problem if javascript is selected because next config is a .mjs file.

There is no problem if JavaScript is selected because the Next config is a .mjs file. I have proposed a solution at this comment. But the code block below says awaiting may not be necessary, so maybe we can delete the await and be done with it. In any case, the template engine should not output an invalid configuration.

if (process.env.NODE_ENV === 'development') {
	// `await`ing the call is not necessary but it helps making sure that the setup has succeeded.
	//  If you cannot use top level awaits you could use the following to avoid an unhandled rejection:
	//  `setupDevPlatform().catch(e => console.error(e));`
	await setupDevPlatform();
}

From: https://github.com/cloudflare/next-on-pages/blob/main/internal-packages/next-dev/README.md

If you give me directions, I can open a PR.

Please also see other issues at the next-on-pages repo with the same problem:

cloudflare/next-on-pages#927
cloudflare/next-on-pages#925

Please provide any relevant error logs

No response

@dario-piotrowicz
Copy link
Member

Hi @beratbayram 🙂

Thanks for the issue and sorry for the late reply I just only now noticed it

Given this problem I think C3 should create a next.config.mjs file instead and use jsdocs for types, exactly how you did it here: cloudflare/next-on-pages#925 (comment)

Would you like to open a PR with this change? 🙂

@beratbayram
Copy link
Author

Thank you for your reply and it will be my first open source contribution. So sorry for any mistakes in advance. I can open a PR but it will take week because I don't have access to my work setup due to a family trip. I hope you don't mind.

Also, I am planning to discard the next starter's config and hard code a new .mjs file. It will work but it might break if nextjs team decides to change the file. Maybe we should limit it to some versions.

Other possible solutions:

What do you think @dario-piotrowicz?

@dario-piotrowicz
Copy link
Member

Thank you for your reply and it will be my first open source contribution. So sorry for any mistakes in advance. I can open a PR but it will take week because I don't have access to my work setup due to a family trip. I hope you don't mind.

Also, I am planning to discard the next starter's config and hard code a new .mjs file. It will work but it might break if nextjs team decides to change the file. Maybe we should limit it to some versions.

Other possible solutions:

What do you think @dario-piotrowicz?

Thanks a lot @beratbayram, I am sure you'll do a fantastic job, I'm very excited that you picked Cloudflare for your first contribution 😄

I think we can wait until you're back, unless we start getting a bunch of complains regarding this (hopefully not 🤞)

I think we should just replace the .ts file with an template/hardcoded .mjs one as you suggested, the config file is pretty stable so I don't think Next.js would break it (especially not without a major bump). So I would just keep things simple here 🙂👍 (and we can always fix/re evaluate things later on if needed).

If you need any help understanding the create-cloudflare source code please just do let me know 🙂

@CarmenPopoviciu CarmenPopoviciu moved this from Untriaged to Backlog in workers-sdk Jan 20, 2025
@CarmenPopoviciu CarmenPopoviciu added the c3 Relating to C3 (create-cloudflare) package label Jan 20, 2025
@beratbayram beratbayram linked a pull request Jan 21, 2025 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working c3 Relating to C3 (create-cloudflare) package
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

3 participants