Files
SnowLuma/.pnpm-store/v11/files/2a/2982bf0a18820cd682d9650a0b16cddfba16564cb1864c5131a1f83f6b6e497c6d2079fa590a688e1c053e0731d7aa68f26a4117265f7b574705e8613f772e
TanTan 2a69b23b47
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
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

67 lines
1.8 KiB
Plaintext

import { html } from "../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 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
export { defaultPlugin, redirectPlugin };