-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
204: Sharing Code Between Backend and Frontend with Scala.js
I see this all the time when developers first dive into Scala.js: they assume that "sharing code" means they can just point their frontend project at their backend source folder and everything will magically work. They think, "I've already defined my User entity and my database logic in the backend; why write it again for the browser?"
The Fallacy: Sharing the Entire Domain Model
Here is where it falls apart. If you try to share your entire backend domain model—the classes that map directly to your database tables—you're going to hit a wall of compilation errors. Why? Because your backend User entity likely depends on Java-specific libraries, JDBC drivers, or Hibernate annotations. Scala.js cannot compile a Postgres driver to JavaScript. It simply doesn't exist in the browser environment.
If you try to force this, you'll spend your entire afternoon fighting ClassNotFoundException or weird linker errors. The mistake isn't the idea of sharing code; it's the scope of what you're sharing. You cannot share the implementation of your data persistence, but you absolutely should share the definition of your data contracts.
The Fix: The Pure Scala "Common" Module
The professional way to handle this is to create a third project in your build—usually called common or shared. This module must be "Pure Scala." No Java-only dependencies, no server-side frameworks, and no browser-specific DOM APIs. It's just logic and data.
Think of it as the "Source of Truth" for your API. Let's say we're building a shipping application. Instead of defining a ShippingAddress class in both the JVM and JS projects, we put it in common. But we don't stop at just data holders; we put the validation logic there too. This way, the frontend can tell the user their zip code is invalid instantly, and the backend can use the exact same code to reject the request if the frontend was bypassed.
// common/src/main/scala/com/app/models/Address.scala
package com.app.models
case class Address(
street: String,
city: String,
zipCode: String
)
object Address {
// This logic runs on BOTH the JVM and in the Browser
def validate(addr: Address): Either[String, Address] = {
if (addr.zipCode.length != 5) Left("Zip code must be exactly 5 digits")
else if (addr.street.trim.isEmpty) Left("Street cannot be empty")
else Right(addr)
}
}
Now, in your backend (JVM), you import com.app.models.Address to handle the incoming JSON. In your frontend (Scala.js), you import the same class to bind to your form fields. I've found that this eliminates about 90% of the "out of sync" bugs where the backend expects a field that the frontend forgot to send.
One tip from the trenches: keep your common module lean. The moment you add a heavy library to it, you're increasing the bundle size of your JavaScript. If you need a complex library for validation on the backend, keep that in the JVM project and only share the basic case classes and simple logic in the common module.
📋 Practical Task
Build a Shared Product Validator for an E-Commerce Store
Your goal is to implement a shared validation layer to ensure a product's price and SKU are consistent across both the client and the server.
- Part 1: The Common Module: Create a case class
ProductRequestwith fields forname: String,price: Double, andsku: String. Inside a companion object, implement avalidatemethod that returns anEither[String, ProductRequest]. The validation should fail if:- The price is less than or equal to 0.
- The SKU does not start with "PROD-".
- Part 2: The Frontend Mock: Write a small Scala.js function that simulates a "Submit" button click. It should take a
ProductRequest, run the shared validation, and print "Form Error: [message]" or "Sending to server..." to the console. - Part 3: The Backend Mock: Write a JVM function that simulates an API endpoint receiving a
ProductRequest. It must run the same shared validation and return a 400 Bad Request message if it fails, ensuring the backend is not relying solely on the frontend's check.
There are no comments for now.