For the latest stable version, please use StreamX Guides 1.0.1! |
Set up page generation with StreamX Rendering Engine service
Serving dynamic content has its drawbacks, including performance overhead due to server-side processing, potential scalability issues, and caching challenges. These issues can negatively impact user experience and increase hosting costs.
StreamX addresses these problems by enabling dynamic content to be pre-generated. Once you have defined the page template and the data, you simply feed them into a StreamX Mesh. There, a configured template engine merges the appropriate template with the data, resulting in page generation. The page(s) are immediately delivered to the web server.
This approach has several benefits:
-
Automatic page regeneration triggered by the publication of new data
-
Faster access to pages since they are pregenerated
In this tutorial, we will generate pages from templates and external source data by using StreamX Mesh. Page generation is handled by the StreamX service, which uses Pebble templates for its templating engine and accepts data in JSON format.
This tutorial covers the following topics:
-
page generation by using the StreamX Rendering Engine, including:
-
managing page templates
-
managing data for the templates
-
Prerequisites
To complete this guide, you will need:
-
Roughly 10 minutes
-
A web browser of your choice
Verify that no other StreamX instance or any other application that uses port 8081 is running. |
Step 1: Get the source files
Clone the Git repository containing source files for the example:
git clone https://github.com/streamx-dev/streamx-docs-resources.git
Step 2: Run the StreamX Mesh
-
Open the terminal and go to
set-up-page-generation-tutorial
inside the cloned project directory. -
Run the StreamX Mesh by using the following command:
streamx run
-
Wait for the following output:
------------------------------------------------------------------- STREAMX IS READY! ------------------------------------------------------------------- ... ------------------------------------------------------------------- Network ID: ... Mesh configuration file: ./mesh.yaml -------------------------------------------------------------------
Step 3: Publish the template and data
-
Publish the
site/template.html
template to therenderers
channel with the following command:streamx publish -s 'template.bytes=file://site/template.html' renderers template.html
-
-s
indicates that an external plain text file is the source for the published content -
renderers
is the channel you are publishing the template to -
template.html
is the publish key
-
-
The StreamX Rendering Engine requires additional context such as:
-
data that triggers page generation,
-
type of generated output is,
-
names of generated results.
To provide the generation details, publish the data with the following command:
streamx publish rendering-contexts pages-rendering-context rendering-contexts/pages-rendering-context.json
-
-
Publish the
data/product_1.json
data ondata
channel with the following command:streamx publish -s 'content.bytes=file://data/product_1.json' data product:1
-
Open your web browser and go to http://localhost:8081/generated/1.html. Verify that the page is accessible and contains information about the published product.
-
Repeat steps 2 and 3 for
data/product_2.json
anddata/product_3.json
streamx publish -s 'content.bytes=file://data/product_2.json' data product:2 streamx publish -s 'content.bytes=file://data/product_3.json' data product:3
Verify that the published data is accessible at the following links:
Step 4: Unpublish the data
In this step, we will unpublish some of the previously published data. Unpublishing will only affect pages generated from that data.
-
Unpublish the data with
product:3
key with the following command:streamx unpublish data product:3
-
Visit http://localhost:8081/generated/3.html again. Confirm that the server responds with 404 error code.
-
Visit http://localhost:8081/generated/1.html and http://localhost:8081/generated/2.html. Confirm that pages generated from
product:1
andproduct:2
data are still published.
Step 5: Update the data
In this step, you will replace the previously published data items with new ones. As in the previous step, only pages generated from this data will be affected.
-
Publish updated
product:1
data with the following command:streamx publish -s 'content.bytes=file://data/product_1_updated.json' data product:1
The new data item differs from the previous one by its name: "Brown Chair".
-
Visit http://localhost:8081/generated/1.html. Confirm that page generated from
product:1
data is updated. -
Visit http://localhost:8081/generated/2.html. Confirm that page generated from
product:2
did not change.
Step 6: Update the template
Now let’s update the template. It will affect all pages generated from that template.
-
Publish updated template with
template.html
key with following command:streamx publish -s 'template.bytes=file://site/template_updated.html' renderers template.html
The new version is different: the title now includes the product ID, and the product category has been added to the list of properties.
-
Visit http://localhost:8081/generated/1.html and http://localhost:8081/generated/2.html. Confirm that both pages have been updated.
Step 7: Unpublish the template
Finally, let’s remove the template. It will affect all pages generated from the template.
-
Unpublish the template with the key
template.html
with the following command:streamx unpublish renderers template.html
-
Visit http://localhost:8081/generated/1.html and http://localhost:8081/generated/2.html. Confirm that the server responds with a 404 status code. Both pages are unpublished because their template is unpublished.
Summary
Congratulations! You have learned how to generate pages based on templates and external sources data with StreamX CLI.