Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Middleware Reference

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.

No

Yes

Yes

No

Yes, Cache Hit

No, Cache Miss or Disabled

Timeout/Error

Success

Validation Failed

Validation Success

Error

Success

Incoming Request

Is GET/HEAD and Accepts HTML?

Call next() - Skip Middleware

Is a Resource Path?

Check Cache?

Serve Cached HTML

Fetch Page Data (getPageData)

Call next(Error)

Validate Page Data

Read Fallback HTML File

Inject/Update HTML Metadata

Inject Blocklet SDK JS

Inject Theme Styles/Script

Generate ETag

Cache Response

Set Headers & Send HTML


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

file

string

The path to the fallback HTML file (e.g., index.html). This file must exist.

options

FallbackOptions

An optional configuration object for the middleware.

Returns

Name

Type

Description

middleware

(req: Request, res: Response, next: NextFunction) => Promise<void>

An Express.js middleware function that handles the fallback HTML serving logic.

FallbackOptions Interface#

Name

Type

Description

Default Value

root

string

Optional. The root directory where the file is located. If not provided, file is treated as an absolute path.

undefined

getPageData

(req: Request) => PageData | Promise<PageData>

Optional. An asynchronous function that returns dynamic page-specific data (title, description, ogImage, embed) based on the incoming request.

() => Promise.resolve({})

timeout

number

Optional. The maximum time (in milliseconds) allowed for the getPageData function to execute.

5000

maxLength

number

Optional. The maximum allowed length for title and description fields returned by getPageData.

1000

cacheTtl

number

Optional. The time-to-live (in milliseconds) for cached HTML responses. If 0 or not set, Cache-Control: no-store headers are used to prevent client-side caching.

60 * 1000 (1 minute)

injectBlockletJs

boolean

Optional. Whether to inject the Blocklet SDK client-side JavaScript.

true

PageData Interface#

The PageData object returned by getPageData allows you to customize the HTML metadata.

Name

Type

Description

Default Value

title

string

Optional. The title of the HTML page.

env.appName

description

string

Optional. The description for the HTML page's meta tag.

env.appDescription

ogImage

string

Optional. The URL for the Open Graph image.

env.appUrl + / + SERVICE_PREFIX + /blocklet/og.png

embed

string

Optional. A URL for blocklet-open-embed meta tag, used for Blocklet Open Embed integration.

undefined

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.


Syntax error in textmermaid version 11.6.0