Skip to content

Latest commit

 

History

History
104 lines (99 loc) · 2.61 KB

cache-tags.md

File metadata and controls

104 lines (99 loc) · 2.61 KB
type summary tags pcx_content_type title weight layout
example
Send Additional Cache Tags using Workers
Caching
Cache Tags
configuration
Cache Tags using Workers
1001
example

{{}} {{}}

export default {
  async fetch(request) {
    const requestUrl = new URL(request.url);
    const params = requestUrl.searchParams;
    const tags =
      params && params.has("tags") ? params.get("tags").split(",") : [];
    const url =
      params && params.has("uri") ? JSON.parse(params.get("uri")) : "";
    if (!url) {
      const errorObject = {
        error: "URL cannot be empty",
      };
      return new Response(JSON.stringify(errorObject), { status: 400 });
    }
    const init = {
      cf: {
        cacheTags: tags,
      },
    };
    return fetch(url, init)
      .then((result) => {
        const cacheStatus = result.headers.get("cf-cache-status");
        const lastModified = result.headers.get("last-modified");
        const response = {
          cache: cacheStatus,
          lastModified: lastModified,
        };
        return new Response(JSON.stringify(response), {
          status: result.status,
        });
      })
      .catch((err) => {
        const errorObject = {
          error: err.message,
        };
        return new Response(JSON.stringify(errorObject), { status: 500 });
      });
  },
};

{{}} {{}}

export default {
  async fetch(request): Promise<Response> {
    const requestUrl = new URL(request.url);
    const params = requestUrl.searchParams;
    const tags =
      params && params.has("tags") ? params.get("tags").split(",") : [];
    const url =
      params && params.has("uri") ? JSON.parse(params.get("uri")) : "";
    if (!url) {
      const errorObject = {
        error: "URL cannot be empty",
      };
      return new Response(JSON.stringify(errorObject), { status: 400 });
    }
    const init = {
      cf: {
        cacheTags: tags,
      },
    };
    return fetch(url, init)
      .then((result) => {
        const cacheStatus = result.headers.get("cf-cache-status");
        const lastModified = result.headers.get("last-modified");
        const response = {
          cache: cacheStatus,
          lastModified: lastModified,
        };
        return new Response(JSON.stringify(response), {
          status: result.status,
        });
      })
      .catch((err) => {
        const errorObject = {
          error: err.message,
        };
        return new Response(JSON.stringify(errorObject), { status: 500 });
      });
  },
} satisfies ExportedHandler;

{{}} {{}}