Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Should a single-page app be served from the Spring Boot backend jar or from a CDN with object storage? Discuss deployment, caching, security headers and CORS.

Serving the SPA from the Spring Boot jar gives one deployable, the same origin for pages and API so there is no CORS, and a simple local setup, but frontend releases need backend deploys and the backend serves all static traffic. A CDN with object storage gives independent releases, global caching and less backend load, but needs CORS or a shared domain with path routing at the CDN or gateway. In both cases security headers such as Content-Security-Policy, HSTS and X-Content-Type-Options must be set wherever the HTML is served, cookie authentication is simplest on the same site, and versioned asset names with no-cache on index.html prevent stale mixes after deploys.

Why this question is asked

Where to host the frontend sounds like a small operational choice, but it shapes release processes, security headers, authentication and caching. Teams often start with the SPA in the jar and move to a CDN later, sometimes discovering CORS and cookie problems on the way. Interviewers ask this design question to see whether we can compare the two setups on several dimensions and pick based on the team's situation. In this answer, we will compare the two deployment models, look at caching, security headers, CORS and authentication in each, and end with how to choose.

1The Two Deployment Models

In the first model, the build copies the compiled SPA into the jar under static, and Spring Boot serves index.html and the assets alongside the API. One artifact goes through one pipeline, the browser loads pages and API from the same origin, and a developer runs one process locally.

In the second model, the frontend build uploads its files to object storage, such as an S3 bucket, and a CDN serves them from edge locations near the users. The backend serves only the API. The frontend has its own pipeline and can release ten times a day without touching the backend.

Jar against CDN and object storage
ConcernServed from the jarCDN and object storage
Releasesfrontend change means a backend deployindependent pipelines
Origin and CORSsame origin, no CORSCORS, or one domain with path routing
Static trafficserved by the Java serviceserved by edge caches near users
Local setupone processdev server plus a proxy to the API
Security headersset in Spring Securityset in the CDN response policy
Rollbackroll back the whole servicerepoint to the previous build

2Deployment and Release Coupling

The jar model ties the two release cycles together. A text change in the frontend requires building, testing and deploying the backend service, with its database migrations and rolling restarts. In a small team owning both, that is fine and even useful, because frontend and API change together and can never be out of step.

With separate pipelines, the frontend and backend can be at different versions at the same moment, so the API has to stay backward compatible with the previous frontend, and the frontend must tolerate an API that has not yet been deployed. That discipline is worth having anyway in larger organisations, where separate teams own the two sides.

3Caching After Deploys

In both models the caching rule is the same. Build tools name assets with a content hash, such as app-7c21e9.js, so they can be cached for a year with immutable. index.html keeps the same name and references the current hashes, so it must be served with no-cache, which makes browsers and CDNs revalidate it on each load.

The CDN model adds one more concern: during a deploy, the new index.html must not be visible before the new assets are uploaded. The safe order is to upload the new hashed assets first, keep the old ones for some days for users with the old page still open, and switch index.html last, then purge it from the CDN. Getting this wrong produces the classic blank page after a release, where index.html points to files that do not exist yet.

4Security Headers Wherever the HTML Is Served

Headers that protect the page must come with the HTML document, so they belong to whatever serves index.html. Content-Security-Policy limits where scripts, styles and connections may come from, which is our main protection against cross-site scripting. Strict-Transport-Security tells browsers to use HTTPS only. X-Content-Type-Options: nosniff stops browsers from guessing content types. Frame options or the CSP frame-ancestors directive prevent clickjacking.

In the jar model, Spring Security can add these headers to every response. In the CDN model, the API's headers do not help the page at all: the CDN must add them through a response headers policy or an edge function. A common mistake after moving to a CDN is a page with no CSP at all, because the policy stayed behind in Spring Security.

5CORS and Authentication

When the page comes from app.example.com and the API from api.example.com, the browser treats them as different origins, so the API must send CORS headers that allow that origin, and preflight OPTIONS requests add latency to many calls. We can avoid this by routing paths on one domain at the CDN or gateway: example.com/api/* goes to the backend and everything else to object storage. The browser then sees one origin again.

Step throughA CDN setup that keeps one origin
  1. Browser at example.comone origin, session cookie only
  2. CDN with path routingsecurity headers on HTML, caching rules
  3. Object storageindex.html no-cache, hashed assets immutable
  4. Backend-for-frontend and API at /apiholds tokens, applies CORS only if needed

4 layers, from the top down. Step through them one at a time.

Path routing at the CDN gives independent releases without CORS.

Authentication follows the same logic. Secure, HttpOnly cookies with SameSite=Lax or Strict are the simplest safe choice for a browser app, and they work cleanly when page and API share a site. If we keep separate domains, a backend-for-frontend helps: a small server-side component on the page's domain handles the OAuth login, keeps the access and refresh tokens on the server, and gives the browser only a session cookie. That keeps tokens out of JavaScript, where an XSS bug could steal them.

6How to Choose

For an internal tool or a small product with one team, I would serve the SPA from the jar: it is simple, same-origin and secure by default, and the backend can easily handle the static traffic of a few thousand users. For a public product with users across regions, separate frontend and backend teams, or frequent frontend releases, I would use a CDN with object storage, put both behind one domain with path routing to avoid CORS, set the security headers at the CDN, and use a backend-for-frontend for authentication. The jar model is also a fine starting point that we can move away from later, as long as the frontend already uses hashed assets and relative API paths.

7Follow-up questions

Does serving static files from Spring Boot hurt performance?

For moderate traffic, not much: static files are small, cached by browsers after the first load, and Spring Boot serves them efficiently. It becomes a cost at large scale or for global users, where a CDN's edge locations give much lower latency than a service in one region.

How do you run the frontend locally with a separate deployment?

Developers run the frontend dev server, for example Vite on port 5173, and configure its proxy to send /api calls to the local Spring Boot app on port 8080. The browser sees one origin, which mirrors the production path routing.

How are environment settings, such as the API URL, given to a CDN-hosted SPA?

Either at build time, producing one build per environment, or better at runtime with a small config.json served next to index.html with no-cache, so the same build can be promoted from staging to production.

8Conclusion

In this answer, we compared serving a single-page app from the Spring Boot jar with serving it from a CDN and object storage. The jar gives one deployable, the same origin and a simple setup, but couples releases and puts static traffic on the backend. The CDN gives independent releases, global caching and less backend load, but needs CORS or path routing on one domain. In both, security headers must be set where the HTML is served, cookie authentication is simplest on the same site with a backend-for-frontend keeping tokens server side, and hashed assets with no-cache on index.html avoid stale mixes after deploys.

What to say in the interviewAbout 80 seconds aloud

Open with

I would compare them on releases, origin, caching, headers and authentication. Serving from the jar gives one deployable and one pipeline, page and API on the same origin so there is no CORS, and a one-process local setup.

Then, if they want more

The downside is that every frontend change needs a backend deploy and the Java service serves all the static traffic.

A CDN with object storage gives independent frontend releases, global edge caching and less backend load, but then page and API are on different origins unless I route paths on one domain at the CDN or gateway, which I would do to avoid CORS and preflights.

Whichever I choose, the security headers, Content-Security-Policy, HSTS, nosniff and frame-ancestors, have to be set where the HTML is served, so with a CDN they move from Spring Security into the CDN's response policy. For authentication, secure HttpOnly cookies are simplest on the same site, and if the domains differ a backend-for-frontend keeps OAuth tokens on the server.

For caching, hashed asset names with a year of immutable caching and no-cache on index.html, and in the CDN case upload assets before switching index.html. My default is the jar for internal tools and small teams, and the CDN with path routing for public, multi-region products or separate frontend teams.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Should a single-page app be served from the Spring Boot backend jar or from a CDN with...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/53f83ac1f8/