Skip to content

fix(cloudflare): pass waitUntil to cache providers#16187

Merged
Princesseuh merged 25 commits into
withastro:mainfrom
gllmt:dev/fix-cache-provider-waituntil
Apr 29, 2026
Merged

fix(cloudflare): pass waitUntil to cache providers#16187
Princesseuh merged 25 commits into
withastro:mainfrom
gllmt:dev/fix-cache-provider-waituntil

Conversation

@gllmt

@gllmt gllmt commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Closes #16145

Changes

  • Add optional waitUntil to the CacheProvider.onRequest() context type.
  • Thread waitUntil through BaseApp.render() so adapters can pass it to runtime cache providers.
  • Forward Cloudflare ExecutionContext.waitUntil from the adapter handler into app.render().
  • Add a regression test covering a custom cache provider on Cloudflare.
  • Added a changeset for astro and @astrojs/cloudflare.

Testing

  • pnpm run build
  • node --test packages/integrations/cloudflare/test/cache-provider-wait-until.test.js

Docs

  • No docs update added. This fixes the implementation to match the existing documented CacheProvider.onRequest() signature.

@changeset-bot

changeset-bot Bot commented Apr 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 57abd56

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

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

@github-actions github-actions Bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Apr 2, 2026
@codspeed-hq

codspeed-hq Bot commented Apr 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing gllmt:dev/fix-cache-provider-waituntil (57abd56) with main (5120ecd)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (f99f32a) during the generation of this report, so 5120ecd was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@edmelly-pg

edmelly-pg commented Apr 2, 2026

Copy link
Copy Markdown

@gllmt

gllmt commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I agree the same plumbing may also apply to Netlify and Vercel.

I scoped this PR to #16145 since that issue is currently labeled pkg: cloudflare, but I’m happy to follow up for the other adapters as well if maintainers would prefer to cover them here.

@edmelly-pg

Copy link
Copy Markdown

Any chance of a maintainer reviewing/progressing this soon? Would be great to get this fix in. Thanks!

@ematipico ematipico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good, but I'm afraid we can't merge it as is. We're adding a new API, which means:

const response = await app.render(request, {
routeData,
locals,
waitUntil: context.waitUntil.bind(context),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this might be correct, but it doesn't hold against the types of waitUntil. You typed waitUntil with a small function that doesn't type the this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that makes sense.

I initially treated this as plumbing to match the already-documented CacheProvider.onRequest() context, but exposing waitUntil through app.render() / RenderOptions does make it a public API surface.

I’ll update the waitUntil typing, promote the astro changeset to minor with a fuller description, and open a docs PR against withastro/docs for the astro-app RenderOptions page, then link it back here.

I’ll keep the implementation scope focused on Cloudflare in this PR and can follow up separately for the other adapters if that’s preferred.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated this so the public waitUntil contract is a dedicated bound callback type (this: void), and the Cloudflare adapter now passes a bound function typed against RenderOptions['waitUntil'] instead of leaving it as an ad-hoc callback shape.

@ematipico ematipico added this to the 6.2 milestone Apr 16, 2026
@gllmt

gllmt commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the requested follow-up changes:

  • updated the waitUntil typing so the public callback contract is explicit and the Cloudflare adapter passes a bound, typed function
  • promoted the astro changeset to minor and expanded the description
  • opened the docs PR for RenderOptions.waitUntil: docs: document RenderOptions.waitUntil docs#13744

Re-ran:

  • pnpm run build
  • node --test packages/integrations/cloudflare/test/cache-provider-wait-until.test.js

Docs verification:

  • pnpm check in withastro/docs

I kept the implementation scope focused on Cloudflare here; happy to follow up separately for the other adapters if that’s preferred.

Comment thread .changeset/fair-walls-raise.md

@ematipico ematipico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's great, thank you @gllmt ! The PR is good now, we will release it once we're ready to ship a new minor of Astro 🫡

Comment thread .changeset/fair-walls-raise.md Outdated
@ematipico

Copy link
Copy Markdown
Member

@gllmt

gllmt commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the lint failure.

Root cause: after the Cloudflare test suite was migrated to TypeScript on this branch, this new test was still left as cache-provider-wait-until.test.js and still imported ./_test-utils.js, which no longer exists. That is what knip was flagging in CI.

Updated it to cache-provider-wait-until.test.ts, switched the import to ./test-utils.ts, and aligned the fixture/preview server typing with the rest of the Cloudflare tests.

Re-ran locally:

  • pnpm exec knip
  • pnpm run lint:ci
  • node --test packages/integrations/cloudflare/test/cache-provider-wait-until.test.ts

@ArmandPhilippot ArmandPhilippot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a nit for the changeset, but otherwise this LGTM! Thank you!

Comment thread .changeset/fair-walls-raise.md Outdated
@not-matthias

This comment has been minimized.

@yanthomasdev yanthomasdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thanks!

@Princesseuh
Princesseuh merged commit fe58071 into withastro:main Apr 29, 2026
27 of 28 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Apr 29, 2026
dadezzz pushed a commit to dadezzz/university_notes that referenced this pull request May 4, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`6.1.10` → `6.2.1`](https://renovatebot.com/diffs/npm/astro/6.1.10/6.2.1) | ![age](https://developer.mend.io/api/mc/badges/age/npm/astro/6.2.1?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/astro/6.1.10/6.2.1?slim=true) |

---

### Release Notes

<details>
<summary>withastro/astro (astro)</summary>

### [`v6.2.1`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#621)

[Compare Source](https://github.com/withastro/astro/compare/[email protected]@6.2.1)

##### Patch Changes

- [#&#8203;16531](withastro/astro#16531) [`76db01d`](withastro/astro@76db01d) Thanks [@&#8203;rodrigosdev](https://github.com/rodrigosdev)! - Fixes config validation for omitted `integrations` fields with newer Zod versions.

- [#&#8203;16535](withastro/astro#16535) [`7df0fe4`](withastro/astro@7df0fe4) Thanks [@&#8203;rururux](https://github.com/rururux)! - Fixed an issue where a warning was displayed when the `server` property was missing during config validation, even though it is not required.

- [#&#8203;16534](withastro/astro#16534) [`5cf6c51`](withastro/astro@5cf6c51) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Fixes compatibility with Zod 4.4.0 for the `server` config property and error formatting

### [`v6.2.0`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#620)

[Compare Source](https://github.com/withastro/astro/compare/[email protected]@6.2.0)

##### Minor Changes

- [#&#8203;16187](withastro/astro#16187) [`fe58071`](withastro/astro@fe58071) Thanks [@&#8203;gllmt](https://github.com/gllmt)! - Adds a `waitUntil` option to the `RenderOptions` so that adapters can forward runtime background-task hooks to Astro.

  When provided by an adapter, runtime cache providers receive `context.waitUntil` in
  `CacheProvider.onRequest()`, which allows background cache work such as stale-while-revalidate
  without blocking the response. The Cloudflare adapter now forwards
  `ExecutionContext.waitUntil` to this API.

- [#&#8203;16290](withastro/astro#16290) [`a49637a`](withastro/astro@a49637a) Thanks [@&#8203;ViVaLaDaniel](https://github.com/ViVaLaDaniel)! - Ensures that `server.allowedHosts` (and `vite.preview.allowedHosts`) configuration is respected when using `astro preview` with the `@astrojs/cloudflare` adapter. This improves security by preventing DNS rebinding attacks when previewing Cloudflare builds locally.

- [#&#8203;15725](withastro/astro#15725) [`4108ec1`](withastro/astro@4108ec1) Thanks [@&#8203;meyer](https://github.com/meyer)! - Adds support for a new `'jsx'` value for the `compressHTML` option. When set, whitespace is stripped using JSX whitespace rules instead of the default HTML compression strategy.

  ```js
  // astro.config.mjs
  import { defineConfig } from 'astro/config';

  export default defineConfig({
    compressHTML: 'jsx',
  });
  ```

  In JSX, whitespaces never matter, as such, no amount of indentation, or newlines will not affect the rendered output. For instance, the following code:

  ```jsx
  <div>
    <span>foo</span>
    <span>bar</span>
  </div>
  ```

  will be rendered as `foobar`, whereas with HTML whitespace rules, a space would be present between the words due to the newline and indentation between the tags.

- [#&#8203;16477](withastro/astro#16477) [`28fb3e1`](withastro/astro@28fb3e1) Thanks [@&#8203;ematipico](https://github.com/ematipico)! - Adds experimental support for configurable log handlers.

  This experimental feature provides better control over Astro's logging infrastructure by allowing users to replace the default console output with custom logging implementations (e.g., structured JSON). This is particularly useful for users using on-demand rendering and wishing to connect their log aggregation services, such as Kibana, Logstash, CloudWatch, Grafana, or Loki.

  By default, Astro provides three built-in log handlers (`json`, `node`, and `console`), but you can also create your own.

##### JSON logging

JSON logging can be enabled via the CLI for the `build`, `dev`, and `sync` commands using the `experimentalJson` flag:

```js
// astro.config.mjs
import { defineConfig, logHandlers } from 'astro/config';

export default defineConfig({
  experimental: {
    logger: logHandlers.json({
      pretty: true,
      level: 'warn',
    }),
  },
});
```

##### Custom logger

You can also create your own custom logger by implementing the correct interface:

```js
// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  experimental: {
    logger: {
      entrypoint: '@&#8203;org/custom-logger',
    },
  },
});
```

```ts
// @&#8203;org/custom-logger.js
import type { AstroLoggerDestination, AstroLoggerMessage } from 'astro';
import { matchesLevel } from 'astor/logger';

function customLogger(level = 'info'): AstroLoggerDestination {
  return {
    write(message: AstroLoggerMessage) {
      if (matchesLevel(message.level, level)) {
        // write message somewhere
      }
    },
  };
}

export default customLogger;
```

For more information on enabling and using this feature in your project, see the [Experimental Logger docs](https://docs.astro.build/en/reference/experimental-flags/logger/).

For a complete overview and to give feedback on this experimental API, see the [Custom logger RFC](https://github.com/withastro/roadmap/blob/logger/proposals/0059-custom-logger.md).

- [#&#8203;16333](withastro/astro#16333) [`0f7c3c8`](withastro/astro@0f7c3c8) Thanks [@&#8203;florian-lefebvre](https://github.com/florian-lefebvre)! - Adds an experimental flag `svgOptimizer` that enables automatic optimization of your SVG components using the provided optimizer. This supersedes the `svgo` experimental flag, which is now removed.

  When enabled, your imported SVG files used as components will be optimized for smaller file sizes and better performance while maintaining visual quality. This can significantly reduce the size of your SVG assets by removing unnecessary metadata, comments, and redundant code.

  Astro ships with a [SVGO](https://svgo.dev/) based optimizer, but any can be used.

  To enable this feature, add the experimental flag in your Astro config and remove `svgo` if it was enabled:

  ```diff
  // astro.config.mjs
  -import { defineConfig } from "astro/config";
  +import { defineConfig, svgoOptimizer } from "astro/config";

  export default defineConfig({
  +  experimental: {
  +    svgOptimizer: svgoOptimizer()
  -    svgo: true
  +  }
  });
  ```

  For more information on enabling and using this feature in your project, see the [experimental SVG optimization docs](https://docs.astro.build/en/reference/experimental-flags/svg-optimization/).

- [#&#8203;16302](withastro/astro#16302) [`f6f8e80`](withastro/astro@f6f8e80) Thanks [@&#8203;florian-lefebvre](https://github.com/florian-lefebvre)! - Adds a new `experimental_getFontFileURL()` method to resolve font file URLs when using the Fonts API

  The `fontData` object exported from `astro:assets` was introduced to provide low-level access to font family data for advanced usage. One of the goals of this API was to be able to resolve buffers using URLs. However, it turned out to be impractical, especially during prerendering.

  Astro now exports a new `experimental_getFontFileURL()` helper function from `astro:assets` to resolve font file URLs from `fontData`. For example, when using [satori](https://github.com/vercel/satori) to generate Open Graph images:

  ```diff
  // src/pages/og.png.ts

  import type { APIRoute } from "astro";
  -import { fontData } from "astro:assets";
  +import { fontData, experimental_getFontFileURL } from "astro:assets";
  -import { outDir } from "astro:config/server";
  -import { readFile } from "node:fs/promises";
  import satori from "satori";
  import { html } from "satori-html";
  import sharp from "sharp";

  export const GET: APIRoute = async (context) => {
    const fontPath = fontData["--font-roboto"][0]?.src[0]?.url;

    if (fontPath === undefined) {
      throw new Error("Cannot find the font path.");
    }

  -  const data = import.meta.env.DEV
  -    ? await fetch(new URL(fontPath, context.url.origin)).then(async (res) => res.arrayBuffer())
  -    : await readFile(new URL(`.${fontPath}`, outDir));
  +  const url = experimental_getFontFileURL(fontPath, context.url);
  +  const data = await fetch(url).then((res) => res.arrayBuffer());

    const svg = await satori(
      html`<div style="color: black;">hello, world</div>`,
      {
        width: 600,
        height: 400,
        fonts: [
          {
            name: "Roboto",
            data,
            weight: 400,
            style: "normal",
          },
        ],
      },
    );

    const pngBuffer = await sharp(Buffer.from(svg))
      .resize(600, 400)
      .png()
      .toBuffer();

    return new Response(new Uint8Array(pngBuffer), {
      headers: {
        "Content-Type": "image/png",
      },
    });
  };
  ```

  See the [Fonts API documentation](https://docs.astro.build/en/guides/fonts/#accessing-font-data-programmatically) for more information.

##### Patch Changes

- [#&#8203;15980](withastro/astro#15980) [`8812382`](withastro/astro@8812382) Thanks [@&#8203;seroperson](https://github.com/seroperson)! - Prevents script deduplication inside `<template>` elements

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xNDEuNiIsInVwZGF0ZWRJblZlciI6IjQzLjE0MS42IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

waitUntil not available in onRequest context in a CacheProvider

7 participants