Backend function running twice on page load

Hello all!

I am running into an issue that to the best of my knowledge was not present until recently. On certain pages, I have backend functions that are called during onReady. What I am noticing is that the backend function is running twice when I refresh the page. The client side code is only running once (tested through a series of console.logs and the Site Monitor), but the backend function will still run twice. On some of these, it’s adding duplicate information to a database which is highly undesirable.

I have checked for other callers, misplaced code, etc, and I cannot determine why this could be happening. Here is an example of where I have seen this:

Page: Home
URL: prycd.com
Method: getTotalExportCount
Backend File: stats.jsw

Frontend Snippet

import {getTotalExportCount, getTotalSearches, getTotalUploads} from 'backend/stats.jsw'
...

console.log("this prints once")

let [total_searches, total_export_count, total_upload_count] = await Promise.all([getTotalSearches(),     
getTotalExportCount(), getTotalUploads()])

console.log("this prints once")

Backend Snippet

export async function getTotalExportCount(){
    console.log("this prints twice")

No other function calls getTotalExportCount from anywhere on the site.

Any help would be massively appreciated!

We all ran into this problem at a certain moment. Usually (I repeat, usually) the problem is that in page onReady, some db-insert/update is performed. What then happens is that this code is run once on the backend (the page speed optimizer, you can’t do anything about that) and once on the frontend.
The only way out is, on the frontend, test for rendering environment (https://www.wix.com/velo/reference/wix-window/rendering-obj/env).

About code running twice spontaneously at the backend, I doubt it. But if you want a clearer answer, give us the code, and please not pages and pages, but new, reduced subset which clearly demonstrates your problem.

Hi Giri Zano. That seems to have fixed the code running twice! I wrapped the onReady code block using the suggested link, and the backend query is only running once. Thank you for your help!