Issue dynamically loading video background

I’m having trouble with
I am trying to dynamically load a video background with code. This works in the site preview but in the live site, it shows one video before interrupting it with another. The element does not have a video background in the WYSIWYG editor. I don’t understand why or how this is happening.
My basic code is as follows:

$w.onReady(() => {
    console.log('Changing section background');

    let section = $w('#mySection');
    let background = section.background;

    // randomly choose video URL
    const videoUrl = getVideoUrl();
    if (!background.src || background.src.length == 0) {
        background.src = videoUrl;
    }
});

Again, this works as expected in site preview. In the live site, it appears one video is loaded than interrupted by the code. Based on when the console message appears, it seems the video is loaded before the code is executed. But the element should not have a video background before my code is executed.

Working in
Wix Studio Dev mode

What I’ve tried so far
Clearing cache and cookies, disabling browser addons

Forgot to mention I also tried disabling caching for the page and that didn’t work either, to my surprise.

Have you got a link to the site?

There’s no dataset or anything connected to the section background - it’s all managed via code?

And no there’s no database or anything, just static links

Bumping, does anyone have an idea about this?

Hi, @someguy3000 !!

Could you let us know what kind of URL getVideoUrl() returns? Is it a Wix media URL or an external direct link URL? :face_in_clouds:

I’m suspecting this might be related to server side rendering :thinking:

Have you wrapped the code to ensure it only runs on the browser, rather than server side too:

import { rendering } from "@wix/site-window";

$w.onReady(async function () {
    if ((await rendering.env()) === "browser") {
		// Your code here
    }
});

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.