columbiaroad/wp-ssr
Composer 安装命令:
composer require columbiaroad/wp-ssr
包简介
Server-side rendering for JavaScript apps inside WordPress templates.
README 文档
README
Poor man's Server Side Rendering
This application aims to solve server side rendering of JavaScript applications inside WordPress templates. It runs in two parts. First there is the WordPress plugin which creates new post type for the renders and REST API endpoints to get an array of render objects. Secondly there is Express server which when accessed fetches these render objects and launches puppeteer to render the pages. When the page is rendered and the wanted application part of that page is parsed, it will save the html back to the post through the REST API.
Disclaimer
This application doesn't provide you real time server side rendering but it's suitable for better SEO accessibility for your content. The puppeteer renders the page always as unauthenticated user and it requires that the state changes are reflected in the url.
Setup
WordPress
You can install the plugin using composer:
composer require "columbiaroad/wp-ssr:0.1.2@dev"
First you need to navigate to the plugin settings and fill in the required information. You need to provide an API key to be used for authenticating with the REST API. Then you need to give WordPress the Node application url what to ping for renders. Last you can define the interval when the renders expire.
In the WordPress template you need to request the rendered content before initial render of the header. You can request the content of the application by using WPSSR\Render::render method which takes in three parameters:
string $urlUrl of the page you are requesting to be renderedstring $app_selectorQuery selector for the application root elementstring $waitfor_selectorQuery selector for the puppeteer to wait before rendering the page
<?php /** * Template Name: React Application * */ $content = WPSSR\Render::render( get_permalink(), '#react-app-root', // App selector '#react-app-inner' // Waitfor selector ); get_header(); ?> <div id="react-app-root"> <?php echo $content; ?> </div> <?php get_footer(); ?>
Node App
To start the Node application you need to have couple of environment variables available for it.
WP_DOMAINwhich points to the root of your WordPress installationAPI_KEYis the same API key that you provided in the plugin settings pageNODE_ENVto define the Node environment. This is used in example to ignore puppeteer HTTPS errorsSCHEDULEto define the schedule for rendering cron job
One way to run the application is to use the Docker image provided:
docker run -d \ -e "WP_DOMAIN=https://example.com" \ -e "API_KEY=my-api-key" \ -e "NODE_ENV=production" \ -e "SCHEDULE=*/15 * * * *" \ -p 80:3000 \ --restart=unless-stoppped \ --name=wpssr \ columbiaroadcom/wp-ssr
Publishing package
The composer package is automatically published to columbiaroad/wp-ssr when a github release is made.
To publish to dockerhub you need access to the repository there. Publishing is done as follows:
docker build . --tag columbiaroadcom/wp-ssr:<tag> docker push columbiaroadcom/wp-ssr:<tag>
TODO
- Add
.envfile support for the Node application - Optimize for large amount of renders
- Lock the rendering process to prevent multiple simultaneous render calls
columbiaroad/wp-ssr 适用场景与选型建议
columbiaroad/wp-ssr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.08k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 columbiaroad/wp-ssr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 columbiaroad/wp-ssr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-02-12