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

Uncaught TypeError: (src_default.middleware ?? []) is not iterable #240

Open
wqcstrong opened this issue Apr 10, 2024 · 4 comments
Open
Assignees
Labels
BUG Something isn't working in progress I'm working on it... allegedly... ONGOING ISSUE This is an ongoing issue that may affect others until resolved.

Comments

@wqcstrong
Copy link

image

@kwhitley
Copy link
Owner

kwhitley commented Apr 10, 2024

What is it?

You’re seeing the wrangler dev issue, described here:
https://itty.dev/itty-router/guides/cloudflare-workers.

What you can do to fix

In your root/index file, destructure your router as part of your export. This strips the Proxy prototype that is intercepting Wrangler/Miniflare's request.

export default { ...router }

What you can do to help

Here's the issue filed with Wrangler team. Please go thumbs up or comment on this thread to spur things along.

What's happening (for the curious)

In local development, Wrangler has introduced a middleware property on the default export. The problem is, if they find it, they assume it's an array (of middleware) and immediately iterate it without any safety checks/validation.

In itty-router, we use a Proxy on the prototype chain to intercept unknown properties of routers and map them to route handlers with the given HTTP method. For instance, router.get maps to GET, router.foo to FOO, etc. So when we export the router directly (perfectly fine in most environments), and Wrangler asks for a middleware property, itty returns a route handler (a function). When you try to iterate a function, you get the error you've found.

@wqcstrong
Copy link
Author

You’re seeing the wrangler dev issue, described here https://itty.dev/itty-router/guides/cloudflare-workers.

Good news is, this works fine in production as-is, or likely more helpful to you, you can destructure your export to solve this in local dev.

export default { ...router }

Thanks, the export default { ...router } is ok , maybe you can keep the issue opened for other user.

@kwhitley
Copy link
Owner

Perfectly happy to, because I certainly doubt you'll be the last to discover it (until the Wrangler team sorts things out on their end).

@kwhitley kwhitley added the ONGOING ISSUE This is an ongoing issue that may affect others until resolved. label Apr 10, 2024
@kwhitley kwhitley self-assigned this Apr 10, 2024
@kwhitley
Copy link
Owner

The PR for this has been merged into main on Wrangler... just need to find the release that includes it:
cloudflare/workers-sdk#5420

@kwhitley kwhitley added BUG Something isn't working in progress I'm working on it... allegedly... labels May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG Something isn't working in progress I'm working on it... allegedly... ONGOING ISSUE This is an ongoing issue that may affect others until resolved.
Projects
None yet
Development

No branches or pull requests

2 participants