Skip to content
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

πŸ› Bug Report β€” Runtime APIs -- Using Cache API in HtmlRewriter while caching the response causes a deadlock #2498

Open
Tyler-OBrien opened this issue Aug 8, 2024 · 0 comments

Comments

@Tyler-OBrien
Copy link

Tyler-OBrien commented Aug 8, 2024

Hey! I've ran into an interesting edge case it seems. Async Handlers are supported with HtmlRewriter, however if you try to use the cache api inside of an async handler while caching the response itself, it just loads forever/seems to deadlock itself. No errors in console. This is reproducible both in local dev and deployed on Cloudflare Workers.

Min. Reproducible Example:

export default {
    async fetch(request, env, ctx) {
        let myResponse = new Response("<!DOCTYPE html><title>x</title><h1>test</h1>", {
            headers: {
                "content-type": "text/html",
            }
        })
        myResponse = new HTMLRewriter()
            .on('h1', {
                async element(e) {
                    await caches.default.put("https://example.com/dummy", new Response("hey!")) // I know this wouldn't get cached, just min to reproduce issue
                    e.setInnerContent('Rewritten');
            
                }
            })
            .transform(myResponse);
        await caches.default.put("https://example.com/raw", myResponse.clone());
        return myResponse;
    },
};

(of course in a larger example I was fetching, changing response headers and caching/using body/using ctx.waitUntil)
The .clone itself doesn't block, I tried doing it outside of it. If I force the rewriter to run and pull the response into memory that works around it:

 let body = await myResponse.arrayBuffer()
 let cacheResponse = new Response(body, myResponse);
 cacheResponse.headers.append("Cache-Control", "s-maxage=120");
 await caches.default.put("https://example.com/raw", cacheResponse);
 return new Response(body, myResponse);
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

No branches or pull requests

1 participant