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

[create-cloudflare] next.js with ts fix #7854

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

Conversation

beratbayram
Copy link

@beratbayram beratbayram commented Jan 21, 2025

Fixes #7665.

WHAT: It changes the output of next.js with TS template.
WHY: Basic templating is broken.
HOW: Existing customers are not affected. New customers will have a better experience.

Related bug only appears when nextjs is used with TS because C3 generates a top-level await statement inside a .ts file which is illegal. So this PR doesn't change how JS version is generated but creates a new next.config.mjs file with new hardcoded content for TS version specifically.

Only TS version is hardcoded to minimize the impact of possible config file change by the next.js team but we can discuss to do that to JS version as well for simplicity.

Other possible solutions are discussed at the linked issue but to summarise:

  • .mts file type can't be used because next config can't be a .mts file(1).
  • Async config and transpilation is too complex for this simple config file.

  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because: already written
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: Changes the output not the usage

I've run tests and two fails as follows but I don't believe they are related to my changes.

image image

@beratbayram beratbayram requested review from a team as code owners January 21, 2025 21:20
Copy link

changeset-bot bot commented Jan 21, 2025

🦋 Changeset detected

Latest commit: a577775

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@beratbayram beratbayram changed the title resolves #925 resolves #7665 Jan 21, 2025
@beratbayram
Copy link
Author

beratbayram commented Jan 21, 2025

dario-piotrowicz (asking maintainer to run E2E tests separately)

@beratbayram beratbayram changed the title resolves #7665 [create-cloudflare] next.js with ts fix Jan 21, 2025
Copy link
Contributor

github-actions bot commented Jan 21, 2025

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-wrangler-7854

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7854/npm-package-wrangler-7854

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-wrangler-7854 dev path/to/script.js
Additional artifacts:

cloudflare-workers-bindings-extension:

wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-workers-bindings-extension-7854 -O ./cloudflare-workers-bindings-extension.0.0.0-v8e853b74c.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v8e853b74c.vsix

create-cloudflare:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-create-cloudflare-7854 --no-auto-update

@cloudflare/kv-asset-handler:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-kv-asset-handler-7854

miniflare:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-miniflare-7854

@cloudflare/pages-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-pages-shared-7854

@cloudflare/unenv-preset:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-unenv-preset-7854

@cloudflare/vite-plugin:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-vite-plugin-7854

@cloudflare/vitest-pool-workers:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-vitest-pool-workers-7854

@cloudflare/workers-editor-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-workers-editor-shared-7854

@cloudflare/workers-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-workers-shared-7854

@cloudflare/workflows-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12924425658/npm-package-cloudflare-workflows-shared-7854

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.105.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20241230.2
workerd 1.20241230.0 1.20241230.0
workerd --version 1.20241230.0 2024-12-30

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

// use bindings during local development (when running the application with \`next dev\`).
// For more information see:
// https://github.com/cloudflare/next-on-pages/blob/main/internal-packages/next-dev/README.md
if (process.env.NODE_ENV === 'development') {
Copy link
Contributor

Choose a reason for hiding this comment

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

@dario-piotrowicz do we need (process.env.NODE_ENV === 'development') here? (or could it be inside setupDevPlatform ?

(The code here is only moved from l42 so not directly related to the PR)

Copy link
Member

Choose a reason for hiding this comment

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

This check is totally unnecessary, moving it inside setupDevPlatform wouldn't actually accomplish anything

it's here to try to make it extra clear to the developer that this function is only needed in development, in reality this only has any effect during development anyways

I'm happy to get rid of it if you want 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

This check is totally unnecessary

Then I would drop it.

And in next-on-pages as well

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

I took a quick look here but I don't really think the changes are optimal, there should be no reason to need both a TS and JS version of updateNextXConfig.

One thing that look weird is that we do not pass --js or --ts to create-next-app

await runFrameworkGenerator(ctx, [projectName]);

@beratbayram
Copy link
Author

I took a quick look here but I don't really think the changes are optimal, there should be no reason to need both a TS and JS version of updateNextXConfig.

JS version only prepends some code to existing config while TS version removes the existing one completely and creates a new hardcoded one from scratch. I can make JS version also behave like TS version but in case of a possible config change by next.js team, both versions will be affected instead of only the TS version.

One thing that look weird is that we do not pass --js or --ts to create-next-app

await runFrameworkGenerator(ctx, [projectName]);

We don't need that, user decides that during next-cli prompt

@vicb
Copy link
Contributor

vicb commented Jan 22, 2025

We don't need that, user decides that during next-cli prompt

Thanks for the clarification here.

JS version only prepends some code to existing config while TS version removes the existing one completely and creates a new hardcoded one from scratch. I can make JS version also behave like TS version but in case of a possible config change by next.js team, both versions will be affected instead of only the TS version.

Could you expand on why the TS config would need to be fully replaced? (maybe add the generated TS config + explanation to the PR description). Thanks!

@beratbayram
Copy link
Author

Thanks for the clarification here.

No problem

Could you expand on why the TS config would need to be fully replaced? (maybe add the generated TS config + explanation to the PR description). Thanks!

I've just edited the description. Please also check the conversation at the linked issue.

@vicb
Copy link
Contributor

vicb commented Jan 22, 2025

@dario-piotrowicz
Copy link
Member

because C3 generates an await statement inside a .ts file which is illegal

I think we should drop the await, see:

cloudflare/next-on-pages@2cd4c3c/internal-packages/next-dev/src/index.ts#L6-L13

cloudflare/next-on-pages@2cd4c3c/internal-packages/next-dev/README.md?plain=1#L56-L59

@dario-piotrowicz what do you think?

yeah I forgot about the awaiting not being actually necessary, yes probably removing it would be the simplest thing, I feel like awaiting the function provides a nice safety net (since we've seen that not awaiting the function is totally fine, but there is no hard guarantee about that, in theory it could no longer be true if Next.js changed how they run their dev server etc...), but since now it complicates the C3 logic here I think we might just get rid of it

@beratbayram
Copy link
Author

beratbayram commented Jan 22, 2025

I've reverted all of my previous work.

yeah I forgot about the awaiting not being actually necessary, yes probably removing it would be the simplest thing,

Removed await and added .catch(console.error)

it's here to try to make it extra clear to the developer that this function is only needed in development

Removed the if and improved the comment to emphasize that.

I hope it is OK now.

@vicb @dario-piotrowicz

@vicb vicb added the e2e Run e2e tests on a PR label Jan 22, 2025
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR and the updates!

I triggered the e2e

Co-authored-by: Victor Berchet <victor@suumit.com>
@beratbayram
Copy link
Author

Thanks for your directions and your patience. It was fun to work on this.

@vicb
Copy link
Contributor

vicb commented Jan 23, 2025

@beratbayram it looks like the formatting is wrong, could you please run pnpm fix on the change?

@beratbayram
Copy link
Author

@beratbayram it looks like the formatting is wrong, could you please run pnpm fix on the change?

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

🐛 BUG: top level await breaks next.js with typescript template
3 participants