From 6d61af1fe0db39c33c478a29b42997816915e8df Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Fri, 9 Aug 2024 16:26:16 +0200 Subject: [PATCH] Initial SSR guide version --- .../core-concepts/server-side-rendering.md | 462 ++++++++++++++++++ 1 file changed, 462 insertions(+) create mode 100644 docs/reference-guides/interactivity-api/core-concepts/server-side-rendering.md diff --git a/docs/reference-guides/interactivity-api/core-concepts/server-side-rendering.md b/docs/reference-guides/interactivity-api/core-concepts/server-side-rendering.md new file mode 100644 index 0000000000000..e2f1ed7e11704 --- /dev/null +++ b/docs/reference-guides/interactivity-api/core-concepts/server-side-rendering.md @@ -0,0 +1,462 @@ +WordPress has always been built on the foundation of server-side rendering. Traditionally, when a user requests a WordPress page, the server processes the PHP code, queries the database, and generates the HTML markup that is sent to the browser. + +In recent years, modern JavaScript frameworks like Vue, React, or Svelte have revolutionized the way we build web applications. These frameworks provide reactive and declarative programming models that enable developers to create dynamic, interactive user interfaces with ease. However, when it comes to server-side rendering, these frameworks require a JavaScript-based server (such as NodeJS) to execute their code and generate the initial HTML. This means that PHP-based servers, like WordPress, cannot directly utilize these frameworks without sacrificing their native PHP rendering capabilities. This limitation poses a challenge for WordPress developers who want to leverage the power of reactive and declarative programming while still benefiting from WordPress's traditional server-side rendering strengths. + +The Interactivity API bridges this gap by bringing reactive and declarative programming principles to WordPress without compromising its server-side rendering foundation. The Interactivity API's Server Directive Processing capabilities enable WordPress to generate the initial HTML with the correct interactive state, providing a faster initial render. After the initial server-side render, the Interactivity API's client-side JavaScript takes over, enabling dynamic updates and interactions without requiring full page reloads. This approach combines the best of both worlds: the SEO and performance benefits of traditional WordPress server-side rendering, and the dynamic, reactive user interfaces offered by modern JavaScript frameworks. + +In this guide, we'll explore how the Interactivity API processes directives on the server, enabling WordPress to deliver interactive, state-aware HTML from the initial page load, while setting the stage for seamless client-side interactivity. + +## Server Directive Processing + +Let's start with an example where a list of fruits is rendered using the `data-wp-each` directive. + +The following are the necessary steps to ensure that the directives are correctly processed by the Server Directive Processing of the Interactivity API during the server-side rendering of WordPress. + +### 1. Marking the block as interactive + +First, to enable the server directive processing of the block's directives, you need to add `supports.interactivity` to the `block.json`: + +```json +{ + "supports": { + "interactivity": true + } +} +``` + +### 2. Initializing the global state or local context + +Then, you need to initialize either the global state or the local context that will be used during the server-side rendering of the page. + +If you are using global state, you must use the `wp_interactivity_state` function: + +```php +wp_interactivity_state( 'myFruitPlugin', array( + 'fruits' => array( 'Apple', 'Banana', 'Cherry' ) +)); +``` + +If you are using local context, the initial values are defined with the `data-wp-context` directive itself, either by: + +- Adding it directly to the HTML. + +```html + +``` + +- Using the `wp_interactivity_data_wp_context` helper. + +```php + array( 'Apple', 'Banana', 'Cherry' ), +); +?> + +``` + +### 3. Defining the interactive elements using directives + +Next, you neet to add the necessary directives to the HTML markup. + +```html + +``` + +In this example: + +- The `data-wp-interactive` directive activates the interactivity for the DOM element and its children. +- The `data-wp-each` directive is used to render a list of elements. The directive can be used in `