Skip to content

Commit fbff0fb

Browse files
committed
fix(frontmatter): Correctly handle bare returns
1 parent a2fff74 commit fbff0fb

4 files changed

Lines changed: 16 additions & 2 deletions

File tree

.changeset/dull-clowns-sin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@astrojs/cloudflare': patch
3+
'astro': patch
4+
---
5+
6+
Fixes `return;` syntax not working in the frontmatter correctly in certain contexts

packages/astro/src/vite-plugin-astro/compile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ async function enhanceCompileError({
110110
const scannedFrontmatter = frontmatterRE.exec(source);
111111
if (scannedFrontmatter) {
112112
// Top-level return is not supported, so replace `return` with throw
113-
const frontmatter = scannedFrontmatter[1].replace(/\breturn\b/g, 'throw');
113+
const frontmatter = scannedFrontmatter[1]
114+
.replace(/\breturn\s*;/g, 'throw 0;')
115+
.replace(/\breturn\b/g, 'throw ');
114116

115117
// If frontmatter does not actually include the offending line, skip
116118
if (lineText && !frontmatter.includes(lineText)) throw err;

packages/integrations/cloudflare/src/esbuild-plugin-astro-frontmatter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export function astroFrontmatterScanPlugin(): ESBuildPlugin {
3030
//
3131
// Known Limitation: Using regex /\breturn\b/ will incorrectly match
3232
// identifiers like `$return` or aliases like `import { return as ret }`.
33-
const contents = frontmatterMatch[1].replace(/\breturn\b/g, 'throw ');
33+
const contents = frontmatterMatch[1]
34+
.replace(/\breturn\s*;/g, 'throw 0;')
35+
.replace(/\breturn\b/g, 'throw ');
3436

3537
return {
3638
contents,

packages/integrations/cloudflare/test/fixtures/top-level-return/src/pages/index.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ if (guard()) {
1313
return Astro.redirect("/404")
1414
}
1515
16+
// Bare return (no value) — must not produce invalid `throw ;` syntax during dep scanning
17+
const source = "hello";
18+
if (!source) return;
19+
1620
---
1721

1822
<html>

0 commit comments

Comments
 (0)