Skip to content

fix(cloudflare): strip return statements from frontmatter in esbuild scan plugin#15588

Merged
Princesseuh merged 2 commits into
withastro:mainfrom
rururux:fix-cf-top-level-return
Feb 20, 2026
Merged

fix(cloudflare): strip return statements from frontmatter in esbuild scan plugin#15588
Princesseuh merged 2 commits into
withastro:mainfrom
rururux:fix-cf-top-level-return

Conversation

@rururux
Copy link
Copy Markdown
Member

@rururux rururux commented Feb 20, 2026

fixes: #15518

Changes

In the previous implementation of the Cloudflare Workers integration, the following error occurred when esbuild attempted to scan dependencies by loading the code:

✘ [ERROR] Top-level return cannot be used inside an ECMAScript module

This pull request addresses this issue with the following changes:

  • Modified esbuild input: Before passing code to esbuild, return statements are now replaced with throw . This logic is based on the implementation found in vite-plugin-astro/compile.ts. To ensure source maps remain accurate, the replacement includes padding to maintain the original character count.
  • Added comments explaining potential errors that might arise from this change in specific edge cases.

Testing

  • Verified that the Top-level return cannot be ... error no longer occurs.
  • Added a test case to ensure that the replacement logic does not break the syntax (which would previously trigger a Failed to run dependency scan ... error).

Docs

N/A

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 20, 2026

🦋 Changeset detected

Latest commit: 64e4b5f

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 the pkg: integration Related to any renderer integration (scope) label Feb 20, 2026
Copy link
Copy Markdown
Member

@Princesseuh Princesseuh 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, awesome work!

@Princesseuh Princesseuh merged commit 425ea16 into withastro:main Feb 20, 2026
20 of 21 checks passed
@rururux
Copy link
Copy Markdown
Member Author

rururux commented Feb 20, 2026

Thank you so much! 🥳

@rururux rururux deleted the fix-cf-top-level-return branch February 20, 2026 18:14
@eighty4
Copy link
Copy Markdown

eighty4 commented Apr 26, 2026

Would this also present with astro-check warnings on return Astro.redirect in Frontmatter? I'm seeing UNUSED_IMPORT_EXAMPLE is declared but its value is never read warnings when importing utils to use with Astro.redirect.

@Princesseuh
Copy link
Copy Markdown
Member

No, that's unrelated to this.

@eighty4
Copy link
Copy Markdown

eighty4 commented Apr 29, 2026

@Princesseuh I understand that the esbuild plugin in the cloudflare adapter throwing an error doesn't cause the warnings in astro-check, but it does seem like it's the same problem presenting in different areas of the Astro ecosystem.

Doing this in frontmatter that is handled in middleware seems to be a workaround for the astro-check warnings on top level return statement:

if (condition)
    throw SomeHttpResponse()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ERROR] Top-level return cannot be used inside an ECMAScript module

3 participants