Server script vs. Client Script

Hello,

i was watching @buildwithhussain 's python essentials video on Youtube, he made a server script to merge Name and Surname to Full Name, why he used server script instead of client script? would not be good if he used client script?

Data Integrity: Server scripts are executed on the server side, which means they can ensure data integrity and consistency.

Security: server script can provide more control and security by enforcing rules and permissions at the server level.

Performance: Server scripts can be more efficient for operations that involve multiple records or complex calculations because they offload the processing to the server, reducing the load on the client’s device.

About Client scripts: They run on the client side, which can provide a more responsive user experience. If users need to see the updated “Full Name” field immediately as they type in the “Name” and “Surname” fields, a client script is a better choice.

@jls thank you for reply, your answer is general to Server Script and Client Script comparison, i mean he may choose Client Script for this basic job but he decided to choose Server Script, is this because he may want to store the full name in db?. Can we make such a distinction that if you want to store your value in db use Server Script.

I try to use Server Scripts all the time. It’s consistent, fast and solid.

Hello again. In the video he tries to demonstrate how python (server script) and Controller Hooks work. Specifically, how it is automatically stored just before saving the document, and as a result of the concatenation of several fields. Considering that python is the language used for server script, it does not make sense to use a client script (javascript).

And as @TurkerTunali says, it is faster, cleaner, solid and secure.

You have another quite interesting video from @buildwithhussain about client scripts: https://www.youtube.com/watch?v=vqVd2cId1ys

@jls yes you are right video title indicates the purpose why he used the Server Script, maybe his example is inappropriate (depends) for Server script, that is why i confused.

@TurkerTunali my Python knowledge is very basic trying to learn it, i would use the Server Script for the same reason, but if the value does not need to be stored I would use the Client script.

I don’t know if i am wrong or right, you can enlighten me.

The server script is useful when you want to ensure that the full name is generated and saved consistently in the database, while the client script is useful for providing an immediate and dynamic response in the user interface. Depending on your specific needs and the workflow of your application, you might use one, both, or neither of these techniques, and instead opt for a concatenation logic directly in the forms or reports where the full name is required.

The way it’s done in the video is a quick and simple method to demonstrate its operation, and considering the objective (to store that data), it is the most appropriate. The example, well… I like spaghetti Bolognese and my wife prefers Napoli style :sweat_smile: :rofl:

3 Likes

@jls i prefer Spaghetti Bolognese too :slight_smile: , thank you for explaining in details.

Client Scripts only run in the browser, but there are more ways to interact with documents (like REST API, Python APIs etc.). Server side code (controller logic) runs in all cases.

In short, all business logic (validations, calculations etc.) must go in server side. Client Scripts should only be used to enhance the user experience: e.g. showing computations in real time as they change fields.

Here is snippet from the ERPNext wiki:

Hope this clarifies your doubt :smiley:

lol :rofl:

@buildwithhussain thank you for reply, it is clear.