Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Skipped
69 lines
2.0 KiB
Plaintext
69 lines
2.0 KiB
Plaintext
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
const require_helper_html_index = require("../html/index.js");
|
|
//#region src/helper/ssg/plugins.ts
|
|
/**
|
|
* The default plugin that defines the recommended behavior.
|
|
*
|
|
* @experimental
|
|
* `defaultPlugin` is an experimental feature.
|
|
* The API might be changed.
|
|
*/
|
|
const defaultPlugin = () => {
|
|
return { afterResponseHook: (res) => {
|
|
if (res.status !== 200) return false;
|
|
return res;
|
|
} };
|
|
};
|
|
const REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([
|
|
301,
|
|
302,
|
|
303,
|
|
307,
|
|
308
|
|
]);
|
|
const generateRedirectHtml = (location) => {
|
|
return require_helper_html_index.html`<!DOCTYPE html>
|
|
<title>Redirecting to: ${location}</title>
|
|
<meta http-equiv="refresh" content="0;url=${location}" />
|
|
<meta name="robots" content="noindex" />
|
|
<link rel="canonical" href="${location}" />
|
|
<body>
|
|
<a href="${location}">Redirecting to <code>${location}</code></a>
|
|
</body>
|
|
`.toString().replace(/\n/g, "");
|
|
};
|
|
/**
|
|
* The redirect plugin that generates HTML redirect pages for HTTP redirect responses for status codes 301, 302, 303, 307 and 308.
|
|
*
|
|
* When used with `defaultPlugin`, place `redirectPlugin` before it, because `defaultPlugin` skips non-200 responses.
|
|
*
|
|
* ```ts
|
|
* // ✅ Will work as expected
|
|
* toSSG(app, fs, { plugins: [redirectPlugin(), defaultPlugin()] })
|
|
*
|
|
* // ❌ Will not work as expected
|
|
* toSSG(app, fs, { plugins: [defaultPlugin(), redirectPlugin()] })
|
|
* ```
|
|
*
|
|
* @experimental
|
|
* `redirectPlugin` is an experimental feature.
|
|
* The API might be changed.
|
|
*/
|
|
const redirectPlugin = () => {
|
|
return { afterResponseHook: (res) => {
|
|
if (REDIRECT_STATUS_CODES.has(res.status)) {
|
|
const location = res.headers.get("Location");
|
|
if (!location) return false;
|
|
const htmlBody = generateRedirectHtml(location);
|
|
return new Response(htmlBody, {
|
|
status: 200,
|
|
headers: { "Content-Type": "text/html; charset=utf-8" }
|
|
});
|
|
}
|
|
return res;
|
|
} };
|
|
};
|
|
//#endregion
|
|
exports.defaultPlugin = defaultPlugin;
|
|
exports.redirectPlugin = redirectPlugin;
|