Inject analytics env (#6876)
This commit is contained in:
parent
a1bac1d5b6
commit
06ca3702f8
5 changed files with 35 additions and 0 deletions
5
.changeset/fifty-guests-remember.md
Normal file
5
.changeset/fifty-guests-remember.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/vercel': patch
|
||||
---
|
||||
|
||||
Correctly handle analytics id where present
|
|
@ -18,6 +18,7 @@ import {
|
|||
writeJson,
|
||||
} from '../lib/fs.js';
|
||||
import { getRedirects } from '../lib/redirects.js';
|
||||
import { exposeEnv } from '../lib/env.js';
|
||||
|
||||
const PACKAGE_NAME = '@astrojs/vercel/edge';
|
||||
|
||||
|
@ -55,6 +56,7 @@ export default function vercelEdge({
|
|||
injectScript('page', 'import "@astrojs/vercel/analytics"');
|
||||
}
|
||||
const outDir = getVercelOutput(config.root);
|
||||
const viteDefine = exposeEnv(['VERCEL_ANALYTICS_ID']);
|
||||
updateConfig({
|
||||
outDir,
|
||||
build: {
|
||||
|
@ -62,6 +64,9 @@ export default function vercelEdge({
|
|||
client: new URL('./static/', outDir),
|
||||
server: new URL('./dist/', config.root),
|
||||
},
|
||||
vite: {
|
||||
define: viteDefine,
|
||||
},
|
||||
...getImageConfig(imageService, imagesConfig, command),
|
||||
});
|
||||
},
|
||||
|
|
15
packages/integrations/vercel/src/lib/env.ts
Normal file
15
packages/integrations/vercel/src/lib/env.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* While Vercel adds the `PUBLIC_` prefix for their `VERCEL_` env vars by default, some env vars
|
||||
* like `VERCEL_ANALYTICS_ID` aren't, so handle them here so that it works correctly in runtime.
|
||||
*/
|
||||
export function exposeEnv(envs: string[]): Record<string, unknown> {
|
||||
const mapped: Record<string, unknown> = {};
|
||||
|
||||
envs
|
||||
.filter((env) => process.env[env])
|
||||
.forEach((env) => {
|
||||
mapped[`import.meta.env.PUBLIC_${env}`] = JSON.stringify(process.env[env]);
|
||||
});
|
||||
|
||||
return mapped;
|
||||
}
|
|
@ -11,6 +11,7 @@ import {
|
|||
import { getVercelOutput, removeDir, writeJson } from '../lib/fs.js';
|
||||
import { copyDependenciesToFunction } from '../lib/nft.js';
|
||||
import { getRedirects } from '../lib/redirects.js';
|
||||
import { exposeEnv } from '../lib/env.js';
|
||||
|
||||
const PACKAGE_NAME = '@astrojs/vercel/serverless';
|
||||
|
||||
|
@ -50,6 +51,7 @@ export default function vercelServerless({
|
|||
injectScript('page', 'import "@astrojs/vercel/analytics"');
|
||||
}
|
||||
const outDir = getVercelOutput(config.root);
|
||||
const viteDefine = exposeEnv(['VERCEL_ANALYTICS_ID']);
|
||||
updateConfig({
|
||||
outDir,
|
||||
build: {
|
||||
|
@ -57,6 +59,9 @@ export default function vercelServerless({
|
|||
client: new URL('./static/', outDir),
|
||||
server: new URL('./dist/', config.root),
|
||||
},
|
||||
vite: {
|
||||
define: viteDefine,
|
||||
},
|
||||
...getImageConfig(imageService, imagesConfig, command),
|
||||
});
|
||||
},
|
||||
|
|
|
@ -8,6 +8,7 @@ import {
|
|||
} from '../image/shared.js';
|
||||
import { emptyDir, getVercelOutput, writeJson } from '../lib/fs.js';
|
||||
import { getRedirects } from '../lib/redirects.js';
|
||||
import { exposeEnv } from '../lib/env.js';
|
||||
|
||||
const PACKAGE_NAME = '@astrojs/vercel/static';
|
||||
|
||||
|
@ -36,11 +37,15 @@ export default function vercelStatic({
|
|||
injectScript('page', 'import "@astrojs/vercel/analytics"');
|
||||
}
|
||||
const outDir = new URL('./static/', getVercelOutput(config.root));
|
||||
const viteDefine = exposeEnv(['VERCEL_ANALYTICS_ID']);
|
||||
updateConfig({
|
||||
outDir,
|
||||
build: {
|
||||
format: 'directory',
|
||||
},
|
||||
vite: {
|
||||
define: viteDefine,
|
||||
},
|
||||
...getImageConfig(imageService, imagesConfig, command),
|
||||
});
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue