[GH-ISSUE #239] Wire up RouterContext.Provider in Pages Router so next/compat/router returns a real router #60

Closed
opened 2026-05-06 12:36:54 +02:00 by BreizhHardware · 0 comments

Originally created by @southpolesteve on GitHub (Mar 3, 2026).
Original GitHub issue: https://github.com/cloudflare/vinext/issues/239

Context

PR #224 added a next/compat/router shim that mirrors Next.js's implementation: useContext(RouterContext). This works correctly for App Router apps (returns null, which is the expected behavior for libraries like @clerk/nextjs).

Problem

In real Next.js, the Pages Router wraps the app in a <RouterContext.Provider> with the actual router instance. This means next/compat/router's useRouter() returns the real router in Pages Router pages, and null in App Router pages.

Vinext's next/router shim reads from window.location directly and never renders a RouterContext.Provider. This means next/compat/router returns null always, even in Pages Router apps.

Expected behavior

  • App Router: useRouter() from next/compat/router returns null (correct today)
  • Pages Router: useRouter() from next/compat/router returns the NextRouter instance (broken today, always returns null)

Fix

Render <RouterContext.Provider value={router}> in the Pages Router rendering path (likely in server/dev-server.ts and server/prod-server.ts) so that useContext(RouterContext) returns the actual router when the Pages Router is mounted.

Originally created by @southpolesteve on GitHub (Mar 3, 2026). Original GitHub issue: https://github.com/cloudflare/vinext/issues/239 ## Context PR #224 added a `next/compat/router` shim that mirrors Next.js's implementation: `useContext(RouterContext)`. This works correctly for App Router apps (returns `null`, which is the expected behavior for libraries like `@clerk/nextjs`). ## Problem In real Next.js, the Pages Router wraps the app in a `<RouterContext.Provider>` with the actual router instance. This means `next/compat/router`'s `useRouter()` returns the real router in Pages Router pages, and `null` in App Router pages. Vinext's `next/router` shim reads from `window.location` directly and never renders a `RouterContext.Provider`. This means `next/compat/router` returns `null` always, even in Pages Router apps. ## Expected behavior - App Router: `useRouter()` from `next/compat/router` returns `null` (correct today) - Pages Router: `useRouter()` from `next/compat/router` returns the `NextRouter` instance (broken today, always returns `null`) ## Fix Render `<RouterContext.Provider value={router}>` in the Pages Router rendering path (likely in `server/dev-server.ts` and `server/prod-server.ts`) so that `useContext(RouterContext)` returns the actual router when the Pages Router is mounted.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#60
No description provided.