Skip to content

feat: Assets resource & Worker Binding #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2025
Merged

feat: Assets resource & Worker Binding #40

merged 4 commits into from
Apr 10, 2025

Conversation

sam-goodwin
Copy link
Owner

@sam-goodwin sam-goodwin commented Apr 10, 2025

const assets = await Assets("static-assets", {
  path: "./dist",
});

const worker = await Worker("my-static-site", {
  name: "my-static-site",
  entrypoint: "./index.ts",
  bindings: {
    ASSETS: assets,
  },
});

Now you can apply Cloudflare's pattern for efficiently serving assets from the cache

export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);

    if (url.pathname.startsWith("/api/")) {
      return new Response("Worker with assets is running!", { 
        status: 200,
        headers: { 'Content-Type': 'text/plain' }
      });
    }
    try {
      return env.ASSETS.fetch(request);
    } catch (err) {
      console.log(err);
      return new Response(\`\${err}\`, { status: 404 });
    }
  }
};

@sam-goodwin sam-goodwin merged commit bdfc2f1 into main Apr 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant