Next.js proxy.ts Was Redirecting My Favicon and OG Image to the Login Page
In short
Dynamic metadata routes like /icon, /apple-icon, /opengraph-image and /twitter-image have no file extension, so a Next.js 16 proxy.ts matcher that skips static files by extension sends them into the auth check, and logged-out crawlers get a 307 to the login page instead of an image. The fix is to name those paths in both gates, the matcher lookahead and isPublicRoute(), and to verify with curl -sI that they return 200 and image/png while private pages still return 307.

On this page
- Why does my Next.js OG image redirect to the login page?
- Why don't dynamic metadata routes match the static file exclusions?
- How do I exclude opengraph-image and icon from the proxy matcher?
- What does the negative lookahead in the matcher actually match?
- How do I check that the favicon and OG image actually serve?
- Should Next.js image routes use the edge or Node.js runtime?
- What should you check when adding a new metadata route?
Short answer: in Next.js 16 the auth middleware lives in proxy.ts, and if its matcher skips static files by extension, the dynamic metadata routes fall straight through to your auth check. /icon, /apple-icon, /opengraph-image and /twitter-image have no file extension, so they reach the session lookup, find no user, and get a 307 to the login page. The browser or link-preview crawler asked for an image, received a redirect to an HTML login form, and showed nothing, with no error anywhere. The fix is to name those exact paths in both gates, the public-route check and the matcher, then prove it with curl -sI, which should say 200 and image/png, not 307.
Why does my Next.js OG image redirect to the login page?
Because your proxy thinks it's a page.
This happened on a marketplace app I build and maintain: Next.js 16, Supabase auth, self-hosted on a Node VPS. The proxy has the usual shape. Public paths go through untouched. Everything else gets a supabase.auth.getUser() call, and no user means a 307 to /auth/login.
A request can avoid that auth check in one of two places:
- The
config.matcherregex. If a path is excluded here, the proxy never runs for it. - The
isPublicRoute()function. If the proxy does run, this returns early for public paths before any session lookup.
The matcher excluded static files by extension (svg, png, jpg, ico, css, js and so on) plus a few names: favicon, robots.txt, sitemap.xml. The public list held the marketing pages, /auth/, /api/ and the blog.
The root opengraph-image.tsx was in neither. Nor was the dynamic favicon, an icon.tsx served at /icon. So both took the same path as a private dashboard page: auth check, no session, because crawlers don't log in, then a 307 to the login page.
Nothing errored. Pages rendered, and the og:image meta tag was there, pointing at a URL on my own domain. That URL answered with a redirect instead of a PNG. A browser won't use a login page as a favicon, and a preview crawler won't render one as a share card. Both show nothing, and nothing tells you why.
The image routes under /blog/ never had the problem, because everything under /blog/ was already public. Only the root ones broke.
Why don't dynamic metadata routes match the static file exclusions?
They have no file extension. That's the whole trap.
A static app/icon.svg is served at /icon.svg, and an extension rule catches it. A generated app/icon.tsx is served at /icon. The app icons docs ↗ show the generated output as . The query string doesn't help, because the matcher looks at the pathname, and the pathname is /icon. The opengraph-image and twitter-image docs ↗ work the same way: /opengraph-image, /twitter-image, no extension.
Both pages call these "special Route Handlers". To the proxy they're just routes, no different from /dashboard.
Caching doesn't save you. By default these images are statically optimized, generated at build time and cached. But the proxy docs ↗ list the execution order, and the proxy runs before filesystem routes. A cached PNG still has to get past the proxy first.
The example in the docs doesn't cover it either. The negative matching example excludes favicon.ico, sitemap.xml and robots.txt as "metadata files". Those all have extensions. Copy that pattern, add an extension list as I did, and the generated image routes still walk into your auth check.
If you're moving to Next.js 16 right now, this is the moment to look, because the migration won't. The version 16 upgrade guide ↗ deprecates middleware.ts in favour of proxy.ts, with the exported function renamed from middleware to proxy. The proxy docs give a codemod for the switch:
npx @next/codemod@canary middleware-to-proxy .The docs say it renames the file and the function. Nothing in that changes what your matcher matches, so whatever the old middleware gated by accident, the proxy gates too.
How do I exclude opengraph-image and icon from the proxy matcher?
Name them. Here's the matcher before and after the fix:
// Before: static files excluded by extension only
matcher: [
"/((?!_next/static|_next/image|favicon|robots\\.txt|sitemap\\.xml|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|css|js|xml|txt)$).*)",
],
// After: extensionless image routes by name, plus webmanifest
matcher: [
"/((?!_next/static|_next/image|favicon|opengraph-image|twitter-image|apple-icon|robots\\.txt|sitemap\\.xml|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|css|js|xml|txt|webmanifest)$).*)",
],webmanifest joined the extension list in the same commit, because that commit also added an app/manifest.ts, which Next serves at /manifest.webmanifest.
And the second gate:
function isPublicRoute(pathname: string): boolean {
if (PUBLIC_PATHS.has(pathname)) return true;
if (pathname.startsWith("/auth/")) return true;
if (pathname.startsWith("/api/")) return true;
if (pathname.startsWith("/_next/")) return true;
if (pathname.startsWith("/favicon")) return true;
// Metadata routes are public assets, not app pages.
if (
pathname === "/icon" ||
pathname === "/apple-icon" ||
pathname === "/opengraph-image" ||
pathname === "/twitter-image" ||
pathname === "/manifest.webmanifest"
)
return true;
if (pathname.startsWith("/blog/")) return true;
return false;
}Strictly, either gate alone stops the 307. I use both on purpose. The matcher is a one-line regex people rarely read, and a path it excludes never pays for the proxy at all. The function is where people actually add public routes, so it's the one most likely to stay correct through future changes. If a refactor breaks one, the other still holds.
One exception, to be honest about it: icon isn't in my matcher. For the favicon I went simpler and replaced the dynamic icon.tsx with a static app/icon.svg, which the svg rule already skips. /icon is still in isPublicRoute, so a generated icon that comes back later won't land on the login page.
What does the negative lookahead in the matcher actually match?
Less than it looks. I ran both patterns against a handful of paths in Node, anchored at both ends. "proxy" means the request enters the proxy, "skip" means it doesn't:
path before after
/opengraph-image proxy skip
/apple-icon proxy skip
/icon proxy proxy
/icon.svg skip skip
/blog/some-post/opengraph-image proxy proxy
/opengraph-image-test proxy skip
/favicon-guide skip skipTwo things fall out of that.
The lookahead only checks the start of the path. /blog/some-post/opengraph-image still enters the proxy after the fix. It works because /blog/ is public in isPublicRoute. A share image nested under a protected section is covered by neither gate, and it needs its own line.
It's a prefix match. favicon also skips /favicon-guide, and opengraph-image skips /opengraph-image-test. Harmless here, because no page in this app starts with those words. But it runs the other way too: an excluded name that happens to prefix a private page takes that page out of the proxy. That's one more reason not to treat the proxy as your only auth check, and the proxy docs say as much for Server Functions.
How do I check that the favicon and OG image actually serve?
Ask for them the way a crawler does: no cookies, and look at the status and the content type, not the page.
curl -sI https://example.com/opengraph-image | grep -iE "^HTTP|^content-type|^location"Before the fix, the image route got the same answer every private page on the site still gets. The redirect target is built from the requested path:
HTTP/1.1 307 Temporary Redirect
location: /auth/login?redirect=%2Fopengraph-imageAfter:
HTTP/1.1 200 OK
content-type: image/png-I sends a HEAD request. If you want to see exactly what a GET returns:
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" https://example.com/opengraph-image
# 200 image/pngI keep a small script for after each deploy. It checks that the public assets open and that a private page is still shut, because a fix that opens everything would pass the first half:
#!/usr/bin/env bash
# Usage: check-meta.sh https://your-site
BASE="${1:?usage: check-meta.sh https://your-site}"
fail=0
serves() {
out=$(curl -s -o /dev/null -w "%{http_code} %{content_type}" "$BASE$1")
case "$out" in
"200 $2"*) echo "ok $1 $out" ;;
*) echo "FAIL $1 $out"; fail=1 ;;
esac
}
gated() {
code=$(curl -s -o /dev/null -w "%{http_code}" "$BASE$1")
if [ "$code" = "307" ]; then echo "ok $1 $code (still gated)"
else echo "FAIL $1 $code"; fail=1; fi
}
serves /opengraph-image image/png
serves /apple-icon image/png
serves /icon.svg image/svg+xml
serves /manifest.webmanifest application/manifest+json
gated /dashboard # any page that must stay private
exit $failWhen I ran it against production while writing this, all four assets came back 200 with the right type, and the private page still got its 307.
Should Next.js image routes use the edge or Node.js runtime?
On a self-hosted Node server, I use Node. In the same commit I moved the root opengraph-image.tsx from runtime = "edge" to runtime = "nodejs", and the new apple-icon.tsx started life on Node. The app runs as a Node process on a VPS. There's no edge network behind it, only an emulated edge runtime inside the same server, and that had been a flaky spot for image routes. ImageResponse from next/og runs fine on Node, and the opengraph-image docs include Node.js runtime examples for reading local assets.
import { ImageResponse } from "next/og";
// Self-hosted on Node: match how the server actually runs.
export const runtime = "nodejs";
export const size = { width: 1200, height: 630 };
export const contentType = "image/png";
export const alt = "What the share card shows";To be clear, the runtime didn't cause the 307. The proxy did that on its own. With both fixed, /opengraph-image and /apple-icon render real PNGs.
The proxy itself has no choice here. In Next.js 16 it runs on Node by default, and the docs say setting runtime in a proxy file throws an error. I wrote up the rest of that migration, including what happens to auth code written for the edge, in moving auth from middleware.ts to proxy.ts in Next.js 16 ↗.
What should you check when adding a new metadata route?
My rule now: every new dynamic metadata route goes into both gates, in the same commit that creates it. If it isn't in both, it will 307 to the login page, and nobody will tell you.
The checklist I run when adding one:
Adding opengraph-image, twitter-image, icon or apple-icon (.tsx/.ts/.js):
[ ] Exact path added to isPublicRoute()
[ ] Name added to the matcher lookahead (skip only if its extension is already excluded)
[ ] Nested under a protected section? It needs its own rule
[ ] runtime = "nodejs" when self-hosted on Node
[ ] Logged out: curl returns 200 and the right content-type
[ ] A private page still returns 307What I'd do differently is stop treating an extension list as "all the static files". In the App Router, some of the most public URLs in the app are routes with no extension at all, and they're exactly the ones crawlers fetch without cookies. I'd also have written that curl check the day the first opengraph-image.tsx went in, instead of trusting that a meta tag in the HTML meant an image behind it.
If your share cards or favicon have gone blank behind an auth layer and you can't see why, get in touch ↗.
FAQ
Why does my Next.js opengraph-image return a 307 redirect?
Your proxy or middleware is treating it as a protected page. Generated routes like /opengraph-image have no file extension, so a matcher that skips static files by extension lets them into the auth check, which finds no session and redirects to the login page. Add the path to your public-route check and to the matcher's exclusion list.
Does the Next.js proxy run for favicon and OG image requests?
It runs for every path the matcher doesn't exclude, and that includes generated metadata routes. Static files such as icon.svg or favicon.ico are skipped only if your matcher excludes their extension or name. Dynamic ones like /icon, /apple-icon, /opengraph-image and /twitter-image have to be named explicitly.
Do I need to exclude metadata routes in both the matcher and the auth check?
Either one alone stops the redirect, but I put them in both. The matcher keeps the proxy from running at all for those paths, and the public-route function still covers them if someone later edits the regex. Neither covers an image route nested under a protected section, so that one needs its own rule.
How do I test that my OG image works for social crawlers?
Request it without cookies and check the status and content type: curl -sI on /opengraph-image should return 200 and image/png, not a 307 with a location header pointing at the login page. Run the same check against a private page to confirm it still redirects. Doing both after every deploy catches a fix that opened too much.
Should opengraph-image use the edge runtime when self-hosting Next.js?
On a self-hosted Node server I set runtime to nodejs. There is no edge network behind a single Node process, so the edge runtime is an emulation, and it had been flaky for image routes in my setup. ImageResponse from next/og works on the Node runtime, and the Next.js docs include Node examples for reading local assets.
Working on something like this?
I build web apps, AI features, and mobile products for clients. If this article matches a problem you have, tell me about it.
Start a conversationMalik Hamza Shabbir · Full-Stack & AI Engineer
I build full-stack and AI products solo: a reputation SaaS in production, RAG pipelines, and React Native apps. I write from what I ship, not from documentation summaries.
Run it on yours
Every number above came from code you can read.
The harness is open and the per-probe log ships with it. If you build one of these systems, or you are choosing between them with real money, the same method is available to you.
Send me an endpoint and I will write an adapter, run it on the same workload under the same rules, and publish the result. It goes in the table whether it wins or not.
Sponsored inclusion is disclosed in the article and the result publishes either way. Paying moves you up the queue, never up the table.
Related articles
My Production Env Changes Did Nothing. A Forgotten .env.local Was Winning.
A stale .env.local sat next to .env.production on a self-managed Next.js server, so my env edits did nothing. Weeks later, a committed copy pointed sign-in at a deleted Supabase project for about 19 hours. These are the checks and deploy rules I use now.
Supabase Sign-In Was Down and the Database Looked Fine. The 401 Was Lying.
During a production sign-in outage, GET /rest/v1/ kept answering 401 in about 0.15s while Supabase auth, PostgREST and storage were dead. These are the probes that tell the truth, the Management API calls that fixed it, and a ten-second runbook.
Smartlead Does Not Dedupe Across Campaigns. I Tested It.
I pushed a lead that already sat in one Smartlead campaign into an empty second campaign, with the duplicate flag set to block it. Smartlead accepted it, and the response looked like a block. Here is the test and how I enforce dedup myself.