承接 koriym/baracoa 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

koriym/baracoa

Composer 安装命令:

composer require koriym/baracoa

包简介

A JavaScript server side rendering interface

README 文档

README

CI

A JavaScript server side rendering interface for PHP.

Baracoa provides a simple interface for JavaScript server side rendering in PHP.

Prerequisites

  • PHP 8.2+
  • V8Js (optional, PhpExecJs used as fallback)

Installation

composer require koriym/baracoa

Basic

In a JS renderer application, implement render function which takes parameter(s) and return html string.

const render = state => (
  `Hello ${state.name}`
)

Call the render() method with JS app name and values to assign to redner.

$baracoa = new Baracoa($jsDir, new ExceptionHandler());
$html = $baracoa->render('min', ['name' => 'World']);
echo $html; // Hello World

In this example, you need to place min.bundle.js JS file in $jsDir directory. Every page needs own JS view application which is bundled single file by bundler tool like webpack.

Typical entry file is like following code.

import render from './render';
global.render = render;

In next section we see the example of Redux with React applicaiton example.

Redux React

The Server Side

Inject an initial component HTML and initial state into a template to be rendered on the client side. To pass along the state, we add a <script> tag that will attach preloadedState to window.__PRELOADED_STATE__. The preloadedState will then be available on the client side by accessing window.__PRELOADED_STATE__.

We also include our bundle file for the client-side application via a <script> tag. This is whatever output your bundling tool provides for your client entry point.

render.js

import React from 'react';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';
import escape from 'escape-html';
import serialize from 'serialize-javascript';
import App from '../containers/App';
import configureStore from '../store/configureStore';

const render = (preloadedState, metas) => {
  const store = configureStore(preloadedState);
  const root = renderToString(
    <Provider store={store}>
      <App />
    </Provider>,
  );
  return `<!doctype html>
    <html>
      <head>
        <title>${escape(metas.title)}</title>
      </head>
      <body>
        <div id="root">${root}</div>
        <script>
          window.__PRELOADED_STATE__ = ${serialize(preloadedState)}
        </script>
        <script src="/build/index.bundle.js"></script>
      </body>
    </html>
`;
};

export default render;

render() method can pass second parameter as SSR meta data which is only available in server side rendering. Typically this value is used in <header> such as for OGP.

$meta = ['title => 'awesome page'];
$html = $baracoa->render('min', ['name' => 'World'], $meta);

The Client Side

We need to do is grab the initial state from window.__PRELOADED_STATE__ which is rendered in server side, and pass it to our createStore() function as the initial state.

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
import App from '../containers/App';

const preloadedState = window.__PRELOADED_STATE__;
const store = configureStore(preloadedState);

render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root'),
);

Performance boost

$cache = new FilesystemCache() // PSR-16
$baracoa = new CacheBaracoa($appBundleJsPath, new ExceptionHandler(), $cache);

An external "snapshot" is saved to increase performance in each app with CacheBaracoa. Highly recommended in production site. Consider internal snapshot for more performance.

See more detail in this blog post.

Run demo

min

git clone git@github.com:koriym/Koriym.Baracoa.git
cd Koriym.Baracoa
composer install
cd docs/example/min
php index.php
# HelloWorld

handlebar

cd docs/example/handlesbar
npm install
npm run build
php public/index.php
# <!doctype html>
# ...

redux react

cd docs/example/redux
npm install
npm run build
npm start

Install V8Js

macOS

Using shivammathur/homebrew-extensions:

brew install shivammathur/extensions/v8js@8.4

(Replace 8.4 with your PHP version)

Linux

See the V8Js README for installation instructions.

JS UI Application Skeleton

UiSkeleton is a Javascript UI application skeleton with hot module loader, browsersync, test, lint and more for development.

koriym/baracoa 适用场景与选型建议

koriym/baracoa 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2017 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「templating」 「javascript」 「v8」 「SSR」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 koriym/baracoa 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 koriym/baracoa 我们能提供哪些服务?
定制开发 / 二次开发

基于 koriym/baracoa 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 12
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 12
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-31