
內容簡介
Pods AJAX Views 提供了一種簡單的方式從 AJAX 生成快取視圖,當它們還沒有被快取時。如果該視圖已經生成,它將直接從緩存提供並輸出到頁面上。
此外掛程式需要運行 Pods Framework 的版本為 2.4.1 或更新版。
為什麼需要 AJAX Views?
如果您正在使用 pods_view 來快取模板輸出,那麼您已經在通過其強大的局部頁面快取來改善網站的性能了。但是,如果您有一個複雜的視圖可能需要幾秒鐘才能生成呢?如果您在同一頁面中有幾個這樣的視圖呢?
在像 WPEngine 這樣的主機上,有一個嚴格的 30 秒限制,然後發送一個 502 伺服器錯誤給訪問者。等待頁面載入很長時間,或者特別是被發送這樣的伺服器錯誤,對訪問者來說是無法接受的,他們可能會選擇退出您的網站。
Pods AJAX Views 可以將這些複雜的視圖轉嫁到單獨的異步 AJAX 請求中,這允許頁面的其餘部分進行構建和發送到瀏覽器。當 AJAX 請求運行時,該視圖像通常一樣被快取,因此對 Pods AJAX View 代碼的後續呼叫將產生與默認 pods_view 相同的結果。
用法
使用方式與 pods_view 相同,它接受相同的參數,除了一個額外的參數 $ forced_regenerate,它可以設置為 true(預設值為 false),這將強制刪除快取的視圖並重新載入。
pods_ajax_view('my-big-cached-template.php');
AJAX 請求是通過相同的 URL 進行的,因此您仍然可以像通常一樣訪問查詢和發佈數據。我們在 template_redirect 中進行鉤取操作,以停止 AJAX 視圖是從 AJAX 載入的,而不是通過當前頁面。它們沒有訪問當前的 WP_Query 或查詢變數,也沒有文章循環。如果您想將任何內容傳遞到其中,請使用 $data 參數。
有關 pods_view 的資訊,請參閱以下資源:
使用 Pods 进行局部页面缓存和智能模板部分
代碼參考:pods_view
可用常數
define('PODS_AJAX_VIEWS_STATS', true) - 创建统计和再生成功能的表格;激活之前必须启用,如果您激活它,只需再次停用/启用即可
define('PODS_AJAX_VIEWS_OVERRIDE', true) - 覆盖主题中的所有 pods_view() 调用并将其转换为 AJAX 视图,即使它们未设置缓存(它将通过 AJAX 加载非缓存版本)
define('PODS_AJAX_VIEWS_API_KEY', 'abcdefghijk') - 随机生成此应高度随机,它是用于访问通过您的网址 /?pods_ajax_view_action=view_sitemap&pods_ajax_view_api_key=XXXX 的站点地图的 API 密钥,必须通过用户(或匿名用户)访问,再生可能是因为 URL 对于用户而言是具体的,并且具有非数值。参数;您可以将其与插件一起使用,例如 Warm Cache,以保持视图始终新鲜且始终生成
define('PODS_AJAX_VIEWS_SITEMAP_LIMIT', -1) - 您可以覆盖将显示的站点地图项目的数量,默認值為 250
貢獻者
請查看我們的 GitHub,以了解貢獻者列表,或搜索我們的 GitHub 问题,以查看參與添加功能,修復錯誤或報告問題/測試的所有人。
GitHub
外掛標籤
開發者團隊
原文外掛簡介
Pods AJAX Views offers an easy way to generate cached views from AJAX when they haven’t been cached yet. It will serve straight from cache and output on the page if the view has already been generated.
This plugin requires the Pods Framework version 2.4.1 or later to run.
Why AJAX Views?
If you are using pods_view to cache template output, you’re already on the right track to improving performance of your site through it’s powerful Partial Page Caching. However, what if you have a complicated view that may take a few seconds to generate? What if you have a few of those views on the same page?
On hosts like WPEngine, there’s a strict 30 second limit and then a 502 server error is sent to the visitor. Waiting a long time for a page to load, or especially being sent a server error like that just isn’t acceptable for visitors and they may choose to bail on your site.
Pods AJAX Views takes those complicated views and lets you off-load them into a separate asynchronous AJAX call that allows the rest of the page to be built and sent to the browser. When the AJAX request runs, the view is cached like normal, so subsequent calls to the Pods AJAX View code will produce the exact same result as a default pods_view would.
Usage
Use the same way as pods_view, it accepts the same arguments, except one additional argument $forced_regenerate at the end which can be set to true (default: false) that will force the view to be deleted from cache and reloaded.
pods_ajax_view( 'my-big-cached-template.php' );
AJAX requests are done through the same URL, so you still have access to the query and postdata like normal. We hook into template_redirect to stop Beware that AJAXed views are loaded from AJAX, not through the current page. They do not have access to the current WP_Query or Query variables, or the Post loop. If you want to pass anything into it, use the $data argument.
For information about pods_view, see these resources:
Partial Page Caching and Smart Template Parts with Pods
Code Reference: pods_view
Available Constants
define( 'PODS_AJAX_VIEWS_STATS', true ) – Creates a table for Stats tracking and regeneration ability; Must be enabled before activating, if you enable it, just deactivate / activate again
define( 'PODS_AJAX_VIEWS_OVERRIDE', true ) – Overrides all pods_view() calls in the theme and turns them into AJAX views, even if they aren’t set to cache (it’ll load the non-cached version via AJAX)
define( 'PODS_AJAX_VIEWS_API_KEY', 'abcdefghijk' ) – This should be highly randomized, it’s the API key used to access the sitemap at yoursite.com/?pods_ajax_view_action=view_sitemap&pods_ajax_view_api_key=XXXX which must be accessed through the user (or anon) the regeneration will be run as because the URLs are specific to the user and have nonces on them; You can use this with a plugin like Warm Cache to keep your views fresh and always generated
define( 'PODS_AJAX_VIEWS_SITEMAP_LIMIT', -1 ) – You can override how many sitemap items will show, default is 250
Contributors
Check out our GitHub for a list of contributors, or search our GitHub issues to see everyone involved in adding features, fixing bugs, or reporting issues/testing.
github.com/pods-framework/pods-ajax-views/graphs/contributors
