> For the complete documentation index, see [llms.txt](https://whoisandywhite.gitbook.io/porterwp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whoisandywhite.gitbook.io/porterwp/features/site-editor-preview-optimiser.md). Agents can also search and read this documentation through its [MCP server](https://whoisandywhite.gitbook.io/porterwp/~gitbook/mcp).

# Site Editor preview optimiser

PorterWP Premium reduces repeated work in the Site Editor's template, template-part, and pattern collection screens. It de-duplicates eligible REST requests and substitutes lightweight ACF block placeholders where a full live render would be wasteful.

The optimizer is enabled by default in PorterWP 1.4.95. Its assets are only enqueued on the Site Editor screen, and its server paths enforce their own request checks.

## Scope

The default collection roots are:

* `template`
* `templates`
* `template-parts`
* `pattern`
* `patterns`

A request is considered a collection view only when `site-editor.php` uses one of those `p` route roots and does not include `postId`, `postType`, or `canvas`. Opening an individual template, template part, or pattern therefore returns to WordPress and ACF's normal live editing behavior.

## Client-side request de-duplication

On a collection screen, PorterWP wraps `wp.apiFetch` and de-duplicates selected GET requests by normalized REST route and sorted query string. The default browser lifetime is 10,000 milliseconds.

The client considers the preview-related WordPress endpoints used for media, template parts, server-rendered blocks, reusable blocks, taxonomies, editor post collections, and bounded taxonomy probes. Other routes and any request with `parse: false` pass through unchanged.

Each consumer receives a clone of the response rather than a shared mutable object. Failed requests are removed from the cache immediately. A successful non-GET API request clears the browser cache, as do `popstate` and `hashchange` navigation.

Set the client lifetime to zero to disable browser caching while retaining the rest of the optimizer:

```php
add_filter( 'porterwp/acf_preview_optimizer/client_cache_ttl', '__return_zero' );
```

## Server-side REST cache

The server cache runs at `rest_dispatch_request`, after WordPress has matched the route and approved its permission callback. It never serves an entry from `rest_pre_dispatch`, because doing so could bypass route permissions.

An eligible request must:

* use `GET`;
* come from a logged-in user with `edit_theme_options`;
* carry a same-host Site Editor collection referer;
* contain no more than 20 query parameters and no more than 4,096 bytes of JSON-serialized query data;
* target one of the preview-related WordPress REST routes.

Only successful 2xx `WP_REST_Response` objects are cached. Response data, status, headers, and links are preserved.

A cache hit is served without writing the response back into the cache, so the TTL is an absolute lifetime from the original response rather than a sliding window that can keep stale editor data alive indefinitely.

The default server lifetime is 30 seconds. With an external persistent object cache, entries use the `porterwp_site_editor_previews` group. Otherwise, PorterWP uses a WordPress transient as a short-lived database-backed fallback.

Keys include the current blog, user, active stylesheet, REST route, and sorted query parameters. With an external object cache they also include WordPress' `posts` and `terms` last-changed values. The transient fallback relies on its short expiry for invalidation.

Disable only the server cache by returning zero:

```php
add_filter(
    'porterwp/acf_preview_optimizer/rest_cache_ttl',
    function ( $seconds, WP_REST_Request $request, WP_REST_Response $response ) {
        return 0;
    },
    10,
    3
);
```

## Lightweight ACF previews

On a collection screen, ACF `acf/ajax/fetch-block` preview requests can receive a small placeholder containing the block name instead of an expensive full preview. Validation requests are always passed through.

The browser normally supplies the synthetic placeholder without making the AJAX request. It builds that placeholder only from block data already present in the editor; it does not retrieve protected server data. The server path remains as a guarded fallback. Before short-circuiting ACF on the server, PorterWP checks ACF's AJAX verification when available and confirms that the current user can edit the target post or has the configured ACF block render capability.

The placeholder path prepares enough block metadata for a stable response but does not run the block's render callback. ACF forms are omitted from collection previews by default. Full fields and live rendering remain available after the item is opened for editing.

Requests with `query[validate]` enabled bypass the lightweight path. This keeps ACF validation behavior intact.

## Optional preview-frame replacement

`disable_preview_frames` defaults to `false`. When explicitly enabled, PorterWP observes the collection screen and replaces preview iframes and empty preview containers with a “Preview paused” panel:

```php
add_filter(
    'porterwp/acf_preview_optimizer/disable_preview_frames',
    '__return_true'
);
```

This is more aggressive than request de-duplication, so enable it only after confirming that editors prefer collection speed over rendered thumbnails.

## Filters

| Filter                                                  | Default                                       | Purpose                                                                                                                                            |
| ------------------------------------------------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `porterwp/acf_preview_optimizer/enabled`                | `true`                                        | Disable the complete optimizer.                                                                                                                    |
| `porterwp/acf_preview_optimizer/is_site_editor_screen`  | Detected `WP_Screen` result                   | Adjust Site Editor screen detection. Receives the result and screen object.                                                                        |
| `porterwp/acf_preview_optimizer/collection_routes`      | Default route list above                      | Change recognized collection route roots.                                                                                                          |
| `porterwp/acf_preview_optimizer/intercept_rest`         | `true`                                        | Enable both client REST interception and server REST caching.                                                                                      |
| `porterwp/acf_preview_optimizer/client_cache_ttl`       | `10000`                                       | Browser cache lifetime in milliseconds; `0` disables it.                                                                                           |
| `porterwp/acf_preview_optimizer/rest_cache_ttl`         | `30`                                          | Server cache lifetime in seconds; receives the request and response; `0` disables it.                                                              |
| `porterwp/acf_preview_optimizer/should_optimize_block`  | Whether the name starts with `acf/`           | Decide whether the server fallback can use a placeholder. Receives the block name and request data.                                                |
| `porterwp/acf_preview_optimizer/placeholder_html`       | PorterWP placeholder                          | Customize server-generated placeholder markup. Receives the block, request, and editor context; trusted callbacks are responsible for safe output. |
| `porterwp/acf_preview_optimizer/render_form`            | `false` in collection views                   | Include an ACF form in a server-generated lightweight response. Receives the request and editor context.                                           |
| `porterwp/acf_preview_optimizer/disable_preview_frames` | `false`                                       | Replace collection preview frames with paused panels.                                                                                              |
| `porterwp/acf_preview_optimizer/editor_post_types`      | `wp_template`, `wp_template_part`, `wp_block` | Change post types used when calculating target-route context.                                                                                      |
| `porterwp/acf_preview_optimizer/is_target_route`        | Calculated context flag                       | Alter the target-route flag supplied to downstream placeholder filters.                                                                            |

To disable every optimization:

```php
add_filter( 'porterwp/acf_preview_optimizer/enabled', '__return_false' );
```

Keep customizations narrowly scoped. Broadening collection routes or block eligibility can suppress previews that editors expect to be live.

The three block/form/HTML filters above apply to the guarded server fallback. The normal browser-generated placeholder applies to every `acf/` block on a recognized collection screen. Disable the complete optimizer if that client behavior is unsuitable; disabling `intercept_rest` only disables REST interception and caching.

## Security boundaries

* Cached REST responses are read only after the endpoint's permission callback.
* Entries are isolated by site, user, theme, route, and parameters.
* A same-host Site Editor referer and `edit_theme_options` are required.
* Non-GET responses and errors are never stored.
* ACF validation, nonce, and edit-capability checks are not replaced on the server path.

## Compatibility note for 1.4.94 integrations

The public method `maybe_return_cached_rest_preview_response()` remains present for source compatibility, but it is intentionally a no-op in 1.4.95. Do not call or hook it to `rest_pre_dispatch`. Use the documented filters to customize the permission-safe optimizer instead.
