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

Add --node-compat to Pages publish #2541

Closed
wants to merge 2 commits into from

Conversation

WalshyDev
Copy link
Member

@WalshyDev WalshyDev commented Jan 12, 2023

What this PR solves / how to test:

This adds --node-compat support to Pages, this is a common feature request and will allow for the usage of Stripe and other such libs within Functions.

Associated docs issues/PR:
N/A

Author has included the following, where applicable:

  • Tests
  • Changeset

Reviewer has performed the following, where applicable:

  • Checked for inclusion of relevant tests
  • Checked for inclusion of a relevant changeset
  • Checked for creation of associated docs updates
  • Manually pulled down the changes and spot-tested

Fixes #1074

@WalshyDev WalshyDev requested review from a team as code owners January 12, 2023 23:59
@changeset-bot
Copy link

changeset-bot bot commented Jan 12, 2023

🦋 Changeset detected

Latest commit: 2bc6376

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

This PR includes changesets to release 1 package
Name Type
wrangler 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

@GregBrimble
Copy link
Contributor

#2540 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Jan 13, 2023

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/6711485115/npm-package-wrangler-2541

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6711485115/npm-package-wrangler-2541 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6711485115/npm-package-cloudflare-pages-shared-2541

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


wrangler@3.15.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare 3.20231025.0 3.20231025.0
workerd 1.20231025.0 1.20231025.0
workerd --version 1.20231025.0 2023-10-25

|

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

@WalshyDev
Copy link
Member Author

I'd like to hold on this until we see where this new nodejs_compat compatibility flag goes, especially since we don't have an in-dash option for this at the moment. I think we're going to quite quickly be at a point where using this polyfill is more harmful than good.

The nodejs compat flag won't have parity for quite a while. In-dash option is also not a blocker, we've done this many times and it takes no time at all to this to the UI.
Considering we already support this in dev and build, I see no reason to not just add it here. People have wanted it ever since Functions was made. This brings us closer in parity with Workers and for basically 0 cost.

This also means the migration to the new flag is also the same rather than being another thing different.

@codecov
Copy link

codecov bot commented Jan 13, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (4b07385) 75.34% compared to head (bb7f385) 72.97%.
Report is 538 commits behind head on main.

❗ Current head bb7f385 differs from pull request most recent head 2bc6376. Consider uploading reports for the commit 2bc6376 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2541      +/-   ##
==========================================
- Coverage   75.34%   72.97%   -2.37%     
==========================================
  Files         223      156      -67     
  Lines       12341     9748    -2593     
  Branches     3190     2570     -620     
==========================================
- Hits         9298     7114    -2184     
+ Misses       3043     2634     -409     
Files Coverage Δ
packages/wrangler/src/pages/build.ts 85.48% <ø> (+15.36%) ⬆️
packages/wrangler/src/pages/dev.ts 18.94% <ø> (+2.73%) ⬆️
packages/wrangler/src/pages/publish.tsx 65.38% <50.00%> (ø)

... and 188 files with indirect coverage changes

@WalshyDev WalshyDev force-pushed the walshy/add-node-compat-to-pages-publish branch from 894c8c0 to bb7f385 Compare January 13, 2023 14:41
Copy link
Contributor

@JacobMGEvans JacobMGEvans left a comment

Choose a reason for hiding this comment

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

Looks great, would like to see an integration test for this before it goes in, probably should go here: https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/src/__tests__/pages.test.ts 😃

Comment on lines 309 to 313
if (nodeCompat) {
console.warn(
"Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test that runs wrangler pages publish --node-compat

@JacobMGEvans
Copy link
Contributor

I'd like to hold on this until we see where this new nodejs_compat compatibility flag goes, especially since we don't have an in-dash option for this at the moment. I think we're going to quite quickly be at a point where using this polyfill is more harmful than good.

The nodejs compat flag won't have parity for quite a while. In-dash option is also not a blocker, we've done this many times and it takes no time at all to this to the UI. Considering we already support this in dev and build, I see no reason to not just add it here. People have wanted it ever since Functions was made. This brings us closer in parity with Workers and for basically 0 cost.

This also means the migration to the new flag is also the same rather than being another thing different.

I agree with this take on the polyfill --node-compat the runtime compat flag won't directly interfere with this implementation, they just cannot be used together.

@notwedtm
Copy link

Any updates on when this might get merged in?

@jadbox
Copy link

jadbox commented Sep 29, 2023

I'm needing to bump this too.

I'm blocked from going into production with CF pages due to this [#2541] bug. This prevents me from using PG tcp pooling in my Page workers.

The other issue is that I'm unable to test against remote data with Pages workers due to this bug:
#3505

Copy link
Contributor

@GregBrimble GregBrimble left a comment

Choose a reason for hiding this comment

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

Blocking until we've had internal product roadmap discussions.

@lrapoport-cf lrapoport-cf added the pages Relating to Pages label Oct 2, 2023
@WalshyDev WalshyDev force-pushed the walshy/add-node-compat-to-pages-publish branch from bb7f385 to 2bc6376 Compare October 31, 2023 19:22
@GregBrimble
Copy link
Contributor

Rather than accepting this as a arg on deploy, should we instead look at the config of the project in the dashboard and use that so it's all kept in one place? Sort of like how we did D1 when it was in beta and needed the shims?

@WalshyDev
Copy link
Member Author

It's already an arg for Workers, if we did move it we'd also need to change dev behaviour to pull from the project which just diverges us more from Workers.

@GregBrimble
Copy link
Contributor

But we don't yet have it for many other similar props like compat date and flags. Feels like we should solve syncing config separately to adding this node compat feature (so should stay consistent until we've got that bit figured out in a separate effort).

@janpio
Copy link

janpio commented Dec 9, 2023

I can confirm that using the prerelease version mentioned above indeed let's me deploy a project using pg to GitHub Pages that I could not deploy before, as the build was running into weird errors messages (because nodejs_compat just was not the right solution for my problem with pg). Unfortunately the deployment does not seem to work though, requests get a 522 and the deployment shows "Failed" in the web UI.

@WalshyDev Is this supposed to work on the deployed side as well or is this PR here only one side of the coin and there would need to be a change on the platform as well?

Update: Turned out to be the size limit of the worker (>1MB, I was testing on a free account) that just leads to a failed deployment, instead of the normal output you get with Cloudflare Workers.


We would really appreciate if this could be added to the stable version of wrangler, even if undocumented similar to --node-compat for dev. Then it could still be removed or updated later when you figure out the long term strategy around node compatiblity.

@Hebilicious
Copy link

Hi @janpio!
If I understand correctly, you were you able to deploy a project to cloudflare pages with pg has a dependency, and then you can use postgres from cloudflare pages without issues?

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.

This feels like a reasonable addition, given that it is already supported by Pages dev and build commands.

What's left to get this ready to be merged?

@janpio
Copy link

janpio commented Jan 10, 2024

@Hebilicious Yes, this change to the CLI unblocks usage of pg in Cloudflare Pages functions mostly. (It does not work when you also want to use Next.js, but that is a separate problem: cloudflare/next-on-pages#605)

@petebacondarwin
Copy link
Contributor

I think a workaround for now is to use wrangler pages build functions --node-compat build-output-directory=public/_worker.js to generate the Pages Worker and then upload that using wrangler publish public.

Does that work for your use case?

@admah
Copy link
Contributor

admah commented Jan 22, 2024

@WalshyDev it looks like a check failed on a formatting issue. Could you fix that and we should be good to merge?

Copy link
Contributor

@GregBrimble GregBrimble left a comment

Choose a reason for hiding this comment

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

I think we still first need to discuss and make a clear decision on how this impacts the source of truth for this currently dash-only config setting. This, although aligns with Workers in some ways by exposing this CLI arg in the deploy command, misaligns with Pages in others, in that this setting can now be configured completely independently of our source-of-truth: the dashboard.

@janpio
Copy link

janpio commented Feb 1, 2024

@GregBrimble Can node_compat (or --node-compat) actually be configured via the UI/dashboard?

In my (external) understanding node_compat is a build-time flag that influences how the CLI builds the code of your worker (by adding a bunch of polyfills that makes for example pg work), and nodejs_compat is a runtime compatibility flag that influences how the code that has been uploaded is being run on the worker, or what code is available when it runs (like some node APIs in the case of nodejs_compat).

@lrapoport-cf lrapoport-cf added the awaiting Cloudflare response Awaiting response from workers-sdk maintainer team label Feb 7, 2024
SevInf pushed a commit to prisma/ecosystem-tests that referenced this pull request Feb 14, 2024
SevInf added a commit to prisma/ecosystem-tests that referenced this pull request Feb 14, 2024
* Temproarily  disable Worker pages test

See cloudflare/workers-sdk#2541

* Fix things

* Fix finally

* Apply suggestions from code review

Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>

---------

Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
@petebacondarwin
Copy link
Contributor

Since this PR is very unlikely to land as-is (e.g. we actually want to make nodejs compat flag the approach to take in the long run) and this PR has been sitting around for quite some time, we are going to close it for now. If and when we decide to implement the node-compat build time setting for Pages we can reopen or reimplement this.

@janpio
Copy link

janpio commented Feb 19, 2024

What is the recommended workaround in the meantime @petebacondarwin? Right now it seems that pg is not usable with Cloudflare Pages then (as it requires node-compat).

@petebacondarwin
Copy link
Contributor

@janpio - can you try the workaround here?

#2541 (comment)

@janpio
Copy link

janpio commented Feb 19, 2024

Oh sorry, we tried that but seem to have forgotten to report back:

  1. The command there is invalid, I assume the correct order would be wrangler pages functions build --node-compat build-output-directory=public/_worker.js
  2. Then it fails during the publish:
    ▲ [WARNING] `wrangler publish` is deprecated and will be removed in the next major version.
    
      Please use `wrangler deploy` instead, which accepts exactly the same arguments.
    
    
    
    ✘ [ERROR] A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
    
          ```
          compatibility_date = "2024-02-01"
          ```
          Or you could pass it in your terminal as `--compatibility-date 2024-02-01`
      See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.
    

Full setup here: prisma/ecosystem-tests#4504 + https://github.com/prisma/ecosystem-tests/actions/runs/7743032416/job/21113520833?pr=4504#step:8:147

@petebacondarwin
Copy link
Contributor

OK let me reproduce. The error message seems to imply you are only missing --compatibility-date flag?

@janpio
Copy link

janpio commented Feb 19, 2024

Indeed, seems I got lost in the different commands before.

Although I now realized that wrangler publish publishes a Worker and not Pages. What is required to build the whole Pages "combination" and deploy it? I got lost when trying to use wrangler pages publish with something I previously built with wrangler pages functions build.

@petebacondarwin
Copy link
Contributor

Yeah sorry about this. It is all a bit messy right now. Trying to work out if there is a magical combination of commands that we could use.

@petebacondarwin
Copy link
Contributor

Give this a go: prisma/ecosystem-tests#4574

@millsp
Copy link

millsp commented Feb 19, 2024

Coming here to report that Pete's workaround is working well 🚀. Thanks!

@Hebilicious
Copy link

Hebilicious commented Feb 20, 2024

@petebacondarwin Is there any chance that the workaround could be documented in the Pages docs, or at least here ?
I believe using pg in cloudflare pages is quite common.

This is what I could gather from the Prisma PR :

echo "Temporary disabled, because wrangler does not support "--node-compat" flag for pages commands yet. See https://github.com/cloudflare/workers-sdk/pull/2541"
pnpm install

# First build the functions using the `--node-compat` flag into the `_worker.js` directory
# (Note that the functions are in the non-standard `fns` directory so that they are not confused with the output `_worker.js` directory.)
# Make sure build folder does not exist
rm -rf build
# Build pages function to build/_worker.js
pnpm wrangler pages functions build fns --node-compat --outdir build/_worker.js
# Copy index.html to build folder
cp index.html build/index.html
# Now deploy the _worker.js alongside the index.html asset to Pages
pnpm wrangler pages deploy ./build

@delay
Copy link

delay commented Feb 26, 2024

Maybe you should consider updating your documentation because as far as I can tell this still isn't working with pg and Pages. The workaround I have found is to use the postgres npm package instead of pg.

@petebacondarwin
Copy link
Contributor

Maybe you should consider updating your documentation because as far as I can tell this still isn't working with pg and Pages. The workaround I have found is to use the postgres npm package instead of pg.

We should update that to make it clear that the tutorial is for Workers not Pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Cloudflare response Awaiting response from workers-sdk maintainer team pages Relating to Pages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Support --node-compat in wrangler pages build