fix: look up redirects against the API instead of fetching our own route - #4
Open
adrians5j wants to merge 1 commit into
Open
fix: look up redirects against the API instead of fetching our own route#4adrians5j wants to merge 1 commit into
adrians5j wants to merge 1 commit into
Conversation
Middleware fetched its own /api/redirects route, which meant a second
request to this deployment on every page view — and it failed in two
environments, silently.
Behind a local HTTPS proxy the Edge runtime doesn't trust the proxy's
certificate, so the request threw SELF_SIGNED_CERT_IN_CHAIN. On a
deployment protected by Vercel Authentication the self-request carries no
credentials and is answered with a 401 or an auth redirect rather than by
our route. In both cases the surrounding `catch {}` discarded the error,
so a broken lookup was indistinguishable from "no redirect is configured"
and every redirect quietly stopped working.
Query the Website Builder API directly instead. It's the only host we
need, it has a real certificate and its own authentication, and it drops
a hop: middleware -> API rather than middleware -> route -> API. Also
removes a function invocation per request.
The response shape is declared locally rather than imported, so
middleware stays free of runtime dependencies.
Failures are now logged. A failed lookup still doesn't take the page
down, but it's no longer invisible.
Verified: /a redirects to /b over both plain http://localhost:3000 and
https://website-builder-nextjs.localhost (the case that was broken), and
a path with no redirect passes through untouched.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
| } catch (err) { | ||
| // A failed lookup must not take the page down, but it must not be silent either: swallowing it | ||
| // is indistinguishable from "no redirect is configured" and hides real API failures. | ||
| console.error(`[middleware] Redirect lookup failed for "${pathname}":`, err); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
middleware.tslooked up redirects by fetching its own/api/redirectsroute:That fails silently in two environments:
SELF_SIGNED_CERT_IN_CHAIN.NODE_EXTRA_CA_CERTSdoes not help — middleware runs on the Edge runtime, which doesn't use Node's TLS root store.In both cases the surrounding
catch {}discarded the error, so a completely broken lookup was indistinguishable from "no redirect is configured". Every redirect just quietly stopped working, with nothing in the logs.Even where it worked, it cost a second function invocation and a full network round trip on every page request, for a chain of middleware → route handler → API.
Fix
Query the Website Builder API directly. It's the only host middleware needs to reach, it has a real certificate and its own authentication, and it removes a hop.
Also:
response.ok. Previously a non-200 was parsed as JSON regardless, so an error body flowed on as if it were redirect data.@webiny/website-builder-sdk, wherePublicRedirectlives, isn't a direct dependency of this project anyway.Verified
/a→307 /bover plainhttp://localhost:3000/a→307 /boverhttps://website-builder-nextjs.localhost— the case that was brokenLocationheadertsc --noEmitcleanNot verified: behaviour on a protected Vercel preview deployment. The reasoning is that we no longer make a request that protection can intercept, but that deserves confirmation on a real preview.
Note
/api/redirectsis now unused by middleware. It still works as a public endpoint; if nothing else calls it, it can be removed in a follow-up.🤖 Generated with Claude Code