
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title><![CDATA[ The Cloudflare Blog ]]></title>
        <description><![CDATA[ Get the latest news on how products at Cloudflare are built, technologies used, and join the teams helping to build a better Internet. ]]></description>
        <link>https://blog.cloudflare.com</link>
        <atom:link href="https://blog.cloudflare.com/" rel="self" type="application/rss+xml"/>
        <language>en-us</language>
        <image>
            <url>https://blog.cloudflare.com/favicon.png</url>
            <title>The Cloudflare Blog</title>
            <link>https://blog.cloudflare.com</link>
        </image>
        <lastBuildDate>Sun, 12 Apr 2026 00:54:40 GMT</lastBuildDate>
        <item>
            <title><![CDATA[Use the language of your choice with Pages Functions via WebAssembly]]></title>
            <link>https://blog.cloudflare.com/pages-functions-with-webassembly/</link>
            <pubDate>Fri, 24 Mar 2023 13:00:00 GMT</pubDate>
            <description><![CDATA[ Today Pages is excited to offer support for WebAssembly when writing a Pages Function ]]></description>
            <content:encoded><![CDATA[ <p></p><p>On the Cloudflare Developer Platform, we understand that building any application is a unique experience for every developer. We know that in the developer ecosystem there are a plethora of tools to choose from and as a developer you have preferences and needs. We don’t believe there are “right” or “wrong” tools to use in development and want to ensure a good developer experience no matter your choices. We believe in meeting you where you are.</p><p>When Pages Functions moved to <a href="/pages-function-goes-ga/">Generally Available in November of last year</a>, we knew it was the key that unlocks a variety of use cases – namely full-stack applications! However, we still felt we could do more to provide the flexibility for you to build what you want and how you want.</p><p>That’s why today we’re opening the doors to developers who want to build their server side applications with something other than JavaScript. We’re excited to announce WebAssembly support for Pages Functions projects!</p><p><a href="https://webassembly.org/"><b>WebAssembly</b></a> <b>(or Wasm)</b> is a low-level assembly-like language that can run with near-native performance. It provides programming languages such as C/C++, C# or Rust with a compilation target, enabling them to run alongside JavaScript. Primarily designed to run on the <a href="https://webassembly.org/docs/web/">web</a> (though <a href="https://webassembly.org/docs/non-web/">not exclusively</a>), WebAssembly opens up exciting opportunities for applications to run on the web platform, both on the client and the server, that up until now couldn't have done so.</p><p>With Pages Functions being Workers “under the hood” and Workers having <a href="/workers-javascript-modules/">Wasm module support</a> for quite <a href="/webassembly-on-cloudflare-workers/">some time</a>, it is only natural that Pages provides a similar experience for our users as well. While not all use cases are a good fit for Wasm, there are <a href="https://webassembly.org/docs/use-cases/">many</a> that are. Our goal with adding Wasm support is enabling those use cases and expanding the boundaries of what Functions can build.</p>
    <div>
      <h3>Using WebAssembly in Pages Functions</h3>
      <a href="#using-webassembly-in-pages-functions">
        
      </a>
    </div>
    <p>WebAssembly in Pages Functions works very similar to how it does today in Workers — we read <code>wasm</code> files as WebAssembly modules, ready for you to import and use directly from within your Functions. In short, like this:</p>
            <pre><code>// functions/api/distance-between.js

import wasmModule from "../../pkg/distance.wasm";

export async function onRequest({ request }) {
  const moduleInstance = await WebAssembly.instantiate(wasmModule);
  const distance = await moduleInstance.exports.distance_between();

  return new Response(distance);
}</code></pre>
            <p>Let’s briefly unpack the code snippet above to highlight some things that are important to understand.</p>
            <pre><code>import wasmModule from "../../pkg/distance.wasm";</code></pre>
            <p>Pages makes no assumptions as to how the binary <code>.wasm</code> files you want to import were compiled. In our example above, <code>distance.wasm</code> can be a file you compiled yourself out of code you wrote, or equally, a file provided in a third-party library’s distribution. The only thing Pages cares about is that <code>distance.wasm</code> is a compiled <a href="https://webassembly.github.io/spec/core/binary/conventions.html">binary</a> Wasm <a href="https://webassembly.github.io/spec/core/binary/modules.html">module</a> file.</p><p>The result of that import is a <a href="https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Module"><code>WebAssembly.Module</code></a> object, which you can then <a href="https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/instantiate">instantiate</a>:</p>
            <pre><code>const moduleInstance = await WebAssembly.instantiate(wasmModule);</code></pre>
            <p>Once the <a href="https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Instance"><code>WebAssembly.Instance</code></a> object is created, you can start using whatever features your Wasm module <a href="https://webassembly.github.io/spec/core/syntax/modules.html#syntax-export"><code>exports</code></a>, inside your Functions code:</p>
            <pre><code>const distance = await moduleInstance.exports.distance_between();</code></pre>
            
    <div>
      <h3>More modules, more fun!</h3>
      <a href="#more-modules-more-fun">
        
      </a>
    </div>
    <p>Apart from Wasm modules, this work unlocks support for two other module types that you can import within your Functions code: <b>text</b> and <b>binary</b>. These are not standardized modules, but can be very handy if you need to import raw text blobs (such as HTML files) as a <code>string</code>:</p>
            <pre><code>// functions/my-function.js
import html from "404.html";

export async function onRequest() {
  return new Response(html,{
    headers: { "Content-Type": "text/html" }
  });
}</code></pre>
            <p>or raw data blobs (such as images) as an <code>ArrayBuffer</code>.</p>
            <pre><code>// functions/my-function.js
import image from "../hearts.png.bin";

export async function onRequest() {
  return new Response(image,{
    headers: { "Content-Type": "image/png" }
  });
}</code></pre>
            
    <div>
      <h3>The distance between us on the surface of Earth</h3>
      <a href="#the-distance-between-us-on-the-surface-of-earth">
        
      </a>
    </div>
    <p>Let’s take a look at a live example to see it all in action! We’ve built a small <a href="https://pages-with-wasm-demo.pages.dev/">demo app</a> that walks you through an example of Functions with WebAssembly end-to-end. You can check out the code of our demo application available on <a href="https://github.com/cloudflare/pages-fns-with-wasm-demo">GitHub</a>.</p><p>The application computes the distance in kilometers on the surface of Earth between your current location (based on the geo coordinates of the incoming <a href="https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties">request</a>) and any other point on the globe, each time you click on the globe's surface.</p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/4ZkZSyM2pJghHYzcwgfw3T/3c3609ec59bb6589d7a575177cbf602e/image3-31.png" />
            
            </figure><p>The code that performs the actual high-performance distance calculation is written in Rust, and is a slight adaptation of the <a href="https://rust-lang-nursery.github.io/rust-cookbook/science/mathematics/trigonometry.html#distance-between-two-points-on-the-earth">example</a> provided in the <a href="https://rust-lang-nursery.github.io/rust-cookbook/">Rust cookbook</a>:</p>
            <pre><code>fn distance_between(from_latitude_degrees: f64, from_longitude_degrees: f64, to_latitude_degrees: f64, to_longitude_degrees: f64) -&gt; f64 {
    let earth_radius_kilometer = 6371.0_f64;

    let from_latitude = from_latitude_degrees.to_radians();
    let to_latitude = to_latitude_degrees.to_radians();

    let delta_latitude = (from_latitude_degrees - to_latitude_degrees).to_radians();
    let delta_longitude = (from_longitude_degrees - to_longitude_degrees).to_radians();

    let central_angle_inner = (delta_latitude / 2.0).sin().powi(2)
        + from_latitude.cos() * to_latitude.cos() * (delta_longitude / 2.0).sin().powi(2);
    let central_angle = 2.0 * central_angle_inner.sqrt().asin();

    let distance = earth_radius_kilometer * central_angle;
    
    return distance;
}</code></pre>
            <p>We have a Rust playground experiment available <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=b60cdd8c60bed969c03bf5b87914c196">here</a>, in case you want to play around with this code snippet in particular.</p><p>To use the <code>distance_between()</code> Rust function in Pages Functions, we first compile the code to WebAssembly using <a href="https://github.com/rustwasm/wasm-pack"><code>wasm-pack</code></a>:</p>
            <pre><code>##
# generate the `pkg` folder which will contain the wasm binary
##
wasm-pack build</code></pre>
            <p>Then, we import the generated <code>.wasm</code> artifact from inside our <code>distance-between.js</code> Pages Function. Now, each time you click on the globe surface, a request to <code>/api/distance-between</code> is made, which will trigger the <code>distance_between()</code> function to execute. Once computed, the distance value is returned by our Function, back to the client, which proceeds to display the value to the user.</p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/1nWcWHd4MNnPVEZQ8A3MwE/868b091b3b4849d2c6d9848a737eb476/image2-23.png" />
            
            </figure><p>We want to point out that this application could have been built entirely in JavaScript, however, we equally wanted to show just how simple it is to build it with Rust. The decision to use Rust was motivated by a few factors. First, the tooling ecosystem for building and working with Rust-generated WebAssembly is quite mature, well documented, and easy to get started with. Second, the Rust <a href="https://www.rust-lang.org/learn">docs</a> are a fantastic resource if you are new to Rust or to Rust with WebAssembly! If you are looking for a step-by-step tutorial on how to generate and set up a Rust and WebAssembly project, we highly recommend checking out Rust’s official <a href="https://rustwasm.github.io/docs/book/introduction.html">WebAssembly Book</a>.</p><p>We hope it gives you a solid starting point in exploring what is possible with Wasm on Pages Functions, and inspires you to create some powerful applications of your own. Head over to our <a href="https://developers.cloudflare.com/pages/platform/functions/module-support/#module-support">docs</a> to get started today!</p> ]]></content:encoded>
            <category><![CDATA[Cloudflare Pages]]></category>
            <category><![CDATA[WASM]]></category>
            <category><![CDATA[WebAssembly]]></category>
            <category><![CDATA[Cloudflare Workers]]></category>
            <category><![CDATA[Developers]]></category>
            <category><![CDATA[Developer Platform]]></category>
            <guid isPermaLink="false">55qq7fR9xOWSZtN9TZgrik</guid>
            <dc:creator>Carmen Popoviciu</dc:creator>
        </item>
    </channel>
</rss>