Building a backend-for-frontend with Next.js and Laravel

By Next Template · Engineering · 4 min read ·

Why we put a thin backend-for-frontend between the browser and a Laravel API, what it handles, and the mistakes that taught us where its boundaries should be.

Cover image for "Building a backend-for-frontend with Next.js and Laravel"

Most of our recent projects pair a Next.js front end with a Laravel API. For a long time the browser talked to the API directly, with a token stored somewhere in JavaScript. It worked, but every project ended with the same uncomfortable questions about token storage, CORS rules and leaking internal URLs. A backend-for-frontend, or BFF, answers most of those questions by putting a small server layer that belongs to the front end between the browser and the API.

What the BFF is responsible for

The BFF is not a second backend. It owns a short list of jobs, and we keep that list written down in every project so it does not grow quietly.

Sessions and tokens

The browser holds an encrypted, HTTP-only session cookie. The API token lives inside that session and never reaches client-side JavaScript. When a Server Component or Route Handler needs data, it reads the session on the server and attaches the token to the outgoing request. A stolen script cannot read a token that the browser never sees.

A trusted channel to the API

Every request from the BFF carries a shared secret and the origin the visitor actually used. The API rejects anything without both. This turns the API into a private service that only our front end can call, while still letting the API know which public domain a request came from.

Shaping responses

The BFF can combine two API calls into one response, rewrite storage URLs so media is served from the same origin, and strip fields the page does not need. It should not contain business rules. If we catch ourselves writing a permission check in the BFF, that check belongs in Laravel.

What stays in Laravel

Validation, authorization, queues, notifications, search indexing and anything that touches the database stay in the API. Laravel is very good at these jobs, and keeping them in one place means a mobile app or an integration can reuse them later without copying logic from the front end.

Mistakes that shaped the boundaries

  • We once cached a user-specific response in a shared cache. The fix was simple, but it taught us to mark every BFF fetch as either public or private from the start.
  • We let the BFF format dates and currencies for a while. It felt convenient until the API needed the same formatting for emails. Formatting now lives in the API or in shared front-end helpers, never in both.
  • We forgot that public pages calling the session become dynamic. Moving the account button into a small client component let the rest of the page be cached again.

Performance and caching

Public pages fetch SEO data and content through a separate helper that never touches cookies or headers. Those responses are cached under tags, and the API calls a protected revalidation route whenever content changes. Visitors get static pages, editors see their changes within seconds, and the API is not called on every page view.

When a BFF is not worth it

For a small internal tool with a handful of users, a direct API with a well-configured session cookie can be enough. The BFF earns its keep when there is a public site that must be fast, a private area that must be secure, and more than one client that will eventually use the API.

Summary

A BFF is a thin, boring layer, and that is the point. It keeps tokens away from the browser, gives the API a trusted caller, and lets public pages stay static. Write down its responsibilities, keep business rules in the API, and review the list whenever a new feature tempts you to add one more thing to the middle layer.

Putting it into practice

If you want to try these ideas, start small. Pick one screen, one job or one page in a project you already know well, apply a single change from this post and measure the result for a couple of weeks. Small, measured changes are easier to explain to a team and much easier to undo if they do not work.

We write these notes from real client work, so every recommendation here has been tested on a production system. If you have a question about engineering, a different experience to share, or a project where these ideas could help, the contact page is the quickest way to reach us, and we read every message.

  • Next.js
  • Laravel
  • Architecture
  • Security

Back to all articles · See related projects

Have a project in mind?

Tell us about the problem you want to solve. We reply within two working days with honest feedback and a sensible first step.

Start a conversation