Backend code called on page load is running twice

I have the page code shown below inside the onReady() function to populate dropdowns for repeater elements. Works fine, but site logging shows the backend async function is running twice each time it is called from the page code.
I think I’ve seen this addressed in help article or in this forum, but cannot find that solution.

  $w("#dynamicDataset").onReady( () => {
  let itemObj = $w("#dynamicDataset").getCurrentItem();
  let ofc=itemObj.ofcGmail;
 getSelectOptions(ofc,"fsCol","ofcCol","catsCol").then(function(opts) {
   $w("#fsDropdown").options= opts;
   });
getSelectOptions(ofc,"newsCol","ofcCol","catsCol").then(function(opts) {
    $w("#newsDropdown").options= opts;
    });    
    getSelectOptions(ofc,"vidCol","ofcCol","catsCol").then(function(opts) {
      $w("#vidDropdown").options= opts;
    });
  });

How should I prevent the backend code from running twice each time it is called from the page code?

Thanks for any guidance.

Global page code and code inside of onReady() will usually run twice by default, once in the backend and once in the browser.

You can use the .env property from the Rendering API to detect and choose which environment the page code runs in.

For example, the following code prevents a CMS item from being saved twice:

import wixData from 'wix-data';
import wixWindowFrontend from 'wix-window-frontend';

let toInsert = {
  "field1": "Some value",
  "field2": "Some other value"
};

$w.onReady(function () {
  if (wixWindowFrontend.rendering.env === "browser") {
    return wixData.insert("myCollection", toInsert)
      .then( (item) => {
        $w("#myText").text = item.title;
      } );
  }
} );

Learn more about the Page Rendering Process for sites built on Wix or Wix Studio in the following help article:

Thanks, Thomas. I got to this solution not too long after I posted the question back in 2022 in the prior Corvid forum. Your reply here will be good for anyone else who is looking into this functionality.
Doug

1 Like