When building applications with PocketBase, it’s tempting to access it from server-side code in frameworks like SvelteKit or Next.js. However, this approach often indicates architectural issues and should generally be avoided. Here’s why:
When you access PocketBase from your server-side code, requests make two network hops:
This adds unnecessary latency compared to direct client-to-PocketBase communication.
Managing authentication state becomes more complex when you need to transfer JWT tokens between client and server. This often leads to security vulnerabilities when not handled properly.
Accessing PocketBase from a single backend IP address can trigger rate limits more easily than distributed client access. This is intentionally designed to encourage direct client communication.
PocketBase is designed to be accessed directly from client applications. Its built-in security rules provide fine-grained access control without needing a middleware server.
If you need server-side logic or privileged operations, use PocketBase’s JS hooks feature instead of wrapping PocketBase calls in a separate backend:
If you’re using server-side rendering primarily to protect PocketBase access, consider:
While generally an anti-pattern, there are valid cases for server-side PocketBase access:
However, even in these cases, consider whether the functionality could be implemented using PocketBase’s native features first.
Gani also posted about this on the PocketBase site: JS SSR - issues and recommendations when interacting with PocketBase
PocketBase is designed to be a complete backend solution with built-in security and extensibility. Adding an additional server layer often complicates the architecture unnecessarily. Before implementing server-side PocketBase access, consider whether you can:
This will lead to simpler, more maintainable, and more performant applications.