vinext 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/build/static-export.d.ts +78 -0
- package/dist/build/static-export.d.ts.map +1 -0
- package/dist/build/static-export.js +553 -0
- package/dist/build/static-export.js.map +1 -0
- package/dist/check.d.ts +52 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +483 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +565 -0
- package/dist/cli.js.map +1 -0
- package/dist/client/entry.d.ts +2 -0
- package/dist/client/entry.d.ts.map +1 -0
- package/dist/client/entry.js +85 -0
- package/dist/client/entry.js.map +1 -0
- package/dist/cloudflare/index.d.ts +8 -0
- package/dist/cloudflare/index.d.ts.map +1 -0
- package/dist/cloudflare/index.js +8 -0
- package/dist/cloudflare/index.js.map +1 -0
- package/dist/cloudflare/kv-cache-handler.d.ts +68 -0
- package/dist/cloudflare/kv-cache-handler.d.ts.map +1 -0
- package/dist/cloudflare/kv-cache-handler.js +304 -0
- package/dist/cloudflare/kv-cache-handler.js.map +1 -0
- package/dist/cloudflare/tpr.d.ts +78 -0
- package/dist/cloudflare/tpr.d.ts.map +1 -0
- package/dist/cloudflare/tpr.js +672 -0
- package/dist/cloudflare/tpr.js.map +1 -0
- package/dist/config/config-matchers.d.ts +106 -0
- package/dist/config/config-matchers.d.ts.map +1 -0
- package/dist/config/config-matchers.js +499 -0
- package/dist/config/config-matchers.js.map +1 -0
- package/dist/config/next-config.d.ts +153 -0
- package/dist/config/next-config.d.ts.map +1 -0
- package/dist/config/next-config.js +274 -0
- package/dist/config/next-config.js.map +1 -0
- package/dist/deploy.d.ts +87 -0
- package/dist/deploy.d.ts.map +1 -0
- package/dist/deploy.js +644 -0
- package/dist/deploy.js.map +1 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3287 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +55 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +201 -0
- package/dist/init.js.map +1 -0
- package/dist/routing/app-router.d.ts +96 -0
- package/dist/routing/app-router.d.ts.map +1 -0
- package/dist/routing/app-router.js +815 -0
- package/dist/routing/app-router.js.map +1 -0
- package/dist/routing/pages-router.d.ts +52 -0
- package/dist/routing/pages-router.d.ts.map +1 -0
- package/dist/routing/pages-router.js +239 -0
- package/dist/routing/pages-router.js.map +1 -0
- package/dist/server/api-handler.d.ts +18 -0
- package/dist/server/api-handler.d.ts.map +1 -0
- package/dist/server/api-handler.js +169 -0
- package/dist/server/api-handler.js.map +1 -0
- package/dist/server/app-dev-server.d.ts +42 -0
- package/dist/server/app-dev-server.d.ts.map +1 -0
- package/dist/server/app-dev-server.js +2718 -0
- package/dist/server/app-dev-server.js.map +1 -0
- package/dist/server/app-router-entry.d.ts +18 -0
- package/dist/server/app-router-entry.d.ts.map +1 -0
- package/dist/server/app-router-entry.js +34 -0
- package/dist/server/app-router-entry.js.map +1 -0
- package/dist/server/dev-server.d.ts +40 -0
- package/dist/server/dev-server.d.ts.map +1 -0
- package/dist/server/dev-server.js +758 -0
- package/dist/server/dev-server.js.map +1 -0
- package/dist/server/html.d.ts +22 -0
- package/dist/server/html.d.ts.map +1 -0
- package/dist/server/html.js +29 -0
- package/dist/server/html.js.map +1 -0
- package/dist/server/image-optimization.d.ts +56 -0
- package/dist/server/image-optimization.d.ts.map +1 -0
- package/dist/server/image-optimization.js +103 -0
- package/dist/server/image-optimization.js.map +1 -0
- package/dist/server/instrumentation.d.ts +68 -0
- package/dist/server/instrumentation.d.ts.map +1 -0
- package/dist/server/instrumentation.js +90 -0
- package/dist/server/instrumentation.js.map +1 -0
- package/dist/server/isr-cache.d.ts +61 -0
- package/dist/server/isr-cache.d.ts.map +1 -0
- package/dist/server/isr-cache.js +134 -0
- package/dist/server/isr-cache.js.map +1 -0
- package/dist/server/metadata-routes.d.ts +103 -0
- package/dist/server/metadata-routes.d.ts.map +1 -0
- package/dist/server/metadata-routes.js +270 -0
- package/dist/server/metadata-routes.js.map +1 -0
- package/dist/server/middleware.d.ts +77 -0
- package/dist/server/middleware.d.ts.map +1 -0
- package/dist/server/middleware.js +228 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/prod-server.d.ts +78 -0
- package/dist/server/prod-server.d.ts.map +1 -0
- package/dist/server/prod-server.js +712 -0
- package/dist/server/prod-server.js.map +1 -0
- package/dist/shims/amp.d.ts +17 -0
- package/dist/shims/amp.d.ts.map +1 -0
- package/dist/shims/amp.js +21 -0
- package/dist/shims/amp.js.map +1 -0
- package/dist/shims/app.d.ts +12 -0
- package/dist/shims/app.d.ts.map +1 -0
- package/dist/shims/app.js +2 -0
- package/dist/shims/app.js.map +1 -0
- package/dist/shims/cache-runtime.d.ts +68 -0
- package/dist/shims/cache-runtime.d.ts.map +1 -0
- package/dist/shims/cache-runtime.js +437 -0
- package/dist/shims/cache-runtime.js.map +1 -0
- package/dist/shims/cache.d.ts +243 -0
- package/dist/shims/cache.d.ts.map +1 -0
- package/dist/shims/cache.js +415 -0
- package/dist/shims/cache.js.map +1 -0
- package/dist/shims/client-only.d.ts +18 -0
- package/dist/shims/client-only.d.ts.map +1 -0
- package/dist/shims/client-only.js +18 -0
- package/dist/shims/client-only.js.map +1 -0
- package/dist/shims/config.d.ts +27 -0
- package/dist/shims/config.d.ts.map +1 -0
- package/dist/shims/config.js +30 -0
- package/dist/shims/config.js.map +1 -0
- package/dist/shims/constants.d.ts +13 -0
- package/dist/shims/constants.d.ts.map +1 -0
- package/dist/shims/constants.js +13 -0
- package/dist/shims/constants.js.map +1 -0
- package/dist/shims/document.d.ts +33 -0
- package/dist/shims/document.d.ts.map +1 -0
- package/dist/shims/document.js +32 -0
- package/dist/shims/document.js.map +1 -0
- package/dist/shims/dynamic.d.ts +33 -0
- package/dist/shims/dynamic.d.ts.map +1 -0
- package/dist/shims/dynamic.js +148 -0
- package/dist/shims/dynamic.js.map +1 -0
- package/dist/shims/error-boundary.d.ts +33 -0
- package/dist/shims/error-boundary.d.ts.map +1 -0
- package/dist/shims/error-boundary.js +88 -0
- package/dist/shims/error-boundary.js.map +1 -0
- package/dist/shims/error.d.ts +16 -0
- package/dist/shims/error.d.ts.map +1 -0
- package/dist/shims/error.js +45 -0
- package/dist/shims/error.js.map +1 -0
- package/dist/shims/fetch-cache.d.ts +61 -0
- package/dist/shims/fetch-cache.d.ts.map +1 -0
- package/dist/shims/fetch-cache.js +307 -0
- package/dist/shims/fetch-cache.js.map +1 -0
- package/dist/shims/font-google.d.ts +122 -0
- package/dist/shims/font-google.d.ts.map +1 -0
- package/dist/shims/font-google.js +387 -0
- package/dist/shims/font-google.js.map +1 -0
- package/dist/shims/font-local.d.ts +61 -0
- package/dist/shims/font-local.d.ts.map +1 -0
- package/dist/shims/font-local.js +303 -0
- package/dist/shims/font-local.js.map +1 -0
- package/dist/shims/form.d.ts +30 -0
- package/dist/shims/form.d.ts.map +1 -0
- package/dist/shims/form.js +78 -0
- package/dist/shims/form.js.map +1 -0
- package/dist/shims/head-state.d.ts +11 -0
- package/dist/shims/head-state.d.ts.map +1 -0
- package/dist/shims/head-state.js +47 -0
- package/dist/shims/head-state.js.map +1 -0
- package/dist/shims/head.d.ts +28 -0
- package/dist/shims/head.d.ts.map +1 -0
- package/dist/shims/head.js +148 -0
- package/dist/shims/head.js.map +1 -0
- package/dist/shims/headers.d.ts +150 -0
- package/dist/shims/headers.d.ts.map +1 -0
- package/dist/shims/headers.js +412 -0
- package/dist/shims/headers.js.map +1 -0
- package/dist/shims/image-config.d.ts +30 -0
- package/dist/shims/image-config.d.ts.map +1 -0
- package/dist/shims/image-config.js +91 -0
- package/dist/shims/image-config.js.map +1 -0
- package/dist/shims/image.d.ts +63 -0
- package/dist/shims/image.d.ts.map +1 -0
- package/dist/shims/image.js +284 -0
- package/dist/shims/image.js.map +1 -0
- package/dist/shims/internal/api-utils.d.ts +12 -0
- package/dist/shims/internal/api-utils.d.ts.map +1 -0
- package/dist/shims/internal/api-utils.js +7 -0
- package/dist/shims/internal/api-utils.js.map +1 -0
- package/dist/shims/internal/app-router-context.d.ts +21 -0
- package/dist/shims/internal/app-router-context.d.ts.map +1 -0
- package/dist/shims/internal/app-router-context.js +15 -0
- package/dist/shims/internal/app-router-context.js.map +1 -0
- package/dist/shims/internal/cookies.d.ts +9 -0
- package/dist/shims/internal/cookies.d.ts.map +1 -0
- package/dist/shims/internal/cookies.js +9 -0
- package/dist/shims/internal/cookies.js.map +1 -0
- package/dist/shims/internal/router-context.d.ts +2 -0
- package/dist/shims/internal/router-context.d.ts.map +1 -0
- package/dist/shims/internal/router-context.js +9 -0
- package/dist/shims/internal/router-context.js.map +1 -0
- package/dist/shims/internal/utils.d.ts +48 -0
- package/dist/shims/internal/utils.d.ts.map +1 -0
- package/dist/shims/internal/utils.js +35 -0
- package/dist/shims/internal/utils.js.map +1 -0
- package/dist/shims/internal/work-unit-async-storage.d.ts +12 -0
- package/dist/shims/internal/work-unit-async-storage.d.ts.map +1 -0
- package/dist/shims/internal/work-unit-async-storage.js +13 -0
- package/dist/shims/internal/work-unit-async-storage.js.map +1 -0
- package/dist/shims/layout-segment-context.d.ts +21 -0
- package/dist/shims/layout-segment-context.d.ts.map +1 -0
- package/dist/shims/layout-segment-context.js +27 -0
- package/dist/shims/layout-segment-context.js.map +1 -0
- package/dist/shims/legacy-image.d.ts +52 -0
- package/dist/shims/legacy-image.d.ts.map +1 -0
- package/dist/shims/legacy-image.js +46 -0
- package/dist/shims/legacy-image.js.map +1 -0
- package/dist/shims/link.d.ts +48 -0
- package/dist/shims/link.d.ts.map +1 -0
- package/dist/shims/link.js +395 -0
- package/dist/shims/link.js.map +1 -0
- package/dist/shims/metadata.d.ts +184 -0
- package/dist/shims/metadata.d.ts.map +1 -0
- package/dist/shims/metadata.js +472 -0
- package/dist/shims/metadata.js.map +1 -0
- package/dist/shims/navigation-state.d.ts +14 -0
- package/dist/shims/navigation-state.d.ts.map +1 -0
- package/dist/shims/navigation-state.js +77 -0
- package/dist/shims/navigation-state.js.map +1 -0
- package/dist/shims/navigation.d.ts +201 -0
- package/dist/shims/navigation.d.ts.map +1 -0
- package/dist/shims/navigation.js +672 -0
- package/dist/shims/navigation.js.map +1 -0
- package/dist/shims/og.d.ts +20 -0
- package/dist/shims/og.d.ts.map +1 -0
- package/dist/shims/og.js +19 -0
- package/dist/shims/og.js.map +1 -0
- package/dist/shims/router-state.d.ts +11 -0
- package/dist/shims/router-state.d.ts.map +1 -0
- package/dist/shims/router-state.js +56 -0
- package/dist/shims/router-state.js.map +1 -0
- package/dist/shims/router.d.ts +103 -0
- package/dist/shims/router.d.ts.map +1 -0
- package/dist/shims/router.js +536 -0
- package/dist/shims/router.js.map +1 -0
- package/dist/shims/script.d.ts +58 -0
- package/dist/shims/script.d.ts.map +1 -0
- package/dist/shims/script.js +163 -0
- package/dist/shims/script.js.map +1 -0
- package/dist/shims/server-only.d.ts +19 -0
- package/dist/shims/server-only.d.ts.map +1 -0
- package/dist/shims/server-only.js +19 -0
- package/dist/shims/server-only.js.map +1 -0
- package/dist/shims/server.d.ts +178 -0
- package/dist/shims/server.d.ts.map +1 -0
- package/dist/shims/server.js +377 -0
- package/dist/shims/server.js.map +1 -0
- package/dist/shims/web-vitals.d.ts +24 -0
- package/dist/shims/web-vitals.d.ts.map +1 -0
- package/dist/shims/web-vitals.js +17 -0
- package/dist/shims/web-vitals.js.map +1 -0
- package/dist/utils/hash.d.ts +6 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +20 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/project.d.ts +36 -0
- package/dist/utils/project.d.ts.map +1 -0
- package/dist/utils/project.js +112 -0
- package/dist/utils/project.js.map +1 -0
- package/dist/utils/query.d.ts +10 -0
- package/dist/utils/query.d.ts.map +1 -0
- package/dist/utils/query.js +27 -0
- package/dist/utils/query.js.map +1 -0
- package/package.json +65 -7
- package/index.js +0 -1
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* next/navigation shim
|
|
3
|
+
*
|
|
4
|
+
* App Router navigation hooks. These work on both server (RSC) and client.
|
|
5
|
+
* Server-side: reads from a request context set by the RSC handler.
|
|
6
|
+
* Client-side: reads from browser Location API and provides navigation.
|
|
7
|
+
*/
|
|
8
|
+
// Use namespace import for RSC safety: the react-server condition doesn't export
|
|
9
|
+
// createContext/useContext/useSyncExternalStore as named exports, and strict ESM
|
|
10
|
+
// would throw at link time for missing bindings. With `import * as React`, the
|
|
11
|
+
// bindings are just `undefined` on the namespace object and we can guard at runtime.
|
|
12
|
+
import * as React from "react";
|
|
13
|
+
// ─── Layout segment depth context ─────────────────────────────────────────────
|
|
14
|
+
// Used by useSelectedLayoutSegments() to know which layout it's inside.
|
|
15
|
+
// The context is created lazily because `React.createContext` is NOT available in
|
|
16
|
+
// the react-server condition of React. In the RSC environment, this remains
|
|
17
|
+
// null and the hooks fall back to returning all segments (depth 0).
|
|
18
|
+
// In SSR and browser environments, the context is created and used normally.
|
|
19
|
+
let _LayoutSegmentCtx = null;
|
|
20
|
+
/**
|
|
21
|
+
* Get or create the layout segment context.
|
|
22
|
+
* Returns null in the RSC environment (createContext unavailable).
|
|
23
|
+
*/
|
|
24
|
+
export function getLayoutSegmentContext() {
|
|
25
|
+
if (_LayoutSegmentCtx === null && typeof React.createContext === "function") {
|
|
26
|
+
_LayoutSegmentCtx = React.createContext(0);
|
|
27
|
+
}
|
|
28
|
+
return _LayoutSegmentCtx;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read the layout segment depth from context. Returns 0 if no context
|
|
32
|
+
* is available (RSC environment, outside React tree, or root level).
|
|
33
|
+
*/
|
|
34
|
+
function useLayoutSegmentDepth() {
|
|
35
|
+
const ctx = getLayoutSegmentContext();
|
|
36
|
+
if (!ctx)
|
|
37
|
+
return 0;
|
|
38
|
+
// useContext is safe here because if createContext exists, useContext does too.
|
|
39
|
+
// This branch is only taken in SSR/Browser, never in RSC.
|
|
40
|
+
// Try/catch for unit tests that call this hook outside a React render tree.
|
|
41
|
+
try {
|
|
42
|
+
return React.useContext(ctx);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Server-side navigation state lives in a separate server-only module
|
|
50
|
+
// (navigation-state.ts) that uses AsyncLocalStorage for request isolation.
|
|
51
|
+
// This module is bundled for the browser, so it can't import node:async_hooks.
|
|
52
|
+
//
|
|
53
|
+
// On the server: state functions are set by navigation-state.ts at import time.
|
|
54
|
+
// On the client: _serverContext falls back to null (hooks use window instead).
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
let _serverContext = null;
|
|
57
|
+
let _serverInsertedHTMLCallbacks = [];
|
|
58
|
+
// These are overridden by navigation-state.ts on the server to use ALS.
|
|
59
|
+
let _getServerContext = () => _serverContext;
|
|
60
|
+
let _setServerContext = (ctx) => { _serverContext = ctx; };
|
|
61
|
+
let _getInsertedHTMLCallbacks = () => _serverInsertedHTMLCallbacks;
|
|
62
|
+
let _clearInsertedHTMLCallbacks = () => { _serverInsertedHTMLCallbacks = []; };
|
|
63
|
+
/**
|
|
64
|
+
* Register ALS-backed state accessors. Called by navigation-state.ts on import.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export function _registerStateAccessors(accessors) {
|
|
68
|
+
_getServerContext = accessors.getServerContext;
|
|
69
|
+
_setServerContext = accessors.setServerContext;
|
|
70
|
+
_getInsertedHTMLCallbacks = accessors.getInsertedHTMLCallbacks;
|
|
71
|
+
_clearInsertedHTMLCallbacks = accessors.clearInsertedHTMLCallbacks;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the navigation context for the current SSR/RSC render.
|
|
75
|
+
* Reads from AsyncLocalStorage when available (concurrent-safe),
|
|
76
|
+
* otherwise falls back to module-level state.
|
|
77
|
+
*/
|
|
78
|
+
export function getNavigationContext() {
|
|
79
|
+
return _getServerContext();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Set the navigation context for the current SSR/RSC render.
|
|
83
|
+
* Called by the framework entry before rendering each request.
|
|
84
|
+
*/
|
|
85
|
+
export function setNavigationContext(ctx) {
|
|
86
|
+
_setServerContext(ctx);
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Client-side state
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
const isServer = typeof window === "undefined";
|
|
92
|
+
/** basePath from next.config.js, injected by the plugin at build time */
|
|
93
|
+
const __basePath = process.env.__NEXT_ROUTER_BASEPATH ?? "";
|
|
94
|
+
/** Strip basePath prefix from a browser pathname */
|
|
95
|
+
function stripBasePath(p) {
|
|
96
|
+
if (!__basePath)
|
|
97
|
+
return p;
|
|
98
|
+
if (p.startsWith(__basePath))
|
|
99
|
+
return p.slice(__basePath.length) || "/";
|
|
100
|
+
return p;
|
|
101
|
+
}
|
|
102
|
+
/** Prepend basePath to a path for browser URLs / fetches */
|
|
103
|
+
function withBasePath(p) {
|
|
104
|
+
if (!__basePath)
|
|
105
|
+
return p;
|
|
106
|
+
return __basePath + p;
|
|
107
|
+
}
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// RSC prefetch cache utilities (shared between link.tsx and browser entry)
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
/** Maximum number of entries in the RSC prefetch cache. */
|
|
112
|
+
const MAX_PREFETCH_CACHE_SIZE = 50;
|
|
113
|
+
/** TTL for prefetch cache entries in ms (matches Next.js static prefetch TTL). */
|
|
114
|
+
export const PREFETCH_CACHE_TTL = 30_000;
|
|
115
|
+
/**
|
|
116
|
+
* Convert a pathname (with optional query/hash) to its .rsc URL.
|
|
117
|
+
* Strips trailing slashes before appending `.rsc` so that cache keys
|
|
118
|
+
* are consistent regardless of the `trailingSlash` config setting.
|
|
119
|
+
*/
|
|
120
|
+
export function toRscUrl(href) {
|
|
121
|
+
const [beforeHash] = href.split("#");
|
|
122
|
+
const qIdx = beforeHash.indexOf("?");
|
|
123
|
+
const pathname = qIdx === -1 ? beforeHash : beforeHash.slice(0, qIdx);
|
|
124
|
+
const query = qIdx === -1 ? "" : beforeHash.slice(qIdx);
|
|
125
|
+
// Strip trailing slash (but preserve "/" root) for consistent cache keys
|
|
126
|
+
const normalizedPath = pathname.length > 1 && pathname.endsWith("/")
|
|
127
|
+
? pathname.slice(0, -1)
|
|
128
|
+
: pathname;
|
|
129
|
+
return normalizedPath + ".rsc" + query;
|
|
130
|
+
}
|
|
131
|
+
/** Get or create the shared in-memory RSC prefetch cache on window. */
|
|
132
|
+
export function getPrefetchCache() {
|
|
133
|
+
if (isServer)
|
|
134
|
+
return new Map();
|
|
135
|
+
const win = window;
|
|
136
|
+
if (!win.__VINEXT_RSC_PREFETCH_CACHE__) {
|
|
137
|
+
win.__VINEXT_RSC_PREFETCH_CACHE__ = new Map();
|
|
138
|
+
}
|
|
139
|
+
return win.__VINEXT_RSC_PREFETCH_CACHE__;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get or create the shared set of already-prefetched RSC URLs on window.
|
|
143
|
+
* Keyed by rscUrl so that the browser entry can clear entries when consumed.
|
|
144
|
+
*/
|
|
145
|
+
export function getPrefetchedUrls() {
|
|
146
|
+
if (isServer)
|
|
147
|
+
return new Set();
|
|
148
|
+
const win = window;
|
|
149
|
+
if (!win.__VINEXT_RSC_PREFETCHED_URLS__) {
|
|
150
|
+
win.__VINEXT_RSC_PREFETCHED_URLS__ = new Set();
|
|
151
|
+
}
|
|
152
|
+
return win.__VINEXT_RSC_PREFETCHED_URLS__;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Store a prefetched RSC response in the cache.
|
|
156
|
+
* Enforces a maximum cache size to prevent unbounded memory growth on
|
|
157
|
+
* link-heavy pages.
|
|
158
|
+
*/
|
|
159
|
+
export function storePrefetchResponse(rscUrl, response) {
|
|
160
|
+
const cache = getPrefetchCache();
|
|
161
|
+
// Evict oldest entry if at capacity (Map iterates in insertion order)
|
|
162
|
+
if (cache.size >= MAX_PREFETCH_CACHE_SIZE) {
|
|
163
|
+
const oldest = cache.keys().next().value;
|
|
164
|
+
if (oldest !== undefined)
|
|
165
|
+
cache.delete(oldest);
|
|
166
|
+
}
|
|
167
|
+
cache.set(rscUrl, { response, timestamp: Date.now() });
|
|
168
|
+
}
|
|
169
|
+
const _listeners = new Set();
|
|
170
|
+
function notifyListeners() {
|
|
171
|
+
for (const fn of _listeners)
|
|
172
|
+
fn();
|
|
173
|
+
}
|
|
174
|
+
// Cached URLSearchParams for referential stability (useSyncExternalStore
|
|
175
|
+
// compares snapshots with Object.is — new URLSearchParams instances are
|
|
176
|
+
// never equal, which would cause infinite re-renders).
|
|
177
|
+
let _cachedSearch = !isServer ? window.location.search : "";
|
|
178
|
+
let _cachedSearchParams = new URLSearchParams(_cachedSearch);
|
|
179
|
+
function getSearchParamsSnapshot() {
|
|
180
|
+
const current = window.location.search;
|
|
181
|
+
if (current !== _cachedSearch) {
|
|
182
|
+
_cachedSearch = current;
|
|
183
|
+
_cachedSearchParams = new URLSearchParams(current);
|
|
184
|
+
}
|
|
185
|
+
return _cachedSearchParams;
|
|
186
|
+
}
|
|
187
|
+
// Same for pathname — cache the string for referential stability
|
|
188
|
+
let _cachedPathname = !isServer ? stripBasePath(window.location.pathname) : "/";
|
|
189
|
+
function getPathnameSnapshot() {
|
|
190
|
+
const current = stripBasePath(window.location.pathname);
|
|
191
|
+
if (current !== _cachedPathname) {
|
|
192
|
+
_cachedPathname = current;
|
|
193
|
+
}
|
|
194
|
+
return _cachedPathname;
|
|
195
|
+
}
|
|
196
|
+
// Track client-side params (set during RSC hydration/navigation)
|
|
197
|
+
// We cache the params object for referential stability — only create a new
|
|
198
|
+
// object when the params actually change (shallow key/value comparison).
|
|
199
|
+
let _clientParams = {};
|
|
200
|
+
let _clientParamsJson = "{}";
|
|
201
|
+
export function setClientParams(params) {
|
|
202
|
+
const json = JSON.stringify(params);
|
|
203
|
+
if (json !== _clientParamsJson) {
|
|
204
|
+
_clientParams = params;
|
|
205
|
+
_clientParamsJson = json;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/** Get the current client params (for testing referential stability). */
|
|
209
|
+
export function getClientParams() {
|
|
210
|
+
return _clientParams;
|
|
211
|
+
}
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
// Hooks
|
|
214
|
+
// ---------------------------------------------------------------------------
|
|
215
|
+
/**
|
|
216
|
+
* Returns the current pathname.
|
|
217
|
+
* Server: from request context. Client: from window.location.
|
|
218
|
+
*/
|
|
219
|
+
export function usePathname() {
|
|
220
|
+
if (isServer) {
|
|
221
|
+
// During SSR of "use client" components, the navigation context may not be set.
|
|
222
|
+
// Return a safe fallback — the client will hydrate with the real value.
|
|
223
|
+
return _getServerContext()?.pathname ?? "/";
|
|
224
|
+
}
|
|
225
|
+
// Client-side: use the hook system for reactivity
|
|
226
|
+
return React.useSyncExternalStore((cb) => { _listeners.add(cb); return () => { _listeners.delete(cb); }; }, getPathnameSnapshot, () => _getServerContext()?.pathname ?? "/");
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Returns the current search params as a read-only URLSearchParams.
|
|
230
|
+
*/
|
|
231
|
+
export function useSearchParams() {
|
|
232
|
+
if (isServer) {
|
|
233
|
+
// During SSR of "use client" components, the navigation context may not be set.
|
|
234
|
+
// Return a safe fallback — the client will hydrate with the real value.
|
|
235
|
+
return _getServerContext()?.searchParams ?? new URLSearchParams();
|
|
236
|
+
}
|
|
237
|
+
return React.useSyncExternalStore((cb) => { _listeners.add(cb); return () => { _listeners.delete(cb); }; }, getSearchParamsSnapshot, () => _getServerContext()?.searchParams ?? new URLSearchParams());
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Returns the dynamic params for the current route.
|
|
241
|
+
*/
|
|
242
|
+
export function useParams() {
|
|
243
|
+
if (isServer) {
|
|
244
|
+
// During SSR of "use client" components, the navigation context may not be set.
|
|
245
|
+
return (_getServerContext()?.params ?? {});
|
|
246
|
+
}
|
|
247
|
+
return _clientParams;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Check if a href is an external URL.
|
|
251
|
+
*/
|
|
252
|
+
function isExternalUrl(href) {
|
|
253
|
+
return href.startsWith("http://") || href.startsWith("https://") || href.startsWith("//");
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Check if a href is only a hash change relative to the current URL.
|
|
257
|
+
*/
|
|
258
|
+
function isHashOnlyChange(href) {
|
|
259
|
+
if (typeof window === "undefined")
|
|
260
|
+
return false;
|
|
261
|
+
if (href.startsWith("#"))
|
|
262
|
+
return true;
|
|
263
|
+
try {
|
|
264
|
+
const current = new URL(window.location.href);
|
|
265
|
+
const next = new URL(href, window.location.href);
|
|
266
|
+
return current.pathname === next.pathname && current.search === next.search && next.hash !== "";
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Scroll to a hash target element, or to the top if no hash.
|
|
274
|
+
*/
|
|
275
|
+
function scrollToHash(hash) {
|
|
276
|
+
if (!hash || hash === "#") {
|
|
277
|
+
window.scrollTo(0, 0);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const id = hash.slice(1);
|
|
281
|
+
const element = document.getElementById(id);
|
|
282
|
+
if (element) {
|
|
283
|
+
element.scrollIntoView({ behavior: "auto" });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Reference to the native history.replaceState before patching.
|
|
288
|
+
* Used internally to avoid triggering the interception for internal operations
|
|
289
|
+
* (e.g. saving scroll position shouldn't cause re-renders).
|
|
290
|
+
* Captured before the history method patching at the bottom of this module.
|
|
291
|
+
*/
|
|
292
|
+
const _nativeReplaceState = !isServer
|
|
293
|
+
? window.history.replaceState.bind(window.history)
|
|
294
|
+
: null;
|
|
295
|
+
/**
|
|
296
|
+
* Save the current scroll position into the current history state.
|
|
297
|
+
* Called before every navigation to enable scroll restoration on back/forward.
|
|
298
|
+
*
|
|
299
|
+
* Uses _nativeReplaceState to avoid triggering the history.replaceState
|
|
300
|
+
* interception (which would cause spurious re-renders from notifyListeners).
|
|
301
|
+
*/
|
|
302
|
+
function saveScrollPosition() {
|
|
303
|
+
const state = window.history.state ?? {};
|
|
304
|
+
_nativeReplaceState.call(window.history, { ...state, __vinext_scrollX: window.scrollX, __vinext_scrollY: window.scrollY }, "");
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Restore scroll position from a history state object (used on popstate).
|
|
308
|
+
*
|
|
309
|
+
* When an RSC navigation is in flight (back/forward triggers both this
|
|
310
|
+
* handler and the browser entry's popstate handler which calls
|
|
311
|
+
* __VINEXT_RSC_NAVIGATE__), we must wait for the new content to render
|
|
312
|
+
* before scrolling. Otherwise the user sees old content flash at the
|
|
313
|
+
* restored scroll position.
|
|
314
|
+
*
|
|
315
|
+
* This handler fires before the browser entry's popstate handler (because
|
|
316
|
+
* navigation.ts is loaded before hydration completes), so we defer via a
|
|
317
|
+
* microtask to give the browser entry handler a chance to set
|
|
318
|
+
* __VINEXT_RSC_PENDING__ first.
|
|
319
|
+
*/
|
|
320
|
+
function restoreScrollPosition(state) {
|
|
321
|
+
if (state && typeof state === "object" && "__vinext_scrollY" in state) {
|
|
322
|
+
const { __vinext_scrollX: x, __vinext_scrollY: y } = state;
|
|
323
|
+
// Defer to allow other popstate listeners (browser entry) to run first
|
|
324
|
+
// and set __VINEXT_RSC_PENDING__. Promise.resolve() schedules a microtask
|
|
325
|
+
// that runs after all synchronous event listeners have completed.
|
|
326
|
+
Promise.resolve().then(() => {
|
|
327
|
+
const pending = window.__VINEXT_RSC_PENDING__ ?? null;
|
|
328
|
+
if (pending) {
|
|
329
|
+
// Wait for the RSC navigation to finish rendering, then scroll.
|
|
330
|
+
pending.then(() => {
|
|
331
|
+
requestAnimationFrame(() => {
|
|
332
|
+
window.scrollTo(x, y);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
// No RSC navigation in flight (Pages Router or already settled).
|
|
338
|
+
requestAnimationFrame(() => {
|
|
339
|
+
window.scrollTo(x, y);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Navigate to a URL, handling external URLs, hash-only changes, and RSC navigation.
|
|
347
|
+
*/
|
|
348
|
+
async function navigateImpl(href, mode, scroll) {
|
|
349
|
+
// External URLs: use full page navigation
|
|
350
|
+
if (isExternalUrl(href)) {
|
|
351
|
+
if (mode === "replace") {
|
|
352
|
+
window.location.replace(href);
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
window.location.assign(href);
|
|
356
|
+
}
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const fullHref = withBasePath(href);
|
|
360
|
+
// Save scroll position before navigating (for back/forward restoration)
|
|
361
|
+
if (mode === "push") {
|
|
362
|
+
saveScrollPosition();
|
|
363
|
+
}
|
|
364
|
+
// Hash-only change: update URL and scroll to target, skip RSC fetch
|
|
365
|
+
if (isHashOnlyChange(fullHref)) {
|
|
366
|
+
const hash = fullHref.includes("#") ? fullHref.slice(fullHref.indexOf("#")) : "";
|
|
367
|
+
if (mode === "replace") {
|
|
368
|
+
window.history.replaceState(null, "", fullHref);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
window.history.pushState(null, "", fullHref);
|
|
372
|
+
}
|
|
373
|
+
notifyListeners();
|
|
374
|
+
if (scroll) {
|
|
375
|
+
scrollToHash(hash);
|
|
376
|
+
}
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
// Extract hash for post-navigation scrolling
|
|
380
|
+
const hashIdx = fullHref.indexOf("#");
|
|
381
|
+
const hash = hashIdx !== -1 ? fullHref.slice(hashIdx) : "";
|
|
382
|
+
if (mode === "replace") {
|
|
383
|
+
window.history.replaceState(null, "", fullHref);
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
window.history.pushState(null, "", fullHref);
|
|
387
|
+
}
|
|
388
|
+
notifyListeners();
|
|
389
|
+
// Trigger RSC re-fetch if available, and wait for the new content to render
|
|
390
|
+
// before scrolling. This prevents the old page from visibly jumping to the
|
|
391
|
+
// top before the new content paints.
|
|
392
|
+
if (typeof window.__VINEXT_RSC_NAVIGATE__ === "function") {
|
|
393
|
+
await window.__VINEXT_RSC_NAVIGATE__(fullHref);
|
|
394
|
+
}
|
|
395
|
+
if (scroll) {
|
|
396
|
+
if (hash) {
|
|
397
|
+
scrollToHash(hash);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
window.scrollTo(0, 0);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* App Router's useRouter — returns push/replace/back/forward/refresh.
|
|
406
|
+
* Different from Pages Router's useRouter (next/router).
|
|
407
|
+
*/
|
|
408
|
+
export function useRouter() {
|
|
409
|
+
const router = {
|
|
410
|
+
push(href, options) {
|
|
411
|
+
if (isServer)
|
|
412
|
+
return;
|
|
413
|
+
navigateImpl(href, "push", options?.scroll !== false);
|
|
414
|
+
},
|
|
415
|
+
replace(href, options) {
|
|
416
|
+
if (isServer)
|
|
417
|
+
return;
|
|
418
|
+
navigateImpl(href, "replace", options?.scroll !== false);
|
|
419
|
+
},
|
|
420
|
+
back() {
|
|
421
|
+
if (isServer)
|
|
422
|
+
return;
|
|
423
|
+
window.history.back();
|
|
424
|
+
},
|
|
425
|
+
forward() {
|
|
426
|
+
if (isServer)
|
|
427
|
+
return;
|
|
428
|
+
window.history.forward();
|
|
429
|
+
},
|
|
430
|
+
refresh() {
|
|
431
|
+
if (isServer)
|
|
432
|
+
return;
|
|
433
|
+
// Re-fetch the current page's RSC stream
|
|
434
|
+
if (typeof window.__VINEXT_RSC_NAVIGATE__ === "function") {
|
|
435
|
+
window.__VINEXT_RSC_NAVIGATE__(window.location.href);
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
prefetch(href) {
|
|
439
|
+
if (isServer)
|
|
440
|
+
return;
|
|
441
|
+
// Prefetch the RSC payload for the target route and store in cache
|
|
442
|
+
const fullHref = withBasePath(href);
|
|
443
|
+
const rscUrl = toRscUrl(fullHref);
|
|
444
|
+
const prefetched = getPrefetchedUrls();
|
|
445
|
+
if (prefetched.has(rscUrl))
|
|
446
|
+
return;
|
|
447
|
+
prefetched.add(rscUrl);
|
|
448
|
+
fetch(rscUrl, {
|
|
449
|
+
headers: { Accept: "text/x-component" },
|
|
450
|
+
priority: "low",
|
|
451
|
+
}).then((response) => {
|
|
452
|
+
if (response.ok) {
|
|
453
|
+
storePrefetchResponse(rscUrl, response);
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
// Non-ok response: allow retry on next prefetch() call
|
|
457
|
+
prefetched.delete(rscUrl);
|
|
458
|
+
}
|
|
459
|
+
}).catch(() => {
|
|
460
|
+
// Network error: allow retry on next prefetch() call
|
|
461
|
+
prefetched.delete(rscUrl);
|
|
462
|
+
});
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
return router;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Returns the active child segment one level below the layout where it's called.
|
|
469
|
+
*
|
|
470
|
+
* In Next.js, this is layout-aware: it returns the segment relative to the
|
|
471
|
+
* nearest parent layout. In our implementation, we approximate by returning
|
|
472
|
+
* the first segment after a specified parallel route key, or the first segment
|
|
473
|
+
* of the pathname. Returns null if at the leaf (no child segments).
|
|
474
|
+
*
|
|
475
|
+
* @param parallelRoutesKey - Which parallel route to read (default: "children")
|
|
476
|
+
*/
|
|
477
|
+
export function useSelectedLayoutSegment(parallelRoutesKey) {
|
|
478
|
+
const segments = useSelectedLayoutSegments(parallelRoutesKey);
|
|
479
|
+
return segments.length > 0 ? segments[0] : null;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Returns all active segments below the layout where it's called.
|
|
483
|
+
*
|
|
484
|
+
* In Next.js, this returns the full array of segments from the current
|
|
485
|
+
* layout down to the leaf page. Each layout in the tree wraps its children
|
|
486
|
+
* with a LayoutSegmentProvider that records the URL segment depth at that
|
|
487
|
+
* level. This hook reads that depth from context and slices the pathname
|
|
488
|
+
* segments accordingly.
|
|
489
|
+
*
|
|
490
|
+
* @param parallelRoutesKey - Which parallel route to read (default: "children")
|
|
491
|
+
*/
|
|
492
|
+
export function useSelectedLayoutSegments(_parallelRoutesKey) {
|
|
493
|
+
const pathname = usePathname();
|
|
494
|
+
const depth = useLayoutSegmentDepth();
|
|
495
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
496
|
+
return segments.slice(depth);
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* useServerInsertedHTML — inject HTML during SSR from client components.
|
|
500
|
+
*
|
|
501
|
+
* Used by CSS-in-JS libraries (styled-components, emotion, StyleX) to inject
|
|
502
|
+
* <style> tags during SSR so styles appear in the initial HTML (no FOUC).
|
|
503
|
+
*
|
|
504
|
+
* The callback is called once after each SSR render pass. The returned JSX/HTML
|
|
505
|
+
* is serialized and injected into the HTML stream.
|
|
506
|
+
*
|
|
507
|
+
* Usage (in a "use client" component wrapping children):
|
|
508
|
+
* useServerInsertedHTML(() => {
|
|
509
|
+
* const styles = sheet.getStyleElement();
|
|
510
|
+
* sheet.instance.clearTag();
|
|
511
|
+
* return <>{styles}</>;
|
|
512
|
+
* });
|
|
513
|
+
*/
|
|
514
|
+
export function useServerInsertedHTML(callback) {
|
|
515
|
+
if (typeof document !== "undefined") {
|
|
516
|
+
// Client-side: no-op (styles are already in the DOM)
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
_getInsertedHTMLCallbacks().push(callback);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Flush all collected useServerInsertedHTML callbacks.
|
|
523
|
+
* Returns an array of results (React elements or strings).
|
|
524
|
+
* Clears the callback list so the next render starts fresh.
|
|
525
|
+
*
|
|
526
|
+
* Called by the SSR entry after renderToReadableStream completes.
|
|
527
|
+
*/
|
|
528
|
+
export function flushServerInsertedHTML() {
|
|
529
|
+
const callbacks = _getInsertedHTMLCallbacks();
|
|
530
|
+
const results = [];
|
|
531
|
+
for (const cb of callbacks) {
|
|
532
|
+
try {
|
|
533
|
+
const result = cb();
|
|
534
|
+
if (result != null)
|
|
535
|
+
results.push(result);
|
|
536
|
+
}
|
|
537
|
+
catch {
|
|
538
|
+
// Ignore errors from individual callbacks
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
callbacks.length = 0;
|
|
542
|
+
return results;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Clear all collected useServerInsertedHTML callbacks without flushing.
|
|
546
|
+
* Used for cleanup between requests.
|
|
547
|
+
*/
|
|
548
|
+
export function clearServerInsertedHTML() {
|
|
549
|
+
_clearInsertedHTMLCallbacks();
|
|
550
|
+
}
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
// Non-hook utilities (can be called from Server Components)
|
|
553
|
+
// ---------------------------------------------------------------------------
|
|
554
|
+
/**
|
|
555
|
+
* HTTP Access Fallback error code — shared prefix for notFound/forbidden/unauthorized.
|
|
556
|
+
* Matches Next.js 16's unified error handling approach.
|
|
557
|
+
*/
|
|
558
|
+
export const HTTP_ERROR_FALLBACK_ERROR_CODE = "NEXT_HTTP_ERROR_FALLBACK";
|
|
559
|
+
/**
|
|
560
|
+
* Check if an error is an HTTP Access Fallback error (notFound, forbidden, unauthorized).
|
|
561
|
+
*/
|
|
562
|
+
export function isHTTPAccessFallbackError(error) {
|
|
563
|
+
if (error && typeof error === "object" && "digest" in error) {
|
|
564
|
+
const digest = String(error.digest);
|
|
565
|
+
return (digest === "NEXT_NOT_FOUND" || // legacy compat
|
|
566
|
+
digest.startsWith(`${HTTP_ERROR_FALLBACK_ERROR_CODE};`));
|
|
567
|
+
}
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Extract the HTTP status code from an HTTP Access Fallback error.
|
|
572
|
+
* Returns 404 for legacy NEXT_NOT_FOUND errors.
|
|
573
|
+
*/
|
|
574
|
+
export function getAccessFallbackHTTPStatus(error) {
|
|
575
|
+
if (error && typeof error === "object" && "digest" in error) {
|
|
576
|
+
const digest = String(error.digest);
|
|
577
|
+
if (digest === "NEXT_NOT_FOUND")
|
|
578
|
+
return 404;
|
|
579
|
+
if (digest.startsWith(`${HTTP_ERROR_FALLBACK_ERROR_CODE};`)) {
|
|
580
|
+
return parseInt(digest.split(";")[1], 10);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
return 404;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Enum matching Next.js RedirectType for type-safe redirect calls.
|
|
587
|
+
*/
|
|
588
|
+
export var RedirectType;
|
|
589
|
+
(function (RedirectType) {
|
|
590
|
+
RedirectType["push"] = "push";
|
|
591
|
+
RedirectType["replace"] = "replace";
|
|
592
|
+
})(RedirectType || (RedirectType = {}));
|
|
593
|
+
/**
|
|
594
|
+
* Throw a redirect. Caught by the framework to send a redirect response.
|
|
595
|
+
*/
|
|
596
|
+
export function redirect(url, type) {
|
|
597
|
+
const error = new Error(`NEXT_REDIRECT:${url}`);
|
|
598
|
+
error.digest = `NEXT_REDIRECT;${type ?? "replace"};${url}`;
|
|
599
|
+
throw error;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Trigger a permanent redirect (308).
|
|
603
|
+
*/
|
|
604
|
+
export function permanentRedirect(url) {
|
|
605
|
+
const error = new Error(`NEXT_REDIRECT:${url}`);
|
|
606
|
+
error.digest = `NEXT_REDIRECT;replace;${url};308`;
|
|
607
|
+
throw error;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Trigger a not-found response (404). Caught by the framework.
|
|
611
|
+
*/
|
|
612
|
+
export function notFound() {
|
|
613
|
+
const error = new Error("NEXT_NOT_FOUND");
|
|
614
|
+
error.digest = `${HTTP_ERROR_FALLBACK_ERROR_CODE};404`;
|
|
615
|
+
throw error;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Trigger a forbidden response (403). Caught by the framework.
|
|
619
|
+
* In Next.js, this is gated behind experimental.authInterrupts — we
|
|
620
|
+
* support it unconditionally for maximum compatibility.
|
|
621
|
+
*/
|
|
622
|
+
export function forbidden() {
|
|
623
|
+
const error = new Error("NEXT_FORBIDDEN");
|
|
624
|
+
error.digest = `${HTTP_ERROR_FALLBACK_ERROR_CODE};403`;
|
|
625
|
+
throw error;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Trigger an unauthorized response (401). Caught by the framework.
|
|
629
|
+
* In Next.js, this is gated behind experimental.authInterrupts — we
|
|
630
|
+
* support it unconditionally for maximum compatibility.
|
|
631
|
+
*/
|
|
632
|
+
export function unauthorized() {
|
|
633
|
+
const error = new Error("NEXT_UNAUTHORIZED");
|
|
634
|
+
error.digest = `${HTTP_ERROR_FALLBACK_ERROR_CODE};401`;
|
|
635
|
+
throw error;
|
|
636
|
+
}
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
638
|
+
// Helpers
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
// React hooks are imported at the top level via ESM.
|
|
641
|
+
// Listen for popstate on the client
|
|
642
|
+
if (!isServer) {
|
|
643
|
+
window.addEventListener("popstate", (event) => {
|
|
644
|
+
notifyListeners();
|
|
645
|
+
// Restore scroll position for back/forward navigation
|
|
646
|
+
restoreScrollPosition(event.state);
|
|
647
|
+
});
|
|
648
|
+
// ---------------------------------------------------------------------------
|
|
649
|
+
// history.pushState / replaceState interception (shallow routing)
|
|
650
|
+
//
|
|
651
|
+
// Next.js intercepts these native methods so that when user code calls
|
|
652
|
+
// `window.history.pushState(null, '', '/new-path?filter=abc')` directly,
|
|
653
|
+
// React hooks like usePathname() and useSearchParams() re-render with
|
|
654
|
+
// the new URL. This is the foundation for shallow routing patterns
|
|
655
|
+
// (filter UIs, tabs, URL search param state, etc.).
|
|
656
|
+
//
|
|
657
|
+
// We wrap the original methods, call through to the native implementation,
|
|
658
|
+
// then notify our listener system so useSyncExternalStore picks up the
|
|
659
|
+
// URL change.
|
|
660
|
+
// ---------------------------------------------------------------------------
|
|
661
|
+
const originalPushState = window.history.pushState.bind(window.history);
|
|
662
|
+
const originalReplaceState = window.history.replaceState.bind(window.history);
|
|
663
|
+
window.history.pushState = function patchedPushState(data, unused, url) {
|
|
664
|
+
originalPushState(data, unused, url);
|
|
665
|
+
notifyListeners();
|
|
666
|
+
};
|
|
667
|
+
window.history.replaceState = function patchedReplaceState(data, unused, url) {
|
|
668
|
+
originalReplaceState(data, unused, url);
|
|
669
|
+
notifyListeners();
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
//# sourceMappingURL=navigation.js.map
|