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) Canceled after 0s
59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
import { isDynamicRoute } from "./utils.js";
|
|
//#region src/helper/ssg/middleware.ts
|
|
const SSG_CONTEXT = "HONO_SSG_CONTEXT";
|
|
const X_HONO_DISABLE_SSG_HEADER_KEY = "x-hono-disable-ssg";
|
|
/**
|
|
* @deprecated
|
|
* Use `X_HONO_DISABLE_SSG_HEADER_KEY` instead.
|
|
* This constant will be removed in the next minor version.
|
|
*/
|
|
const SSG_DISABLED_RESPONSE = (() => {
|
|
try {
|
|
return new Response("SSG is disabled", {
|
|
status: 404,
|
|
headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: "true" }
|
|
});
|
|
} catch {
|
|
return null;
|
|
}
|
|
})();
|
|
/**
|
|
* Define SSG Route
|
|
*/
|
|
const ssgParams = (params) => async (c, next) => {
|
|
if (isDynamicRoute(c.req.path)) {
|
|
c.req.raw.ssgParams = Array.isArray(params) ? params : await params(c);
|
|
return c.notFound();
|
|
}
|
|
await next();
|
|
};
|
|
/**
|
|
* @experimental
|
|
* `isSSGContext` is an experimental feature.
|
|
* The API might be changed.
|
|
*/
|
|
const isSSGContext = (c) => !!c.env?.[SSG_CONTEXT];
|
|
/**
|
|
* @experimental
|
|
* `disableSSG` is an experimental feature.
|
|
* The API might be changed.
|
|
*/
|
|
const disableSSG = () => async function disableSSG(c, next) {
|
|
if (isSSGContext(c)) {
|
|
c.header(X_HONO_DISABLE_SSG_HEADER_KEY, "true");
|
|
return c.notFound();
|
|
}
|
|
await next();
|
|
};
|
|
/**
|
|
* @experimental
|
|
* `onlySSG` is an experimental feature.
|
|
* The API might be changed.
|
|
*/
|
|
const onlySSG = () => async function onlySSG(c, next) {
|
|
if (!isSSGContext(c)) return c.notFound();
|
|
await next();
|
|
};
|
|
//#endregion
|
|
export { SSG_CONTEXT, SSG_DISABLED_RESPONSE, X_HONO_DISABLE_SSG_HEADER_KEY, disableSSG, isSSGContext, onlySSG, ssgParams };
|