withastro / adapters

Home for Astro's core maintained adapters
47 stars 26 forks source link

Cloudflare v10 the final `_workers.js` folder/file bundle is too large #218

Closed dallyh closed 2 months ago

dallyh commented 3 months ago

Astro Info

Astro                    v4.5.12
Node                     v20.12.0
System                   Windows (x64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/react

Describe the Bug

Hello, one thing that just came up. I'm using the <Code/> component from Astro to highlight code. In previous version (9.2.0) there were no issues, everything worked great. However when I use this component in v10 of the adapter, the _worker.js folder size and final bundle size exceeds the free limit (1MB) and on my production site, the bundle with all of the additional scripts exceeds the Workers paid limit (10MB). The _workers.js folder has around 7.8MBs. Compared to worker.js from previous version which had only a few kilobytes (about 100Kb), this is a huge bump in taken space. This looks like there is something wrong with the bundling, as there are many additional generated chunks which all contain CSS and or things for the <Code/> component.

Log from build output with all of the chunks:

21:59:20.314    Found _worker.js in output directory. Uploading.
21:59:21.378    Attaching additional modules:
21:59:21.379    - _noop-middleware.mjs (esm)
21:59:21.379    - chunks/abap_pCqau4N2.mjs (esm)
21:59:21.379    - chunks/actionscript-3_NzPKopoz.mjs (esm)
21:59:21.379    - chunks/ada_QnHoGYVp.mjs (esm)
21:59:21.379    - chunks/andromeeda_Cv_6fglG.mjs (esm)
21:59:21.379    - chunks/angular-html_Clkub1oT.mjs (esm)
21:59:21.380    - chunks/angular-ts_PP5EF8yR.mjs (esm)
21:59:21.380    - chunks/apache_DYREHYRy.mjs (esm)
21:59:21.380    - chunks/apex_BYHOdJII.mjs (esm)
21:59:21.380    - chunks/apl_DOIyJ4U3.mjs (esm)
21:59:21.380    - chunks/applescript_CS-sM12c.mjs (esm)
21:59:21.380    - chunks/ara_BgPe7cwx.mjs (esm)
21:59:21.380    - chunks/asm_BY9_7m6i.mjs (esm)
21:59:21.381    - chunks/astro/assets-service_EciEFaPb.mjs (esm)
21:59:21.381    - chunks/astro_Bf3-gfiJ.mjs (esm)
21:59:21.381    - chunks/astro_DhQrrqM1.mjs (esm)
21:59:21.381    - chunks/aurora-x_mKJZZF4V.mjs (esm)
21:59:21.381    - chunks/awk_i7A1tVsF.mjs (esm)
21:59:21.381    - chunks/ayu-dark_B_sIwDAD.mjs (esm)
21:59:21.381    - chunks/ballerina_BR1NG9JZ.mjs (esm)
21:59:21.381    - chunks/bat_fmRRQuH-.mjs (esm)
21:59:21.381    - chunks/beancount_enk-0gIg.mjs (esm)
21:59:21.382    - chunks/berry_Fh4VDIWS.mjs (esm)
21:59:21.382    - chunks/bibtex_BHqGw8DL.mjs (esm)
21:59:21.382    - chunks/bicep_B7YkHraM.mjs (esm)
21:59:21.382    - chunks/blade_Chk_4t8Z.mjs (esm)
21:59:21.382    - chunks/c_DEKMKJ1W.mjs (esm)
21:59:21.382    - chunks/cadence_BoFYeDx9.mjs (esm)
21:59:21.382    - chunks/catppuccin-frappe_sF6xF8jx.mjs (esm)
21:59:21.382    - chunks/catppuccin-latte_DBmoFhwj.mjs (esm)
21:59:21.382    - chunks/catppuccin-macchiato_C4UVs1BY.mjs (esm)
21:59:21.383    - chunks/catppuccin-mocha_DOw-ZrGS.mjs (esm)
21:59:21.383    - chunks/clarity_Dly4j6FF.mjs (esm)
21:59:21.383    - chunks/clojure_DntiU-jS.mjs (esm)
21:59:21.383    - chunks/cmake_DeIoDqRR.mjs (esm)
21:59:21.383    - chunks/cobol_etfGZ00D.mjs (esm)
21:59:21.383    - chunks/codeql_CPJdbXK4.mjs (esm)
21:59:21.384    - chunks/coffee_BaBsvjhK.mjs (esm)
21:59:21.384    - chunks/cpp_C0oHDn0r.mjs (esm)
21:59:21.384    - chunks/crystal_D5MhxC36.mjs (esm)
21:59:21.384    - chunks/csharp_BFFzdsoE.mjs (esm)
21:59:21.384    - chunks/css_BOM__s5K.mjs (esm)
21:59:21.384    - chunks/csv_-H6_jlwH.mjs (esm)
21:59:21.384    - chunks/cue_B3w6dSIO.mjs (esm)
21:59:21.384    - chunks/cypher_C-hu9qKX.mjs (esm)
21:59:21.385    - chunks/d_Jb-trg65.mjs (esm)
21:59:21.385    - chunks/dark-plus_cSOCrd8C.mjs (esm)
21:59:21.385    - chunks/dart_CF6UpCus.mjs (esm)
21:59:21.385    - chunks/dax_C47u4J9e.mjs (esm)
21:59:21.385    - chunks/diff_kuOFchSj.mjs (esm)
21:59:21.385    - chunks/docker_C_OKR8Im.mjs (esm)
21:59:21.385    - chunks/dracula-soft_DILpI92C.mjs (esm)
21:59:21.385    - chunks/dracula_Vdl8KPpY.mjs (esm)
21:59:21.386    - chunks/dream-maker_CXtWW4_Q.mjs (esm)
21:59:21.386    - chunks/elixir_B7mkhK4E.mjs (esm)
21:59:21.386    - chunks/elm_DpfRj0Nx.mjs (esm)
21:59:21.386    - chunks/erb_BbiZ94yj.mjs (esm)
21:59:21.386    - chunks/erlang_CR00i1HI.mjs (esm)
21:59:21.386    - chunks/fish_B5NZKTZ-.mjs (esm)
21:59:21.386    - chunks/fortran-fixed-form_DDURpJuc.mjs (esm)
21:59:21.386    - chunks/fortran-free-form_CjTUzTfL.mjs (esm)
21:59:21.387    - chunks/fsharp_B1ed7f4T.mjs (esm)
21:59:21.387    - chunks/gdresource_BDT0CebS.mjs (esm)
21:59:21.387    - chunks/gdscript_Cdf99qD7.mjs (esm)
21:59:21.387    - chunks/gdshader_Cjjby8SF.mjs (esm)
21:59:21.387    - chunks/generic_B-u1lJbe.mjs (esm)
21:59:21.387    - chunks/gherkin_CwU-tkX8.mjs (esm)
21:59:21.387    - chunks/git-commit_C7BT_dgu.mjs (esm)
21:59:21.387    - chunks/git-rebase_BmVpvGZ-.mjs (esm)
21:59:21.388    - chunks/github-dark-default_B8P_lb0a.mjs (esm)
21:59:21.388    - chunks/github-dark-dimmed_ChlVXoxw.mjs (esm)
21:59:21.388    - chunks/github-dark_BdFUFoTk.mjs (esm)
21:59:21.388    - chunks/github-light-default_-V-0OqaJ.mjs (esm)
21:59:21.388    - chunks/github-light_BDbYph2i.mjs (esm)
21:59:21.388    - chunks/gleam_DedD5j3T.mjs (esm)
21:59:21.388    - chunks/glimmer-js_CUVtYB50.mjs (esm)
21:59:21.388    - chunks/glimmer-ts_DVOAnqMo.mjs (esm)
21:59:21.388    - chunks/glsl_B35esJ4Y.mjs (esm)
21:59:21.389    - chunks/gnuplot_ar4_nrdN.mjs (esm)
21:59:21.389    - chunks/go_DLLSwo-H.mjs (esm)
21:59:21.389    - chunks/graphql_51Hx7E7f.mjs (esm)
21:59:21.389    - chunks/groovy_DMC2U9dZ.mjs (esm)
21:59:21.389    - chunks/hack_BJboQDvz.mjs (esm)
21:59:21.389    - chunks/haml_dCjYYPPD.mjs (esm)
21:59:21.389    - chunks/handlebars_Dl-dP7_U.mjs (esm)
21:59:21.389    - chunks/haskell_I-15ZybP.mjs (esm)
21:59:21.390    - chunks/hcl_Bvq2BLBr.mjs (esm)
21:59:21.390    - chunks/hjson_2EFv5bno.mjs (esm)
21:59:21.390    - chunks/hlsl_5wHJrTpd.mjs (esm)
21:59:21.390    - chunks/houston_TNaFZrz2.mjs (esm)
21:59:21.390    - chunks/html-derivative_BLoGRPUb.mjs (esm)
21:59:21.390    - chunks/html_BQ75P8SH.mjs (esm)
21:59:21.390    - chunks/http_C93cuPgN.mjs (esm)
21:59:21.390    - chunks/imba_BQ7G8x7L.mjs (esm)
21:59:21.390    - chunks/ini_CxEV6vK9.mjs (esm)
21:59:21.391    - chunks/java_S5axv2ha.mjs (esm)
21:59:21.391    - chunks/javascript_1jpmdg9f.mjs (esm)
21:59:21.391    - chunks/jinja_CNeJ7kCv.mjs (esm)
21:59:21.391    - chunks/jison_CFQdI8o-.mjs (esm)
21:59:21.391    - chunks/json5_Cz3SKyDc.mjs (esm)
21:59:21.391    - chunks/json_Bxn05aNV.mjs (esm)
21:59:21.391    - chunks/jsonc_32NJziF2.mjs (esm)
21:59:21.391    - chunks/jsonl_COzIwKtV.mjs (esm)
21:59:21.392    - chunks/jsonnet_C-ygVd7v.mjs (esm)
21:59:21.392    - chunks/jssm_CpUatrkZ.mjs (esm)
21:59:21.392    - chunks/jsx_ohfolMdN.mjs (esm)
21:59:21.392    - chunks/julia_DArOYbrn.mjs (esm)
21:59:21.392    - chunks/kotlin_GpCj3zMl.mjs (esm)
21:59:21.392    - chunks/kusto_DvKr5LCd.mjs (esm)
21:59:21.392    - chunks/latex_BG3hjNcA.mjs (esm)
21:59:21.392    - chunks/less_sOpDniyv.mjs (esm)
21:59:21.392    - chunks/light-plus_B5w_sP5Q.mjs (esm)
21:59:21.392    - chunks/liquid_Cz6hU4QA.mjs (esm)
21:59:21.393    - chunks/lisp_CeDhjDtZ.mjs (esm)
21:59:21.393    - chunks/logo_7efi0g9H.mjs (esm)
21:59:21.393    - chunks/lua_BnRhOPp9.mjs (esm)
21:59:21.393    - chunks/make_jVOdWphU.mjs (esm)
21:59:21.393    - chunks/markdown_BvKITVIe.mjs (esm)
21:59:21.393    - chunks/marko_BoYr3E-l.mjs (esm)
21:59:21.393    - chunks/material-theme-darker_B3b8AHuv.mjs (esm)
21:59:21.393    - chunks/material-theme-lighter_BBpAMYa9.mjs (esm)
21:59:21.393    - chunks/material-theme-ocean_DLnMTXhw.mjs (esm)
21:59:21.393    - chunks/material-theme-palenight_DlqR4urc.mjs (esm)
21:59:21.394    - chunks/material-theme_BnSs0JaN.mjs (esm)
21:59:21.394    - chunks/matlab_lji9uzwf.mjs (esm)
21:59:21.394    - chunks/mdc_D7JWlV8u.mjs (esm)
21:59:21.394    - chunks/mdx_CaTJWKbd.mjs (esm)
21:59:21.394    - chunks/mermaid_Ddw19k2C.mjs (esm)
21:59:21.394    - chunks/min-dark_qpN_GaDb.mjs (esm)
21:59:21.394    - chunks/min-light_BkjAuPrJ.mjs (esm)
21:59:21.394    - chunks/mojo_CDGOrdw2.mjs (esm)
21:59:21.394    - chunks/monokai_De3GYqVb.mjs (esm)
21:59:21.395    - chunks/move_COqzlyff.mjs (esm)
21:59:21.395    - chunks/narrat_BFXdyhmJ.mjs (esm)
21:59:21.395    - chunks/nextflow_D0dNoqIR.mjs (esm)
21:59:21.395    - chunks/nginx_Bqgp1PmF.mjs (esm)
21:59:21.395    - chunks/night-owl_COSDzmVO.mjs (esm)
21:59:21.395    - chunks/nim_CyJ_DIjM.mjs (esm)
21:59:21.395    - chunks/nix_BPGK_TBz.mjs (esm)
21:59:21.395    - chunks/nord_BdzlALVU.mjs (esm)
21:59:21.395    - chunks/nushell_DEP-wv9L.mjs (esm)
21:59:21.395    - chunks/objective-c_CeoN-agW.mjs (esm)
21:59:21.396    - chunks/objective-cpp_C22lHaz-.mjs (esm)
21:59:21.396    - chunks/ocaml_C0xj5shc.mjs (esm)
21:59:21.396    - chunks/one-dark-pro_tUVHX38X.mjs (esm)
21:59:21.396    - chunks/pages/generic_C5f6ygvI.mjs (esm)
21:59:21.396    - chunks/pascal_choms-Ap.mjs (esm)
21:59:21.396    - chunks/perl_DfUVk5na.mjs (esm)
21:59:21.396    - chunks/php_4FvCTxza.mjs (esm)
21:59:21.397    - chunks/plsql_DbF0LmMH.mjs (esm)
21:59:21.397    - chunks/poimandres_ChNJsD0Q.mjs (esm)
21:59:21.397    - chunks/postcss_C23aOnym.mjs (esm)
21:59:21.397    - chunks/powerquery_BrmumUw3.mjs (esm)
21:59:21.397    - chunks/powershell_cXW0bMb2.mjs (esm)
21:59:21.397    - chunks/prerender_BuMeEG28.mjs (esm)
21:59:21.397    - chunks/prisma_B4tDsiXh.mjs (esm)
21:59:21.398    - chunks/prolog_B2PjIrlI.mjs (esm)
21:59:21.398    - chunks/proto_BkSAUo2A.mjs (esm)
21:59:21.398    - chunks/pug_Dbg4OEg7.mjs (esm)
21:59:21.399    - chunks/puppet_KECwM_B1.mjs (esm)
21:59:21.399    - chunks/purescript_-6vfEkph.mjs (esm)
21:59:21.400    - chunks/python_ChL-lis5.mjs (esm)
21:59:21.401    - chunks/r_40HxkGj2.mjs (esm)
21:59:21.401    - chunks/raku_BOTZFe5z.mjs (esm)
21:59:21.401    - chunks/razor_DNrrJj68.mjs (esm)
21:59:21.401    - chunks/red_CzNlAgpo.mjs (esm)
21:59:21.402    - chunks/reg_CFybfmOK.mjs (esm)
21:59:21.402    - chunks/rel_6T2Fahwt.mjs (esm)
21:59:21.402    - chunks/riscv_CgRnyd5S.mjs (esm)
21:59:21.402    - chunks/rose-pine-dawn_BNadIBCo.mjs (esm)
21:59:21.402    - chunks/rose-pine-moon_DeTP7ucM.mjs (esm)
21:59:21.402    - chunks/rose-pine_BnyI4Y81.mjs (esm)
21:59:21.403    - chunks/rst_oO244uKw.mjs (esm)
21:59:21.403    - chunks/ruby_CbxvL9zu.mjs (esm)
21:59:21.403    - chunks/rust_CdCTvrJO.mjs (esm)
21:59:21.404    - chunks/sas_BAuzBD0D.mjs (esm)
21:59:21.404    - chunks/sass_BksE7QN8.mjs (esm)
21:59:21.404    - chunks/scala_BBBfKtb7.mjs (esm)
21:59:21.404    - chunks/scheme_CqaXO5-1.mjs (esm)
21:59:21.404    - chunks/scss_BpLf2qZb.mjs (esm)
21:59:21.404    - chunks/shaderlab_DHNkhV_F.mjs (esm)
21:59:21.404    - chunks/shellscript_BwVMUSqc.mjs (esm)
21:59:21.404    - chunks/shellsession_BY00LTTD.mjs (esm)
21:59:21.404    - chunks/slack-dark_mntFe7Ve.mjs (esm)
21:59:21.405    - chunks/slack-ochin_B5NE_gTG.mjs (esm)
21:59:21.405    - chunks/smalltalk_C_oS3f2_.mjs (esm)
21:59:21.405    - chunks/solarized-dark_JAcjRiQR.mjs (esm)
21:59:21.405    - chunks/solarized-light_C6Rd8QAa.mjs (esm)
21:59:21.405    - chunks/solidity_BbdlKny0.mjs (esm)
21:59:21.405    - chunks/sparql_D4Ra2Huh.mjs (esm)
21:59:21.405    - chunks/splunk_BMEYXQL1.mjs (esm)
21:59:21.405    - chunks/sql_BRw4qU9h.mjs (esm)
21:59:21.405    - chunks/ssh-config_DNViWBVt.mjs (esm)
21:59:21.405    - chunks/stata_DF9mUUgS.mjs (esm)
21:59:21.405    - chunks/stylus_BQg4uv6K.mjs (esm)
21:59:21.405    - chunks/svelte_DBYsWVUj.mjs (esm)
21:59:21.405    - chunks/swift_DNXdSlPm.mjs (esm)
21:59:21.406    - chunks/synthwave-84_Bf53Xi_m.mjs (esm)
21:59:21.406    - chunks/system-verilog_CIYTlmZK.mjs (esm)
21:59:21.406    - chunks/tasl_CWOt8ZWT.mjs (esm)
21:59:21.406    - chunks/tcl_D30zLR8m.mjs (esm)
21:59:21.406    - chunks/terraform_lTEmmhdd.mjs (esm)
21:59:21.406    - chunks/test-2_Ddbc6eTQ.mjs (esm)
21:59:21.406    - chunks/tex_lk9kkYNI.mjs (esm)
21:59:21.406    - chunks/tokyo-night_DDLizAgq.mjs (esm)
21:59:21.406    - chunks/toml_CTOxeXUa.mjs (esm)
21:59:21.406    - chunks/tsx_CSGt2xz6.mjs (esm)
21:59:21.406    - chunks/turtle_BAUOntQB.mjs (esm)
21:59:21.406    - chunks/twig_B8XsXn5I.mjs (esm)
21:59:21.406    - chunks/typescript_D80a_6Ty.mjs (esm)
21:59:21.406    - chunks/typst_B7giB_vg.mjs (esm)
21:59:21.407    - chunks/v_BRLMMcHn.mjs (esm)
21:59:21.407    - chunks/vb_DjYpVGcp.mjs (esm)
21:59:21.407    - chunks/verilog_DNF0P-N8.mjs (esm)
21:59:21.407    - chunks/vesper_5UFV0H-I.mjs (esm)
21:59:21.407    - chunks/vhdl_Bbo95uGr.mjs (esm)
21:59:21.407    - chunks/viml_DRGV1H_7.mjs (esm)
21:59:21.407    - chunks/vitesse-black_DvkkUbmW.mjs (esm)
21:59:21.407    - chunks/vitesse-dark_CxSPP1Th.mjs (esm)
21:59:21.407    - chunks/vitesse-light_iI3PFMgF.mjs (esm)
21:59:21.407    - chunks/vue-html_Bp06nvpx.mjs (esm)
21:59:21.407    - chunks/vue_CaUkvy-g.mjs (esm)
21:59:21.407    - chunks/vyper_gwDCYyIS.mjs (esm)
21:59:21.407    - chunks/wasm_B_MTSBG2.mjs (esm)
21:59:21.407    - chunks/wasm_DQg7kNPG.mjs (esm)
21:59:21.407    - chunks/wenyan_CdYV3dlA.mjs (esm)
21:59:21.408    - chunks/wgsl_CbUMGdo3.mjs (esm)
21:59:21.408    - chunks/wolfram_Cw_YwwMe.mjs (esm)
21:59:21.408    - chunks/xml_Csyr71ip.mjs (esm)
21:59:21.408    - chunks/xsl_tFXuH_ga.mjs (esm)
21:59:21.408    - chunks/yaml_CP0-42s5.mjs (esm)
21:59:21.408    - chunks/zenscript_CnlfS7RL.mjs (esm)
21:59:21.408    - chunks/zig_BMy00Bol.mjs (esm)
21:59:21.408    - manifest_BsW0SRki.mjs (esm)
21:59:21.408    - renderers.mjs (esm)
21:59:21.460    ✨ Compiled Worker successfully
21:59:21.648    Found _routes.json in output directory. Uploading.
21:59:21.662    Validating asset output directory
21:59:22.867    Deploying your site to Cloudflare's global network...
21:59:28.135    Uploading... (0/4)
21:59:28.775    Uploading... (1/4)
21:59:28.906    Uploading... (3/4)
21:59:29.052    Uploading... (4/4)
21:59:29.053    ✨ Success! Uploaded 4 files (1.36 sec)
21:59:29.053    
21:59:29.368    ✨ Upload complete!
21:59:32.267    Success: Assets published!
21:59:35.801    Error: Failed to publish your Function. Got error: Your Functions script is over the 1 MiB size limit (workers.api.error.script_too_large)

Steps to reproduce

I would also like to add that I noticed when there are content collections used, there are also chunks generated for them. Again this is new one for me. This one is not in the provided example, and taken from my other website. The provided example contains only the <Code/> component from above, but still it looks like it may be the same thing. I have added a content collection to the example. The chunks are there (beginning with test-collection...).

For example those three chunks:

image

And the code looks like this (which to me looks like functions used to get data for the content collections):

image

And looks like the generated chunks are mostly not minified, though I don't know if Cloudflare does some minification or it should be done at the build step.

Thanks!

What's the expected result?

The bundle should not be of this size.

Link to Minimal Reproducible Example

https://github.com/dallyh/astro-cf-10-issues/tree/code-bundle-issue

Participation

alexanderniebuhr commented 3 months ago

Could this be the same as https://github.com/withastro/astro/issues/10552?

dallyh commented 3 months ago

Looks like it, if it is then sorry that i created the issue here, i meanwhile added the content collection to the provided example, however I don't really know if the code for generating chunks for something like the <Code/> component is the same as generating chunks for the content collections. Thanks for pointing that issue to me!

alexanderniebuhr commented 3 months ago

All good tracking it here. We'll try to figure out where the issue occurs.

dallyh commented 3 months ago

I may have one question regarding this. Why did the previous version of the adapter generated _worker.js file, and now it is a _worker.js directory? Does it have to do anything with the mentioned ticket?

I was searching and it looks like the Pages deployment flow does not do any _worker.js bundling at all, and it will attach everything that is in that folder. I don't know if this is the case, but here https://community.cloudflare.com/t/refactor-a-worker-to-a-pages-function-problem/536838 (i know, it is not official from Cloudflare), one user mentions that:

When using advanced mode, you need to bundle the worker—Cloudflare Pages build system doesn’t handle the bundling (if I recall correctly.)

If I go and run npx wrangler deploy ./dist/_worker.js --dry-run --outdir ./dist/build --compatibility-date 2024-03-31 myself, the outputted bundle has 260 kilobytes.

I'm just being curious at this point :)

EDIT:

If I chain the commands like this, the build is succesfull and the page works (but I don't know to which extend.).

Commands:

    "bundle-worker": "npx wrangler deploy ./dist/_worker.js --dry-run --outdir ./dist/build --compatibility-date 2024-03-31",
    "build-cf": "pnpm run astro build && pnpm run bundle-worker && rm -rf ./dist/_worker.js && mv ./dist/build/_worker.js ./dist && rm -rf ./dist/build"

CF's build output:

17:14:46.418    Executing user command: npm run build-cf
17:14:46.967    
17:14:46.968    > astro-cf-10-issues@0.0.1 build-cf
17:14:46.968    > pnpm run astro build && pnpm run bundle-worker && rm -rf ./dist/_worker.js && mv ./dist/build/_worker.js ./dist && rm -rf ./dist/build
17:14:46.968    
17:14:47.501    
17:14:47.502    > astro-cf-10-issues@0.0.1 astro /opt/buildhome/repo
17:14:47.502    > astro "build"
17:14:47.502    
17:14:49.474    15:14:49 [build] output: "hybrid"
17:14:49.475    15:14:49 [build] directory: /opt/buildhome/repo/dist/
17:14:49.475    15:14:49 [build] adapter: @astrojs/cloudflare
17:14:49.475    15:14:49 [build] Collecting build info...
17:14:49.476    15:14:49 [build] ✓ Completed in 530ms.
17:14:49.477    15:14:49 [build] Building hybrid entrypoints...
17:14:54.968    15:14:54 [vite] ✓ built in 5.46s
17:14:54.968    15:14:54 [build] ✓ Completed in 5.49s.
17:14:54.985    
17:14:54.985     prerendering static routes 
17:14:55.006    15:14:55 ▶ src/pages/test-2.astro
17:14:55.211    15:14:55   └─ /test-2/index.html (+204ms)
17:14:55.211    15:14:55 ✓ Completed in 233ms.
17:14:55.211    
17:14:55.211     generating optimized images 
17:14:55.214    15:14:55   ▶ /_astro/favicon.vp_fBu0c_1fShzE.svg (before: 0kB, after: 0kB) (+1ms) (1/1)
17:14:55.215    15:14:55 ✓ Completed in 2ms.
17:14:55.215    
17:14:55.233    15:14:55 
17:14:55.233     finalizing server assets 
17:14:55.233    
17:14:55.233    15:14:55 [build] Rearranging server assets...
17:14:55.238    15:14:55 [build] Server built in 6.29s
17:14:55.239    15:14:55 [build] Complete!
17:14:55.706    
17:14:55.706    > astro-cf-10-issues@0.0.1 bundle-worker /opt/buildhome/repo
17:14:55.707    > npx wrangler deploy ./dist/_worker.js --dry-run --outdir ./dist/build --compatibility-date 2024-03-31
17:14:55.707    
17:14:57.245     ⛅️ wrangler 3.39.0 (update available 3.41.0)
17:14:57.245    ---------------------------------------------
17:14:57.318    --dry-run: exiting now.
17:14:57.328    Total Upload: 257.03 KiB / gzip: 62.83 KiB
17:14:57.417    Finished
17:14:57.417    Found _worker.js in output directory. Uploading.
17:14:58.430    ✨ Compiled Worker successfully
17:14:58.489    Found _routes.json in output directory. Uploading.
17:14:58.502    Validating asset output directory
17:14:59.332    Deploying your site to Cloudflare's global network...
17:15:06.887    Uploading... (4/4)
17:15:06.887    ✨ Success! Uploaded 0 files (4 already uploaded) (0.42 sec)
17:15:06.888    
17:15:07.276    ✨ Upload complete!
17:15:10.306    Success: Assets published!
17:15:13.182    Success: Your site was deployed!

Site is here: https://96e116ec.astro-cf-10-issues.pages.dev/test-2/ and branch on the example repo here https://github.com/dallyh/astro-cf-10-issues/tree/create-worker-bundle.

You can see that the build output from Cloudflare differs a lot, as before it attached additional modules (looks like all of them) and also failed due to size limits.

EDIT2: Just tried it on my project, and it kinda works too, however the bundle size is strange depending on some factors like React and where it is used (which is probably to be expected).

Bundle without React component on an ondemand route:

 ⛅️ wrangler 3.41.0
-------------------
--dry-run: exiting now.
Total Upload: 245.62 KiB / gzip: 68.58 KiB

Bundle with React component on an ondemand route:

 ⛅️ wrangler 3.41.0
-------------------
--dry-run: exiting now.
Total Upload: 6080.26 KiB / gzip: 1878.34 KiB
▲ [WARNING] We recommend keeping your script less than 1MiB (1024 KiB) after gzip. Exceeding this can affect cold start time. Consider using Wrangler's `--minify` option to reduce your bundle size.
alexanderniebuhr commented 3 months ago

@dallyh We worked closely with the Cloudflare team and using a _wrangler.js directory instead of a _wrangler.js bundle, is the correct way.

dallyh commented 3 months ago

Got it, thanks. I'll wait for the upstream issue to get fixed, however bundling the directory could be taken as a workaround for now, because there's no other way to keep the functionality of everything and also keep the worker within the 1MB/10MB constraint. I'm worried that this could be an issue going forward with the current approach.

alexanderniebuhr commented 3 months ago

I flagged this with Astro core and the Cloudflare team to see why this happens, there shouldn't be unused code in the directory. But it sounds like bundling does tree-shake some things away, so we need to understand why and fix the initial output. Anyways, feel free to add this workaround in your project.

marceloverdijk commented 3 months ago

I'm also facing issues with Astro Cloudflare hybrid output. I see some code in the bundle that I would not expect.. and even causing failure to deploy.

I created a thread on Discord about this before finding this issue:

https://discord.com/channels/830184174198718474/1224835877721870516

Could this maybe be similar issue?

dallyh commented 2 months ago

I would like to add that minifying all of the outputted files (if possible) could also save few Kb's. Most of the files are generated as-is without any minification, like renderers, manifest, index etc.

tombl commented 2 months ago

Especially now that Workers supports sourcemaps, minification seems like a free size reduction.

alexanderniebuhr commented 2 months ago

@dallyh minifying would be something we would need to discuss in core, since the files are generated by astro build.

I just note that minifying the files had a history of making stack traces unusable in the past.