Load environment variables in deno SSR (#3483)
* Properly shim env in Deno * Add test for loading env vars in Deno * Add changeset
This commit is contained in:
parent
1aeb1d0a00
commit
b795a085f0
5 changed files with 30 additions and 2 deletions
5
.changeset/chilly-sheep-doubt.md
Normal file
5
.changeset/chilly-sheep-doubt.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/deno': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Deno integration now loads environment variables in server runtime
|
|
@ -22,7 +22,7 @@
|
||||||
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
||||||
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
||||||
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
||||||
"test": "deno test --allow-run --allow-read --allow-net ./test/"
|
"test": "deno test --allow-run --allow-read --allow-env --allow-net ./test/"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"astro": "workspace:*",
|
"astro": "workspace:*",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
(globalThis as any).process = {
|
(globalThis as any).process = {
|
||||||
argv: [],
|
argv: [],
|
||||||
env: {},
|
// @ts-ignore
|
||||||
|
env: Deno.env.toObject(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,6 +5,12 @@ async function startApp(cb) {
|
||||||
await runBuildAndStartApp('./fixtures/basics/', cb);
|
await runBuildAndStartApp('./fixtures/basics/', cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this needs to be here and not in the specific test case, because
|
||||||
|
// the variables are loaded in the global scope of the built server
|
||||||
|
// module, which is only executed once upon the first load
|
||||||
|
const varContent = 'this is a value stored in env variable';
|
||||||
|
Deno.env.set('SOME_VARIABLE', varContent);
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: 'Basics',
|
name: 'Basics',
|
||||||
async fn() {
|
async fn() {
|
||||||
|
@ -39,3 +45,17 @@ Deno.test({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: 'Correctly loads run-time env variables',
|
||||||
|
async fn() {
|
||||||
|
await startApp(async () => {
|
||||||
|
const resp = await fetch('http://127.0.0.1:8085/');
|
||||||
|
const html = await resp.text();
|
||||||
|
|
||||||
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
||||||
|
const p = doc.querySelector('p#env-value');
|
||||||
|
assertEquals(p.innerText, varContent);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
---
|
---
|
||||||
import ReactComponent from '../components/React.jsx';
|
import ReactComponent from '../components/React.jsx';
|
||||||
|
const envValue = import.meta.env.SOME_VARIABLE;
|
||||||
---
|
---
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -8,6 +9,7 @@ import ReactComponent from '../components/React.jsx';
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Basic App on Deno</h1>
|
<h1>Basic App on Deno</h1>
|
||||||
|
<p id="env-value">{envValue}</p>
|
||||||
<ReactComponent />
|
<ReactComponent />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue