middleware.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // src/helper/ssg/middleware.ts
  2. import { isDynamicRoute } from "./utils.js";
  3. var SSG_CONTEXT = "HONO_SSG_CONTEXT";
  4. var X_HONO_DISABLE_SSG_HEADER_KEY = "x-hono-disable-ssg";
  5. var SSG_DISABLED_RESPONSE = (() => {
  6. try {
  7. return new Response("SSG is disabled", {
  8. status: 404,
  9. headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: "true" }
  10. });
  11. } catch {
  12. return null;
  13. }
  14. })();
  15. var ssgParams = (params) => async (c, next) => {
  16. if (isDynamicRoute(c.req.path)) {
  17. ;
  18. c.req.raw.ssgParams = Array.isArray(params) ? params : await params(c);
  19. return c.notFound();
  20. }
  21. await next();
  22. };
  23. var isSSGContext = (c) => !!c.env?.[SSG_CONTEXT];
  24. var disableSSG = () => async function disableSSG2(c, next) {
  25. if (isSSGContext(c)) {
  26. c.header(X_HONO_DISABLE_SSG_HEADER_KEY, "true");
  27. return c.notFound();
  28. }
  29. await next();
  30. };
  31. var onlySSG = () => async function onlySSG2(c, next) {
  32. if (!isSSGContext(c)) {
  33. return c.notFound();
  34. }
  35. await next();
  36. };
  37. export {
  38. SSG_CONTEXT,
  39. SSG_DISABLED_RESPONSE,
  40. X_HONO_DISABLE_SSG_HEADER_KEY,
  41. disableSSG,
  42. isSSGContext,
  43. onlySSG,
  44. ssgParams
  45. };