plugins.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // src/helper/ssg/plugins.ts
  2. import { html } from "../html/index.js";
  3. var defaultPlugin = () => {
  4. return {
  5. afterResponseHook: (res) => {
  6. if (res.status !== 200) {
  7. return false;
  8. }
  9. return res;
  10. }
  11. };
  12. };
  13. var REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
  14. var generateRedirectHtml = (location) => {
  15. const content = html`<!DOCTYPE html>
  16. <title>Redirecting to: ${location}</title>
  17. <meta http-equiv="refresh" content="0;url=${location}" />
  18. <meta name="robots" content="noindex" />
  19. <link rel="canonical" href="${location}" />
  20. <body>
  21. <a href="${location}">Redirecting to <code>${location}</code></a>
  22. </body>
  23. `;
  24. return content.toString().replace(/\n/g, "");
  25. };
  26. var redirectPlugin = () => {
  27. return {
  28. afterResponseHook: (res) => {
  29. if (REDIRECT_STATUS_CODES.has(res.status)) {
  30. const location = res.headers.get("Location");
  31. if (!location) {
  32. return false;
  33. }
  34. const htmlBody = generateRedirectHtml(location);
  35. return new Response(htmlBody, {
  36. status: 200,
  37. headers: { "Content-Type": "text/html; charset=utf-8" }
  38. });
  39. }
  40. return res;
  41. }
  42. };
  43. };
  44. export {
  45. defaultPlugin,
  46. redirectPlugin
  47. };