Fallback HTML
The fallback
middleware is designed for single-page applications (SPAs) to serve a base HTML file, ensuring proper search engine optimization (SEO) and a consistent initial load experience. It dynamically injects critical metadata and Blocklet-specific scripts into the HTML response based on runtime conditions and page-specific data.
This middleware is crucial for Blocklets that render their content client-side, allowing them to still provide rich, server-rendered metadata for crawlers and social media previews. It works in conjunction with other middlewares such as Authentication and CSRF Protection to provide a complete web serving solution.
How It Works#
The fallback
middleware intercepts incoming requests. If the request is a GET or HEAD request, accepts HTML, and is not for a static resource (as determined by RESOURCE_PATTERN
), it proceeds to read a specified base HTML file. It then performs several dynamic modifications to this HTML, including injecting page-specific metadata, Open Graph tags, and Blocklet SDK client-side scripts. It also incorporates a caching mechanism to improve performance for subsequent requests.
fallback
Middleware Function#
The fallback
function returns an Express middleware that serves your SPA's index HTML file.
fallback(file: string, options?: FallbackOptions)
Parameters
Name | Type | Description |
---|---|---|
|
| The path to the fallback HTML file (e.g., |
|
| An optional configuration object for the middleware. |
Returns
Name | Type | Description |
---|---|---|
|
| An Express.js middleware function that handles the fallback HTML serving logic. |
FallbackOptions
Interface#
Name | Type | Description | Default Value |
---|---|---|---|
|
| Optional. The root directory where the |
|
|
| Optional. An asynchronous function that returns dynamic page-specific data ( |
|
|
| Optional. The maximum time (in milliseconds) allowed for the |
|
|
| Optional. The maximum allowed length for |
|
|
| Optional. The time-to-live (in milliseconds) for cached HTML responses. If |
|
|
| Optional. Whether to inject the Blocklet SDK client-side JavaScript. |
|
PageData
Interface#
The PageData
object returned by getPageData
allows you to customize the HTML metadata.
Name | Type | Description | Default Value |
---|---|---|---|
|
| Optional. The title of the HTML page. |
|
|
| Optional. The description for the HTML page's meta tag. |
|
|
| Optional. The URL for the Open Graph image. |
|
|
| Optional. A URL for |
|
Example Response for PageData
(from getPageData
function)
{
"title": "My Custom Page Title",
"description": "A descriptive summary of my page.",
"ogImage": "https://example.com/assets/my-og-image.png",
"embed": "https://example.com/open-embed-config.json"
}
HTML Injection and Modification#
The fallback
middleware dynamically modifies the base HTML file to enhance SEO and integrate Blocklet-specific functionalities. The process involves reading the HTML, applying transformations, and then serving the modified content.