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

waitUntil not working in cloudflare pages preset #2672

Open
retuerto opened this issue Aug 18, 2024 · 0 comments
Open

waitUntil not working in cloudflare pages preset #2672

retuerto opened this issue Aug 18, 2024 · 0 comments

Comments

@retuerto
Copy link

retuerto commented Aug 18, 2024

Environment

Nuxt 3.12.4 with Nitro 2.9.7 with cloudflare pages preset through nuxthub

Reproduction

https://stackblitz.com/edit/github-ch89s7?file=routes%2Findex.ts,plugins%2Fbackground.ts

Describe the bug

I just found out that it works in development environment but it doesn't work as expected when deployed on cloudflare throught nuxthub. event.waitUntil gets blocked and the user waits for the response when it shouldn't happen like that. I tried this and it works:

const waitFor = (ms: number) =>
  new Promise((resolve) => setTimeout(resolve, ms))

async function backgroundTask(id: number) {
  await waitFor(5000)
  console.log(`Task ${id} is done!`)
}

export default defineEventHandler((event) => {
  const cfCtx = event.context.cloudflare.context
  const taskId = Math.round(Math.random() * 1000)
  event.context.taskId = taskId
  let now = new Date()
  console.log('Time before backgroundTask : ' + now.toLocaleTimeString())
  cfCtx.waitUntil(backgroundTask(taskId))
  now = new Date()
  return `Task ${taskId} is running in background... : ${now.toLocaleTimeString()}`
})

Additional context

No response

Logs

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant