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

Nuxt: window is not defined #157

Closed
larrasu opened this issue Nov 15, 2024 · 5 comments
Closed

Nuxt: window is not defined #157

larrasu opened this issue Nov 15, 2024 · 5 comments

Comments

@larrasu
Copy link

larrasu commented Nov 15, 2024

I followed the documentation to a T and got the error window is not defined when I try to add the <Analytics/> on the app.vue.

@feugy
Copy link
Member

feugy commented Nov 15, 2024

Hello @larrasu, thanks for the bug report.

Could you share a reproduction please?
We have a working example for Nuxt, and one for vue, so I'll need your help to find out what's wrong.

@Viserion77
Copy link

Same here!
I have a sample of the bug in this branch:
feat-add-@vercel/analytics

to run the project and see the error just install the dependencies and run npx nuxt dev

@Viserion77
Copy link

Trying to trace the origin of the error...

I believe everything starts here:

import { createComponent } from '../vue/create-component';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any
export const Analytics = createComponent('nuxt');

For Nuxt, the createComponent from Vue is being used. In turn, inside the setup function, there's a validation on the window prop, which causes the error to occur:

I'm studying SSR in Nuxt, and I noticed that window only exists after Vue's mounted lifecycle hook...

This even makes me question whether analytics works in this kind of architecture, since everything fails during the injection in this validation:

if (!isBrowser()) return;

@feugy
Copy link
Member

feugy commented Nov 19, 2024

Hey folks!
I'm able to reproduce this. Very annoyingly, this does not happen on the example app, possibly because of intra-monorepo dependencies.

@Viserion77 Analytics should only happen in client side, when a user actually visit your page. So it's expected that we skip everything when (pre)rendering the page on server side.

@feugy
Copy link
Member

feugy commented Nov 19, 2024

@larrasu, @Viserion77 thanks again for the bug report. Version 1.4.1 is out.

I'll close this bug, please re-open it if the fix isn't working as expected !

@feugy feugy closed this as completed Nov 19, 2024
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

No branches or pull requests

3 participants