bear/ssr-module
Composer 安装命令:
composer require bear/ssr-module
包简介
Javascript SSR module for BEAR.Sunday
关键字:
README 文档
README
JavaScript view layer for BEAR.Sunday
This module enables you to write views in JavaScript while keeping your application logic in PHP. The JavaScript templates are executed server-side (SSR) for initial rendering and can hydrate on the client for interactivity.
When to Use This Module
- You want to write views in JavaScript (React, Vue, etc.) within a PHP application
- You prefer to keep server-side application logic in BEAR.Sunday while using JavaScript for UI
- You need both server-side rendering and client-side hydration with the same view code
Prerequisites
- PHP 8.2+
- Node.js (for SSR execution)
- V8Js (Optional - for embedded execution without process overhead)
Install
Composer Install
composer require bear/ssr-module
Module Install
$buildDir = dirname(__DIR__, 2) . '/var/www/build'; $this->install(new SsrModule($buildDir));
Place your {app}.bundle.js file in the $buildDir directory. This JS is used for server side rendering (SSR) only.
#[Ssr] Attribute
Basic
use BEAR\SsrModule\Annotation\Ssr; #[Ssr(app: 'index_ssr')] public function onGet(): static { $this->body = [ 'name' => 'World', ]; return $this; }
Add the #[Ssr] attribute to methods where you want SSR. Set the JS application name with app.
JS Render Application
Here is a minimalistic JS application. Export a render function.
Use koriym/js-ui-skeleton to create a JavaScript UI application.
const render = state => ( `Hello ${state.name}` )
State and Metas
In SSR applications, you may need two kinds of data:
state: Public data sent to the client (included in HTML)metas: Server-side only data
Separate them using the state and metas parameters in the #[Ssr] attribute:
use BEAR\SsrModule\Annotation\Ssr; #[Ssr(app: 'index_ssr', state: ['name', 'age'], metas: ['title'])] public function onGet(): static { $this->body = [ 'name' => 'World', 'age' => 4.6E8, 'title' => 'Age of the World', ]; return $this; }
render.js:
const render = (preloadedState, metas) => { return `<html> <head> <title>${escape(metas.title)}</title> </head> <body> <script>window.__PRELOADED_STATE__ = ${serialize(preloadedState)}</script> </body> </html>`; }; export default render;
Cache Modules
For production, use cache modules to improve performance:
APCu Cache
$this->install(new ApcSsrModule($buildDir));
Custom Cache
use BEAR\SsrModule\Annotation\SsrCacheConfig; use Psr\SimpleCache\CacheInterface; // Bind your PSR-16 cache implementation $this->bind(CacheInterface::class) ->annotatedWith(SsrCacheConfig::class) ->to(YourCacheImplementation::class); $this->install(new CacheSsrModule()); $this->install(new SsrModule($buildDir));
JavaScript Runtime
This module uses koriym/baracoa for JavaScript execution, which supports two runtimes:
| Runtime | Pros | Cons |
|---|---|---|
| Node.js (default) | No PHP extension required, easy deployment | Process spawn overhead per render |
| V8Js | Embedded execution, no process overhead | Requires PHP extension installation |
Node.js is used automatically when V8Js is not available.
Performance Note
When using event-driven caching with TTL=0 (cache invalidated by events rather than time), the Node.js process overhead becomes negligible. The rendered HTML is cached indefinitely and invalidated only when the underlying data changes, so JavaScript execution occurs only on cache misses.
bear/ssr-module 适用场景与选型建议
bear/ssr-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「react」 「v8」 「SSR」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bear/ssr-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bear/ssr-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bear/ssr-module 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Async Reactive Postgres Driver for PHP (Non-blocking)
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
A JavaScript server side rendering interface
Simple Items Gatcha with PHP
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-06