-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: polyfill fetch in every ssr scenario * test(fetch): update fetch tests * docs: update data fetching guide to remove caveats about `fetch` and isomorphic usage * refactor: update regex for clarity
- Loading branch information
1 parent
e0e5088
commit 4470c81
Showing
9 changed files
with
103 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Plugin } from 'vite'; | ||
import MagicString from 'magic-string'; | ||
|
||
// https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726 | ||
function isSSR(options: undefined | boolean | { ssr: boolean }): boolean { | ||
if (options === undefined) { | ||
return false | ||
} | ||
if (typeof options === 'boolean') { | ||
return options | ||
} | ||
if (typeof options == 'object') { | ||
return !!options.ssr | ||
} | ||
return false | ||
} | ||
|
||
// This matches any JS-like file (that we know of) | ||
// See https://regex101.com/r/Cgofir/1 | ||
const SUPPORTED_FILES = /\.(astro|svelte|vue|[cm]?js|jsx|[cm]?ts|tsx)$/; | ||
const DEFINE_FETCH = `import fetch from 'node-fetch';\n`; | ||
|
||
export default function pluginFetch(): Plugin { | ||
return { | ||
name: '@astrojs/vite-plugin-fetch', | ||
enforce: 'post', | ||
async transform(code, id, opts) { | ||
const ssr = isSSR(opts); | ||
|
||
// If this isn't an SSR pass, `fetch` will already be available! | ||
if (!ssr) { | ||
return null; | ||
} | ||
|
||
// Only transform JS-like files | ||
if (!id.match(SUPPORTED_FILES)) { | ||
return null; | ||
} | ||
|
||
// Optimization: only run on probable matches | ||
if (!code.includes('fetch')) { | ||
return null; | ||
} | ||
|
||
const s = new MagicString(code); | ||
s.prepend(DEFINE_FETCH); | ||
|
||
const result = s.toString(); | ||
|
||
const map = s.generateMap({ | ||
source: id, | ||
includeContent: true | ||
}); | ||
|
||
return { code: result, map } | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/fetch/src/components/AstroComponent.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<span id="astro-component">{typeof fetch}</span> |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/fetch/src/components/SvelteComponent.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<span id="svelte">{ typeof fetch }</span> |
16 changes: 16 additions & 0 deletions
16
packages/astro/test/fixtures/fetch/src/components/VueComponent.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<template> | ||
<span id="vue">{{ type }}</span> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent } from 'vue' | ||
export default defineComponent({ | ||
setup() { | ||
return { | ||
type: typeof fetch | ||
} | ||
} | ||
}) | ||
</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
--- | ||
import Child from '../components/Child.jsx'; | ||
import Test from '../components/AstroComponent.astro'; | ||
import JsxComponent from '../components/JsxComponent.jsx'; | ||
import SvelteComponent from '../components/SvelteComponent.svelte'; | ||
import VueComponent from '../components/VueComponent.vue'; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Global fetch</title> | ||
</head> | ||
<body> | ||
<Child /> | ||
<span id="astro-page">{typeof fetch}</span> | ||
<Test /> | ||
<JsxComponent /> | ||
<SvelteComponent /> | ||
<VueComponent /> | ||
</body> | ||
</html> | ||
</html> |