diff options
author | 2021-08-03 13:13:00 -0400 | |
---|---|---|
committer | 2021-08-03 13:13:00 -0400 | |
commit | 39df7952a5a53e8540b834419e8fa553e45ea7d3 (patch) | |
tree | da749113350fd1b5cb09d0bdac95dff87a7955d5 | |
parent | fc739c24d7c9ae75d377dae6bbe5253c1e5aca02 (diff) | |
download | astro-39df7952a5a53e8540b834419e8fa553e45ea7d3.tar.gz astro-39df7952a5a53e8540b834419e8fa553e45ea7d3.tar.zst astro-39df7952a5a53e8540b834419e8fa553e45ea7d3.zip |
Make fetch available in all component types (#949)
* Make fetch available in all component types
This makes `globalThis.fetch` available in all components.
* Adds a changeset
-rw-r--r-- | .changeset/wet-garlics-beam.md | 5 | ||||
-rw-r--r-- | packages/astro/src/compiler/index.ts | 4 | ||||
-rw-r--r-- | packages/astro/test/fetch.test.js | 18 | ||||
-rw-r--r-- | packages/astro/test/fixtures/fetch/snowpack.config.json | 3 | ||||
-rw-r--r-- | packages/astro/test/fixtures/fetch/src/components/Child.jsx | 7 | ||||
-rw-r--r-- | packages/astro/test/fixtures/fetch/src/pages/index.astro | 12 |
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 |