Course lesson 16 · Smart Routing Service
Geo based smart routing
This lesson turns the data-service lookup into an actual redirect. It validates the relevant Cloudflare metadata, chooses a country-specific destination when configured, and otherwise chooses the default URL. A small routing helper holds the selection logic while the Hono route coordinates lookup, error handling, and the redirect response.
Primary source: course video 16 · Supplied English subtitles · 00:11:50.
Understand the idea
- A missing link record is different from a known link without a country override: the former returns not found, the latter still has a default destination.
- A shared Zod schema narrows country and coordinate metadata and converts coordinate strings to numbers.
- safeParse exposes success or failure as a result that the route handles explicitly.
- Destination selection accepts an optional country: missing country or missing country mapping falls back to the link’s default URL.
- Separating destination selection from the HTTP route keeps the decision easy to inspect and reuse.
What the course does
- Add not-found handling for a link ID with no configuration.
- Define or inspect the shared Cloudflare-info schema and parse the cf metadata with it.
- Add a routing helper that checks the country mapping and falls back to the default destination.
- Replace the temporary JSON result with Hono’s redirect response.
- Set a known default destination in the dashboard and test the redirect locally, then deploy the data service.
- Use the instructor’s VPN experiment: configure Malaysia with an alternate destination and verify the deployed redirect changes when that country is detected.
Watch for
- Although missing country falls back to default, the demonstrated route returns an error when parsing the metadata object fails. The instructor explicitly identifies default fallback as a possible improvement.
- VPN regions may not match the country Cloudflare observes, so the instructor warns that geographic testing can be misleading.
- Added advice: distinguish an unknown link from unavailable location information when deciding user-facing fallback behavior.
Recall before revealing
What destination is chosen for a valid link when its country is absent or has no override?
Reveal the explanation
The routing helper chooses the default destination; a failure to parse the entire metadata object follows a separate error path in the demonstrated route.
Try it
Predict and test three cases: a configured country, an unconfigured country, and a missing link ID.
Check your result
The configured country uses its override, the unconfigured country uses the default, and the missing link receives a not-found response.
Practice prompts and answer checks are added teaching material. Answer from memory first, then compare and explain any difference.
Rewatch only what you need
Open this lesson in your original course library and seek to the times below. Videos are not hosted on this site.
- 00:00:00 — Missing-link handling
- 00:02:00 — Shared metadata schema and coordinate conversion
- 00:04:00 — Metadata parsing failure behavior
- 00:06:00 — Country override and default selection
- 00:10:01 — Malaysia VPN redirect experiment
Companion primary documentation: Hono on Cloudflare Workers. For recorded API names, commands, limits, and prices, check the version you use.