summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/wet-garlics-beam.md5
-rw-r--r--packages/astro/src/compiler/index.ts4
-rw-r--r--packages/astro/test/fetch.test.js18
-rw-r--r--packages/astro/test/fixtures/fetch/snowpack.config.json3
-rw-r--r--packages/astro/test/fixtures/fetch/src/components/Child.jsx7
-rw-r--r--packages/astro/test/fixtures/fetch/src/pages/index.astro12
6 files changed, 49 insertions, 0 deletions
diff --git a/.changeset/wet-garlics-beam.md b/.changeset/wet-garlics-beam.md
new file mode 100644
index 000000000..30773e3bc
--- /dev/null
+++ b/.changeset/wet-garlics-beam.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Makes `fetch` available in all framework components
diff --git a/packages/astro/src/compiler/index.ts b/packages/astro/src/compiler/index.ts
index c7bd98655..c9cce245f 100644
--- a/packages/astro/src/compiler/index.ts
+++ b/packages/astro/src/compiler/index.ts
@@ -115,6 +115,10 @@ export async function compileComponent(source: string, { compileOptions, filenam
import fetch from 'node-fetch';
${result.imports.join('\n')}
+if(!('fetch' in globalThis)) {
+ globalThis.fetch = fetch;
+}
+
${/* Global Astro Namespace (shadowed & extended by the scoped namespace inside of __render()) */ ''}
const __TopLevelAstro = {
site: new URL(${JSON.stringify(site)}),
diff --git a/packages/astro/test/fetch.test.js b/packages/astro/test/fetch.test.js
new file mode 100644
index 000000000..5f00a61ad
--- /dev/null
+++ b/packages/astro/test/fetch.test.js
@@ -0,0 +1,18 @@
+import { suite } from 'uvu';
+import * as assert from 'uvu/assert';
+import { doc } from './test-utils.js';
+import { setup } from './helpers.js';
+
+const Fetch = suite('Global Fetch');
+
+setup(Fetch, './fixtures/fetch');
+
+Fetch('Is available in non-Astro components.', async ({ runtime }) => {
+ const result = await runtime.load('/');
+ assert.ok(!result.error, `build error: ${result.error}`);
+
+ const $ = doc(result.contents);
+ assert.equal($('#jsx').text(), 'function');
+});
+
+Fetch.run(); \ No newline at end of file
diff --git a/packages/astro/test/fixtures/fetch/snowpack.config.json b/packages/astro/test/fixtures/fetch/snowpack.config.json
new file mode 100644
index 000000000..8f034781d
--- /dev/null
+++ b/packages/astro/test/fixtures/fetch/snowpack.config.json
@@ -0,0 +1,3 @@
+{
+ "workspaceRoot": "../../../../../"
+}
diff --git a/packages/astro/test/fixtures/fetch/src/components/Child.jsx b/packages/astro/test/fixtures/fetch/src/components/Child.jsx
new file mode 100644
index 000000000..071473ea9
--- /dev/null
+++ b/packages/astro/test/fixtures/fetch/src/components/Child.jsx
@@ -0,0 +1,7 @@
+import { h } from 'preact';
+
+export default function() {
+ return (
+ <span id="jsx">{ typeof fetch }</span>
+ );
+} \ No newline at end of file
diff --git a/packages/astro/test/fixtures/fetch/src/pages/index.astro b/packages/astro/test/fixtures/fetch/src/pages/index.astro
new file mode 100644
index 000000000..66979efa3
--- /dev/null
+++ b/packages/astro/test/fixtures/fetch/src/pages/index.astro
@@ -0,0 +1,12 @@
+---
+import Child from '../components/Child.jsx';
+---
+
+<html lang="en">
+<head>
+ <title>Global fetch</title>
+</head>
+<body>
+ <Child />
+</body>
+</html> \ No newline at end of file