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

Update Next.js template to support bindings #4688

Merged
merged 12 commits into from
Jan 16, 2024
Merged

Conversation

irvinebroque
Copy link
Contributor

@irvinebroque irvinebroque commented Jan 2, 2024

What

Updates starter template that running npm create cloudflare and selecting Next.js creates, to make use of bindings proxy, as implemented in next-on-pages.

Why

Provides a starting point that shows how to work with bindings in local dev, even when local dev server is in Node.js.

Before merging

refs irvinebroque/next-bindings-test#1
refs #4523

Copy link

changeset-bot bot commented Jan 2, 2024

⚠️ No Changeset found

Latest commit: 9621aee

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link
Contributor

github-actions bot commented Jan 2, 2024

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/7534950857/npm-package-wrangler-4688

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7534950857/npm-package-wrangler-4688 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7534950857/npm-package-miniflare-4688
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7534950857/npm-package-cloudflare-pages-shared-4688
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7534950857/npm-package-create-cloudflare-4688 --no-auto-update

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


wrangler@3.22.4 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20231218.1
workerd 1.20231218.0 1.20231218.0
workerd --version 1.20231218.0 2023-12-18

|

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

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8d6b3ee) 75.63% compared to head (9621aee) 75.70%.
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4688      +/-   ##
==========================================
+ Coverage   75.63%   75.70%   +0.06%     
==========================================
  Files         243      243              
  Lines       13210    13222      +12     
  Branches     3393     3396       +3     
==========================================
+ Hits         9992    10010      +18     
+ Misses       3218     3212       -6     

see 7 files with indirect coverage changes

@dario-piotrowicz
Copy link
Member

@irvinebroque another thing that needs to be updated in c3 for Next.js (which I was planning to do as soon as the next-dev submodule isn't experimental anymore) is to update the package.json scripts, we should updated them to something along these lines:

    "pages:build": "npx @cloudflare/next-on-pages",
    "pages:deploy": "npm run pages:build && wrangler pages deploy .vercel/output/static",
-   "pages:watch": "npx @cloudflare/next-on-pages --watch",
-   "pages:dev": "npx wrangler pages dev .vercel/output/static --compatibility-date=2023-12-18 --compatibility-flag=nodejs_compat"
+   "pages:preview": "npm run pages:build && npx wrangler pages dev .vercel/output/static --compatibility-date=2023-12-18 --compatibility-flag=nodejs_compat"

so that then developers can follow the recommended workflow and:

  • develop locally using dev,
  • preview using pages:preview
  • and deploy using pages:deploy

As I said I was planning to do this as soon as next-dev's ready, so I am really happy to do it in a followup PR, or maybe the change could be included here, whatever you prefer 🙂 (in any case I thought you'd want to be aware of this)

@admah
Copy link
Contributor

admah commented Jan 4, 2024

@dario-piotrowicz is there an ETA on when then module might not be experimental? Is it possible to ship while it's experimental?

@dario-piotrowicz
Copy link
Member

dario-piotrowicz commented Jan 4, 2024

@dario-piotrowicz is there an ETA on when then module might not be experimental? Is it possible to ship while it's experimental?

Yeah it'd be possible to ship this while the package is experimental, but anyways as ETA for making the module non-experimental I'd say in the next few days, next week at the very very latest, it's just a matter of merging this PR: cloudflare/next-on-pages#612 and making a next-on-pages release 🙂

@dario-piotrowicz
Copy link
Member

@irvinebroque @admah the next-on-pages next-dev sub-module is no longer experimental 🙂👍

Comment on lines +488 to +489
// Note: this simple implementation doesn't handle tsconfigs containing comments
// (as it is not needed for the existing use cases)
Copy link
Member

Choose a reason for hiding this comment

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

I implemented this quick solution just to get things working with Next.js, before merging I should explore if we can also, without too much complications, handle jsonc tsconfigs

Copy link
Member

Choose a reason for hiding this comment

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

Updating jsonc files doesn't seem very simple, I can see that in wrangler it was already considered not worth it:

// We don't update the tsconfig.json because
// it could be complicated in existing projects
// and we don't want to break them. Instead, we simply
// tell the user that they need to update their tsconfig.json

Here as well a generic solution might end up too complex and not worth it...

I did try to experiment a bit with json5 here but mixed success (the tsconfig can get parsed by during the parsing the comments are lost)

I think would keep this as is for now (since currently it is good enough for Next.js) and improve things later if need be 🤔

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

I pulled this PR down and used it to create a Next.js application locally.
I uncommented the various KV binding bits and added to the pages:preview script.
Then npm run dev and npm run pages:preview all work as expected.
I can also see that the typings are working as expected!

Nice!

@petebacondarwin petebacondarwin merged commit fed5427 into main Jan 16, 2024
24 checks passed
@petebacondarwin petebacondarwin deleted the bib/next-template branch January 16, 2024 16:13
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.

4 participants