Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support configurable deployment basepath #158

Open
wants to merge 4 commits into
base: canary
Choose a base branch
from

Conversation

feugy
Copy link
Member

@feugy feugy commented Nov 18, 2024

🖖 What's in there?

Since ages, we're considering the possibility to configure, at build time, the basepath used for both downloading the injected script and reporting the collected metrics.

This PR brings such capability, and works with our supported frameworks: plain React (CRA), Next, Nuxt, Vue, Sveltkit, Astro, Remix (only when used with vite).

🤺 How to test?

Check the vercel deployments of each example app.
To reproduce locally, and set custom basepath:

pnpm i && pnpm -F @vercel/analytics build 

Then:

  1. For Nextjs app

    NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH="/abc" pnpm -F nextjs build
    pnpm -F nextjs start

    browse to http://localhost:3000 and check your console: it's trying (and failing) to download /abc/insights/script.js

  2. For Nextjs 15 app

    NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH="/def" pnpm -F nextjs-15 build
    pnpm -F nextjs-15 start

    browse to http://localhost:3000 and check your console: it's trying (and failing) to download /def/insights/script.js

  3. For Sveltekit app

    VITE_VERCEL_OBSERVABILITY_BASEPATH="/hij" pnpm -F sveltekit build
    pnpm -F sveltekit preview

    browse to http://localhost:4173 and check your console: it's trying (and failing) to download /hij/insights/script.js

  4. For vue app

    VITE_VERCEL_OBSERVABILITY_BASEPATH="/klm" pnpm -F vue build
    pnpm -F vue preview

    browse to http://localhost:4173 and check your console: it's trying (and failing) to download /klm/insights/script.js

  5. For nuxt app

    VITE_VERCEL_OBSERVABILITY_BASEPATH="/nop" pnpm -F nuxt build
    pnpm -F nuxt preview

    browse to http://localhost:3000 and check your console: it's trying (and failing) to download /nop/insights/script.js

  6. For remix app

    VITE_VERCEL_OBSERVABILITY_BASEPATH="/qrs" pnpm -F remix build
    pnpm -F remix start

    browse to http://localhost:3000 and check your console: it's trying (and failing) to download /qrs/insights/script.js

  7. For astro app

    PUBLIC_VERCEL_OBSERVABILITY_BASEPATH="/tuv" pnpm -F astro build
    pnpm -F astro preview

    browse to http://localhost:4321 and check your console: it's trying (and failing) to download /tuv/insights/script.js

There is no example for CRA.

🔬Notes to reviewers

This is a generic solution, heavily inspired by @emspishak (thanks for the work!), and would replace #156.

I've changed the example app for Remix to used the vite, as per the latest official recommendation (remix 2.14 to date).

I've also replaced Jest with vitest, because the primer couldn't handle import.meta properly. And it's superior in my opinion (look at the reduced number of deps and shorter configuration).

🔗 Related PRs

Copy link

vercel bot commented Nov 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
analytics-astro ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 8:13am
analytics-next15 🛑 Canceled (Inspect) Nov 22, 2024 8:13am
analytics-nextjs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 8:13am
analytics-nuxt 🛑 Canceled (Inspect) Nov 22, 2024 8:13am
analytics-remix ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 8:13am
analytics-sveltekit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 8:13am
analytics-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 8:13am

Copy link

socket-security bot commented Nov 18, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@remix-run/[email protected] environment, filesystem Transitive: eval, network, shell, unsafe +498 55.6 MB mjackson
npm/@remix-run/[email protected] Transitive: environment, eval, filesystem, network +48 17 MB mjackson
npm/@remix-run/[email protected] environment, network Transitive: filesystem +10 5.45 MB mjackson
npm/@remix-run/[email protected] Transitive: environment, eval, filesystem, network, unsafe +126 19.7 MB mjackson
npm/@swc/[email protected] None +2 208 kB kdy1, kwonoj
npm/@types/[email protected] None +1 2.36 MB types
npm/@types/[email protected] None 0 0 B
npm/@vitejs/[email protected] None 0 198 kB antfu, patak, soda, ...3 more
npm/[email protected] None 0 27.3 kB omrilotan
npm/[email protected] environment, filesystem +3 372 kB ai
npm/[email protected] environment +3 4.63 MB react-bot
npm/[email protected] environment +2 339 kB react-bot
npm/[email protected] None +17 4.77 MB conduitry, rich_harris, svelte-admin
npm/[email protected] None +17 4.77 MB conduitry, rich_harris, svelte-admin
npm/[email protected] eval, filesystem Transitive: environment, network, shell, unsafe +77 10.1 MB egoist
npm/[email protected] Transitive: environment, filesystem, network, shell +3 6.07 MB antfu, patak, soda, ...2 more
npm/[email protected] environment, eval Transitive: filesystem, unsafe +19 16.1 MB yyx990803

🚮 Removed packages: npm/@remix-run/[email protected], npm/@remix-run/[email protected], npm/@remix-run/[email protected], npm/@remix-run/[email protected], npm/@remix-run/[email protected], npm/@sveltejs/[email protected], npm/@swc/[email protected], npm/@types/[email protected], npm/@vercel/[email protected], npm/@vitejs/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

Copy link

socket-security bot commented Nov 18, 2024

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSourceCI
Possible typosquat attack npm/[email protected] ⚠︎

View full report↗︎

Next steps

What is a typosquat?

Package name is similar to other popular packages and may not be the package you want.

Use care when consuming similarly named packages and ensure that you did not intend to consume a different package. Malicious packages often publish using similar names as existing popular packages.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

@feugy feugy force-pushed the damien/log-766-implement-web-analyticsspeed-insights-randomised-intake branch from a809cb5 to 2f29274 Compare November 18, 2024 11:02
@feugy feugy force-pushed the damien/log-766-implement-web-analyticsspeed-insights-randomised-intake branch from 2f29274 to 4a65858 Compare November 18, 2024 11:05
@feugy feugy force-pushed the damien/log-766-implement-web-analyticsspeed-insights-randomised-intake branch from 4a65858 to 705a0f0 Compare November 18, 2024 13:25
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error -- only TS during build time will complain.
// @ts-ignore -- Rollup will fail to generate d.ts because it doesn't know import.meta.env.
const viteValue = import.meta.env
.VITE_VERCEL_OBSERVABILITY_BASEPATH as string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also check for typeof import === 'undefined' like for projectId?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't have to, because import doesn't behave like process. process is a variable that belongs to the global namespace, while import is a keyword.
So it doesn't have a type, that's why I've wrapped it within a try-catch.

Copy link

@kakadiadarpan kakadiadarpan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good, tested with the provided usecases and default behaviour (without any ENV var).

Thanks for providing clear instructions! 🙌

packages/web/src/utils.ts Outdated Show resolved Hide resolved
@feugy feugy changed the base branch from main to canary November 22, 2024 07:59
@feugy feugy force-pushed the damien/log-766-implement-web-analyticsspeed-insights-randomised-intake branch from a450c58 to 23388cb Compare November 22, 2024 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants