+client.js

🚧
This is currently being implemented. ETA very soon.

The +client.js file adds client-side JavaScript. The code of +client.js is executed only once at the very beginning when the user goes to your website.

It's usually used for initializing the client-side code of error tracking, user tracking, cookie consent, CSS frameworks, etc.

// /pages/+client.js
// Environment: client
 
console.log("I'm the first line of client-side JavaScript.")

If you inspect the HTML of your page, you'll see that it's the first JavaScript file to be referenced, and thus the first JavaScript file to be executed.

The code of +client.js isn't re-executed upon client-side navigation. For executing code upon the lifecycle of page rendering, see following hooks instead:

If you don't use a UI framework Vike extension (vike-react/vike-vue/vike-solid), then also see onRenderClient() hook.

Examples

See also