Skip to content

defer

defer(fn) registers a cleanup function to run immediately after the top-level hook exits. Essential for preventing zombie processes and memory leaks.

defer(fn)
sync
Param Type Description
fn function Cleanup function to call after the hook exits

None.

function override_fetch(request, ctx)
local browser = cdp.launch({ headless = true })
defer(function() browser:close() end) -- Always closes
local page = browser:attach()
page:open(request.url)
return { status = 200, body = page:content() }
end