main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { CacheStates } from "../../../shared/lib/app-router-context.shared-runtime";
import { fillLazyItemsTillLeafWithHead } from "./fill-lazy-items-till-leaf-with-head";
import { fillCacheWithNewSubTreeData } from "./fill-cache-with-new-subtree-data";
export function applyFlightData(existingCache, cache, flightDataPath, wasPrefetched) {
if (wasPrefetched === void 0) wasPrefetched = false;
// The one before last item is the router state tree patch
const [treePatch, subTreeData, head] = flightDataPath.slice(-3);
// Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData === null) {
return false;
}
if (flightDataPath.length === 3) {
cache.status = CacheStates.READY;
cache.subTreeData = subTreeData;
fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head, wasPrefetched);
} else {
// Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY;
cache.subTreeData = existingCache.subTreeData;
cache.parallelRoutes = new Map(existingCache.parallelRoutes);
// Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath, wasPrefetched);
}
return true;
}
//# sourceMappingURL=apply-flight-data.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/apply-flight-data.ts"],"names":["CacheStates","fillLazyItemsTillLeafWithHead","fillCacheWithNewSubTreeData","applyFlightData","existingCache","cache","flightDataPath","wasPrefetched","treePatch","subTreeData","head","slice","length","status","READY","parallelRoutes","Map"],"mappings":"AAAA,SAEEA,WAAW,QACN,wDAAuD;AAE9D,SAASC,6BAA6B,QAAQ,wCAAuC;AACrF,SAASC,2BAA2B,QAAQ,qCAAoC;AAEhF,OAAO,SAASC,gBACdC,aAAwB,EACxBC,KAAgB,EAChBC,cAA8B,EAC9BC,aAA8B;IAA9BA,IAAAA,0BAAAA,gBAAyB;IAEzB,0DAA0D;IAC1D,MAAM,CAACC,WAAWC,aAAaC,KAAK,GAAGJ,eAAeK,KAAK,CAAC,CAAC;IAE7D,8FAA8F;IAC9F,IAAIF,gBAAgB,MAAM;QACxB,OAAO;IACT;IAEA,IAAIH,eAAeM,MAAM,KAAK,GAAG;QAC/BP,MAAMQ,MAAM,GAAGb,YAAYc,KAAK;QAChCT,MAAMI,WAAW,GAAGA;QACpBR,8BACEI,OACAD,eACAI,WACAE,MACAH;IAEJ,OAAO;QACL,mDAAmD;QACnDF,MAAMQ,MAAM,GAAGb,YAAYc,KAAK;QAChCT,MAAMI,WAAW,GAAGL,cAAcK,WAAW;QAC7CJ,MAAMU,cAAc,GAAG,IAAIC,IAAIZ,cAAcW,cAAc;QAC3D,oEAAoE;QACpEb,4BACEG,OACAD,eACAE,gBACAC;IAEJ;IAEA,OAAO;AACT"}

View File

@@ -0,0 +1,83 @@
import { matchSegment } from "../match-segments";
/**
* Deep merge of the two router states. Parallel route keys are preserved if the patch doesn't have them.
*/ function applyPatch(initialTree, patchTree) {
const [initialSegment, initialParallelRoutes] = initialTree;
const [patchSegment, patchParallelRoutes] = patchTree;
// if the applied patch segment is __DEFAULT__ then we can ignore it and return the initial tree
// this is because the __DEFAULT__ segment is used as a placeholder on navigation
if (patchSegment === "__DEFAULT__" && initialSegment !== "__DEFAULT__") {
return initialTree;
}
if (matchSegment(initialSegment, patchSegment)) {
const newParallelRoutes = {};
for(const key in initialParallelRoutes){
const isInPatchTreeParallelRoutes = typeof patchParallelRoutes[key] !== "undefined";
if (isInPatchTreeParallelRoutes) {
newParallelRoutes[key] = applyPatch(initialParallelRoutes[key], patchParallelRoutes[key]);
} else {
newParallelRoutes[key] = initialParallelRoutes[key];
}
}
for(const key in patchParallelRoutes){
if (newParallelRoutes[key]) {
continue;
}
newParallelRoutes[key] = patchParallelRoutes[key];
}
const tree = [
initialSegment,
newParallelRoutes
];
if (initialTree[2]) {
tree[2] = initialTree[2];
}
if (initialTree[3]) {
tree[3] = initialTree[3];
}
if (initialTree[4]) {
tree[4] = initialTree[4];
}
return tree;
}
return patchTree;
}
/**
* Apply the router state from the Flight response. Creates a new router state tree.
*/ export function applyRouterStatePatchToTree(flightSegmentPath, flightRouterState, treePatch) {
const [segment, parallelRoutes, , , isRootLayout] = flightRouterState;
// Root refresh
if (flightSegmentPath.length === 1) {
const tree = applyPatch(flightRouterState, treePatch);
return tree;
}
const [currentSegment, parallelRouteKey] = flightSegmentPath;
// Tree path returned from the server should always match up with the current tree in the browser
if (!matchSegment(currentSegment, segment)) {
return null;
}
const lastSegment = flightSegmentPath.length === 2;
let parallelRoutePatch;
if (lastSegment) {
parallelRoutePatch = applyPatch(parallelRoutes[parallelRouteKey], treePatch);
} else {
parallelRoutePatch = applyRouterStatePatchToTree(flightSegmentPath.slice(2), parallelRoutes[parallelRouteKey], treePatch);
if (parallelRoutePatch === null) {
return null;
}
}
const tree = [
flightSegmentPath[0],
{
...parallelRoutes,
[parallelRouteKey]: parallelRoutePatch
}
];
// Current segment is the root layout
if (isRootLayout) {
tree[4] = true;
}
return tree;
}
//# sourceMappingURL=apply-router-state-patch-to-tree.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/apply-router-state-patch-to-tree.ts"],"names":["matchSegment","applyPatch","initialTree","patchTree","initialSegment","initialParallelRoutes","patchSegment","patchParallelRoutes","newParallelRoutes","key","isInPatchTreeParallelRoutes","tree","applyRouterStatePatchToTree","flightSegmentPath","flightRouterState","treePatch","segment","parallelRoutes","isRootLayout","length","currentSegment","parallelRouteKey","lastSegment","parallelRoutePatch","slice"],"mappings":"AAIA,SAASA,YAAY,QAAQ,oBAAmB;AAEhD;;CAEC,GACD,SAASC,WACPC,WAA8B,EAC9BC,SAA4B;IAE5B,MAAM,CAACC,gBAAgBC,sBAAsB,GAAGH;IAChD,MAAM,CAACI,cAAcC,oBAAoB,GAAGJ;IAE5C,gGAAgG;IAChG,iFAAiF;IACjF,IAAIG,iBAAiB,iBAAiBF,mBAAmB,eAAe;QACtE,OAAOF;IACT;I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AAcT,kBAAkBM,MAAM,KAAK;IAEjD,IAAII;IACJ,IAAID,aAAa;QACfC,qBAAqBtB,WAAWgB,cAAc,CAACI,iBAAiB,EAAEN;IACpE,OAAO;QACLQ,qBAAqBX,4BACnBC,kBAAkBW,KAAK,CAAC,IACxBP,cAAc,CAACI,iBAAiB,EAChCN;QAGF,IAAIQ,uBAAuB,MAAM;YAC/B,OAAO;QACT;IACF;IAEA,MAAMZ,OAA0B;QAC9BE,iBAAiB,CAAC,EAAE;QACpB;YACE,GAAGI,cAAc;YACjB,CAACI,iBAAiB,EAAEE;QACtB;KACD;IAED,qCAAqC;IACrC,IAAIL,cAAc;QAChBP,IAAI,CAAC,EAAE,GAAG;IACZ;IAEA,OAAOA;AACT"}

View File

@@ -0,0 +1,77 @@
import { INTERCEPTION_ROUTE_MARKERS } from "../../../server/future/helpers/interception-routes";
import { isGroupSegment } from "../../../shared/lib/segment";
import { matchSegment } from "../match-segments";
const removeLeadingSlash = (segment)=>{
return segment[0] === "/" ? segment.slice(1) : segment;
};
const segmentToPathname = (segment)=>{
if (typeof segment === "string") {
return segment;
}
return segment[1];
};
function normalizeSegments(segments) {
return segments.reduce((acc, segment)=>{
segment = removeLeadingSlash(segment);
if (segment === "" || isGroupSegment(segment)) {
return acc;
}
return acc + "/" + segment;
}, "") || "/";
}
export function extractPathFromFlightRouterState(flightRouterState) {
const segment = Array.isArray(flightRouterState[0]) ? flightRouterState[0][1] : flightRouterState[0];
if (segment === "__DEFAULT__" || INTERCEPTION_ROUTE_MARKERS.some((m)=>segment.startsWith(m))) return undefined;
if (segment.startsWith("__PAGE__")) return "";
const segments = [
segment
];
var _flightRouterState_;
const parallelRoutes = (_flightRouterState_ = flightRouterState[1]) != null ? _flightRouterState_ : {};
const childrenPath = parallelRoutes.children ? extractPathFromFlightRouterState(parallelRoutes.children) : undefined;
if (childrenPath !== undefined) {
segments.push(childrenPath);
} else {
for (const [key, value] of Object.entries(parallelRoutes)){
if (key === "children") continue;
const childPath = extractPathFromFlightRouterState(value);
if (childPath !== undefined) {
segments.push(childPath);
}
}
}
return normalizeSegments(segments);
}
function computeChangedPathImpl(treeA, treeB) {
const [segmentA, parallelRoutesA] = treeA;
const [segmentB, parallelRoutesB] = treeB;
const normalizedSegmentA = segmentToPathname(segmentA);
const normalizedSegmentB = segmentToPathname(segmentB);
if (INTERCEPTION_ROUTE_MARKERS.some((m)=>normalizedSegmentA.startsWith(m) || normalizedSegmentB.startsWith(m))) {
return "";
}
if (!matchSegment(segmentA, segmentB)) {
var _extractPathFromFlightRouterState;
// once we find where the tree changed, we compute the rest of the path by traversing the tree
return (_extractPathFromFlightRouterState = extractPathFromFlightRouterState(treeB)) != null ? _extractPathFromFlightRouterState : "";
}
for(const parallelRouterKey in parallelRoutesA){
if (parallelRoutesB[parallelRouterKey]) {
const changedPath = computeChangedPathImpl(parallelRoutesA[parallelRouterKey], parallelRoutesB[parallelRouterKey]);
if (changedPath !== null) {
return segmentToPathname(segmentB) + "/" + changedPath;
}
}
}
return null;
}
export function computeChangedPath(treeA, treeB) {
const changedPath = computeChangedPathImpl(treeA, treeB);
if (changedPath == null || changedPath === "/") {
return changedPath;
}
// lightweight normalization to remove route groups
return normalizeSegments(changedPath.split("/"));
}
//# sourceMappingURL=compute-changed-path.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/compute-changed-path.ts"],"names":["INTERCEPTION_ROUTE_MARKERS","isGroupSegment","matchSegment","removeLeadingSlash","segment","slice","segmentToPathname","normalizeSegments","segments","reduce","acc","extractPathFromFlightRouterState","flightRouterState","Array","isArray","some","m","startsWith","undefined","parallelRoutes","childrenPath","children","push","key","value","Object","entries","childPath","computeChangedPathImpl","treeA","treeB","segmentA","parallelRoutesA","segmentB","parallelRoutesB","normalizedSegmentA","normalizedSegmentB","parallelRouterKey","changedPath","computeChangedPath","split"],"mappings":"AACA,SAASA,0BAA0B,QAAQ,qDAAoD;AAC/F,SAASC,cAAc,QAAQ,8BAA6B;A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;IACF;IAEA,OAAOpB,kBAAkBC;AAC3B;AAEA,SAASoB,uBACPC,KAAwB,EACxBC,KAAwB;IAExB,MAAM,CAACC,UAAUC,gBAAgB,GAAGH;IACpC,MAAM,CAACI,UAAUC,gBAAgB,GAAGJ;IAEpC,MAAMK,qBAAqB7B,kBAAkByB;IAC7C,MAAMK,qBAAqB9B,kBAAkB2B;IAE7C,IACEjC,2BAA2Be,IAAI,CAC7B,CAACC,IACCmB,mBAAmBlB,UAAU,CAACD,MAAMoB,mBAAmBnB,UAAU,CAACD,KAEtE;QACA,OAAO;IACT;IAEA,IAAI,CAACd,aAAa6B,UAAUE,WAAW;YAE9BtB;QADP,8FAA8F;QAC9F,OAAOA,CAAAA,oCAAAA,iCAAiCmB,kBAAjCnB,oCAA2C;IACpD;IAEA,IAAK,MAAM0B,qBAAqBL,gBAAiB;QAC/C,IAAIE,eAAe,CAACG,kBAAkB,EAAE;YACtC,MAAMC,cAAcV,uBAClBI,eAAe,CAACK,kBAAkB,EAClCH,eAAe,CAACG,kBAAkB;YAEpC,IAAIC,gBAAgB,MAAM;gBACxB,OAAO,AAAGhC,kBAAkB2B,YAAU,MAAGK;YAC3C;QACF;IACF;IAEA,OAAO;AACT;AAEA,OAAO,SAASC,mBACdV,KAAwB,EACxBC,KAAwB;IAExB,MAAMQ,cAAcV,uBAAuBC,OAAOC;IAElD,IAAIQ,eAAe,QAAQA,gBAAgB,KAAK;QAC9C,OAAOA;IACT;IAEA,mDAAmD;IACnD,OAAO/B,kBAAkB+B,YAAYE,KAAK,CAAC;AAC7C"}

View File

@@ -0,0 +1,6 @@
export function createHrefFromUrl(url, includeHash) {
if (includeHash === void 0) includeHash = true;
return url.pathname + url.search + (includeHash ? url.hash : "");
}
//# sourceMappingURL=create-href-from-url.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/create-href-from-url.ts"],"names":["createHrefFromUrl","url","includeHash","pathname","search","hash"],"mappings":"AAAA,OAAO,SAASA,kBACdC,GAA8C,EAC9CC,WAA2B;IAA3BA,IAAAA,wBAAAA,cAAuB;IAEvB,OAAOD,IAAIE,QAAQ,GAAGF,IAAIG,MAAM,GAAIF,CAAAA,cAAcD,IAAII,IAAI,GAAG,EAAC;AAChE"}

View File

@@ -0,0 +1,42 @@
import { CacheStates } from "../../../shared/lib/app-router-context.shared-runtime";
import { createHrefFromUrl } from "./create-href-from-url";
import { fillLazyItemsTillLeafWithHead } from "./fill-lazy-items-till-leaf-with-head";
import { extractPathFromFlightRouterState } from "./compute-changed-path";
export function createInitialRouterState(param) {
let { buildId, initialTree, children, initialCanonicalUrl, initialParallelRoutes, isServer, location, initialHead } = param;
const cache = {
status: CacheStates.READY,
data: null,
subTreeData: children,
// The cache gets seeded during the first render. `initialParallelRoutes` ensures the cache from the first render is there during the second render.
parallelRoutes: isServer ? new Map() : initialParallelRoutes
};
// When the cache hasn't been seeded yet we fill the cache with the head.
if (initialParallelRoutes === null || initialParallelRoutes.size === 0) {
fillLazyItemsTillLeafWithHead(cache, undefined, initialTree, initialHead);
}
var // the || operator is intentional, the pathname can be an empty string
_ref;
return {
buildId,
tree: initialTree,
cache,
prefetchCache: new Map(),
pushRef: {
pendingPush: false,
mpaNavigation: false
},
focusAndScrollRef: {
apply: false,
onlyHashChange: false,
hashFragment: null,
segmentPaths: []
},
canonicalUrl: // location.href is read as the initial value for canonicalUrl in the browser
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates in the useEffect further down in this file.
location ? createHrefFromUrl(location) : initialCanonicalUrl,
nextUrl: (_ref = extractPathFromFlightRouterState(initialTree) || (location == null ? void 0 : location.pathname)) != null ? _ref : null
};
}
//# sourceMappingURL=create-initial-router-state.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/create-initial-router-state.ts"],"names":["CacheStates","createHrefFromUrl","fillLazyItemsTillLeafWithHead","extractPathFromFlightRouterState","createInitialRouterState","buildId","initialTree","children","initialCanonicalUrl","initialParallelRoutes","isServer","location","initialHead","cache","status","READY","data","subTreeData","parallelRoutes","Map","size","undefined","tree","prefetchCache","pushRef","pendingPush","mpaNavigation","focusAndScrollRef","apply","onlyHashChange","hashFragment","segmentPaths","canonicalUrl","nextUrl","pathname"],"mappings":"AAIA,SAASA,WAAW,QAAQ,wDAAuD;AACnF,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,6BAA6B,QAAQ,wCAAuC;AACrF,SAASC,gCAAgC,QAAQ,yBAAwB;AAazE,OAAO,SAASC,yBAAyB,KASV;IATU,IAAA,EACvCC,OAAO,EACPC,WAAW,EACXC,QAAQ,EACRC,mBAAmB,EACnBC,qBAAqB,EACrBC,QAAQ,EACRC,QAAQ,EACRC,WAAW,EACkB,GATU;IAUvC,MAAMC,QAAmB;QACvBC,QAAQd,YAAYe,KAAK;QACzBC,MAAM;QACNC,aAAaV;QACb,oJAAoJ;QACpJW,gBAAgBR,WAAW,IAAIS,QAAQV;IACzC;IAEA,yEAAyE;IACzE,IAAIA,0BAA0B,QAAQA,sBAAsBW,IAAI,KAAK,GAAG;QACtElB,8BAA8BW,OAAOQ,WAAWf,aAAaM;IAC/D;QAsBI,sEAAsE;IACrET;IArBL,OAAO;QACLE;QACAiB,MAAMhB;QACNO;QACAU,eAAe,IAAIJ;QACnBK,SAAS;YAAEC,aAAa;YAAOC,eAAe;QAAM;QACpDC,mBAAmB;YACjBC,OAAO;YACPC,gBAAgB;YAChBC,cAAc;YACdC,cAAc,EAAE;QAClB;QACAC,cACE,6EAA6E;QAC7E,kJAAkJ;QAClJrB,WAEIV,kBAAkBU,YAClBH;QACNyB,SAEE,CAAC9B,OAAAA,iCAAiCG,iBAAgBK,4BAAAA,SAAUuB,QAAQ,aAAnE/B,OACD;IACJ;AACF"}

View File

@@ -0,0 +1,53 @@
import { matchSegment } from "../match-segments";
/**
* Create optimistic version of router state based on the existing router state and segments.
* This is used to allow rendering layout-routers up till the point where data is missing.
*/ export function createOptimisticTree(segments, flightRouterState, parentRefetch) {
const [existingSegment, existingParallelRoutes, url, refresh, isRootLayout] = flightRouterState || [
null,
{}
];
const segment = segments[0];
const isLastSegment = segments.length === 1;
const segmentMatches = existingSegment !== null && matchSegment(existingSegment, segment);
// if there are multiple parallel routes at this level, we need to refetch here
// to ensure we get the correct tree. This is because we don't know which
// parallel route will match the next segment.
const hasMultipleParallelRoutes = Object.keys(existingParallelRoutes).length > 1;
const shouldRefetchThisLevel = !flightRouterState || !segmentMatches || hasMultipleParallelRoutes;
let parallelRoutes = {};
if (existingSegment !== null && segmentMatches) {
parallelRoutes = existingParallelRoutes;
}
let childTree;
// if there's multiple parallel routes at this level, we shouldn't create an
// optimistic tree for the next level because we don't know which one will
// match the next segment.
if (!isLastSegment && !hasMultipleParallelRoutes) {
const childItem = createOptimisticTree(segments.slice(1), parallelRoutes ? parallelRoutes.children : null, parentRefetch || shouldRefetchThisLevel);
childTree = childItem;
}
const result = [
segment,
{
...parallelRoutes,
...childTree ? {
children: childTree
} : {}
}
];
if (url) {
result[2] = url;
}
if (!parentRefetch && shouldRefetchThisLevel) {
result[3] = "refetch";
} else if (segmentMatches && refresh) {
result[3] = refresh;
}
if (segmentMatches && isRootLayout) {
result[4] = isRootLayout;
}
return result;
}
//# sourceMappingURL=create-optimistic-tree.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/create-optimistic-tree.ts"],"names":["matchSegment","createOptimisticTree","segments","flightRouterState","parentRefetch","existingSegment","existingParallelRoutes","url","refresh","isRootLayout","segment","isLastSegment","length","segmentMatches","hasMultipleParallelRoutes","Object","keys","shouldRefetchThisLevel","parallelRoutes","childTree","childItem","slice","children","result"],"mappings":"AACA,SAASA,YAAY,QAAQ,oBAAmB;AAEhD;;;CAGC,GACD,OAAO,SAASC,qBACdC,QAAkB,EAClBC,iBAA2C,EAC3CC,aAAsB;IAEtB,MAAM,CAACC,iBAAiBC,wBAAwBC,KAAKC,SAASC,aAAa,GACzEN,qBAAqB;QAAC;QAAM,CAAC;KAAE;IACjC,MAAMO,UAAUR,QAAQ,CAAC,EAAE;IAC3B,MAAMS,gBAAgBT,SAASU,MAAM,KAAK;IAE1C,MAAMC,iBACJR,oBAAoB,QAAQL,aAAaK,iBAAiBK;IAE5D,+EAA+E;IAC/E,yEAAyE;IACzE,8CAA8C;IAC9C,MAAMI,4BACJC,OAAOC,IAAI,CAACV,wBAAwBM,MAAM,GAAG;IAC/C,MAAMK,yBACJ,CAACd,qBAAqB,CAACU,kBAAkBC;IAE3C,IAAII,iBAAuC,CAAC;IAC5C,IAAIb,oBAAoB,QAAQQ,gBAAgB;QAC9CK,iBAAiBZ;IACnB;IAEA,IAAIa;IAEJ,4EAA4E;IAC5E,0EAA0E;IAC1E,0BAA0B;IAC1B,IAAI,CAACR,iBAAiB,CAACG,2BAA2B;QAChD,MAAMM,YAAYnB,qBAChBC,SAASmB,KAAK,CAAC,IACfH,iBAAiBA,eAAeI,QAAQ,GAAG,MAC3ClB,iBAAiBa;QAGnBE,YAAYC;IACd;IAEA,MAAMG,SAA4B;QAChCb;QACA;YACE,GAAGQ,cAAc;YACjB,GAAIC,YAAY;gBAAEG,UAAUH;YAAU,IAAI,CAAC,CAAC;QAC9C;KACD;IAED,IAAIZ,KAAK;QACPgB,MAAM,CAAC,EAAE,GAAGhB;IACd;IAEA,IAAI,CAACH,iBAAiBa,wBAAwB;QAC5CM,MAAM,CAAC,EAAE,GAAG;IACd,OAAO,IAAIV,kBAAkBL,SAAS;QACpCe,MAAM,CAAC,EAAE,GAAGf;IACd;IAEA,IAAIK,kBAAkBJ,cAAc;QAClCc,MAAM,CAAC,EAAE,GAAGd;IACd;IAEA,OAAOc;AACT"}

View File

@@ -0,0 +1,20 @@
/**
* Create data fetching record for Promise.
*/ // TODO-APP: change `any` to type inference.
export function createRecordFromThenable(thenable) {
thenable.status = "pending";
thenable.then((value)=>{
if (thenable.status === "pending") {
thenable.status = "fulfilled";
thenable.value = value;
}
}, (err)=>{
if (thenable.status === "pending") {
thenable.status = "rejected";
thenable.value = err;
}
});
return thenable;
}
//# sourceMappingURL=create-record-from-thenable.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/create-record-from-thenable.ts"],"names":["createRecordFromThenable","thenable","status","then","value","err"],"mappings":"AAAA;;CAEC,GACD,4CAA4C;AAC5C,OAAO,SAASA,yBAAyBC,QAAa;IACpDA,SAASC,MAAM,GAAG;IAClBD,SAASE,IAAI,CACX,CAACC;QACC,IAAIH,SAASC,MAAM,KAAK,WAAW;YACjCD,SAASC,MAAM,GAAG;YAClBD,SAASG,KAAK,GAAGA;QACnB;IACF,GACA,CAACC;QACC,IAAIJ,SAASC,MAAM,KAAK,WAAW;YACjCD,SAASC,MAAM,GAAG;YAClBD,SAASG,KAAK,GAAGC;QACnB;IACF;IAEF,OAAOJ;AACT"}

View File

@@ -0,0 +1,6 @@
export function createRouterCacheKey(segment, withoutSearchParameters) {
if (withoutSearchParameters === void 0) withoutSearchParameters = false;
return Array.isArray(segment) ? segment[0] + "|" + segment[1] + "|" + segment[2] : withoutSearchParameters && segment.startsWith("__PAGE__") ? "__PAGE__" : segment;
}
//# sourceMappingURL=create-router-cache-key.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/create-router-cache-key.ts"],"names":["createRouterCacheKey","segment","withoutSearchParameters","Array","isArray","startsWith"],"mappings":"AAEA,OAAO,SAASA,qBACdC,OAAgB,EAChBC,uBAAwC;IAAxCA,IAAAA,oCAAAA,0BAAmC;IAEnC,OAAOC,MAAMC,OAAO,CAACH,WACjB,AAAGA,OAAO,CAAC,EAAE,GAAC,MAAGA,OAAO,CAAC,EAAE,GAAC,MAAGA,OAAO,CAAC,EAAE,GACzCC,2BAA2BD,QAAQI,UAAU,CAAC,cAC9C,aACAJ;AACN"}

View File

@@ -0,0 +1,100 @@
"use client";
// @ts-ignore
// eslint-disable-next-line import/no-extraneous-dependencies
// import { createFromFetch } from 'react-server-dom-webpack/client'
const { createFromFetch } = !!process.env.NEXT_RUNTIME ? require("react-server-dom-webpack/client.edge") : require("react-server-dom-webpack/client");
import { NEXT_ROUTER_PREFETCH, NEXT_ROUTER_STATE_TREE, NEXT_RSC_UNION_QUERY, NEXT_URL, RSC, RSC_CONTENT_TYPE_HEADER } from "../app-router-headers";
import { urlToUrlWithoutFlightMarker } from "../app-router";
import { callServer } from "../../app-call-server";
import { PrefetchKind } from "./router-reducer-types";
import { hexHash } from "../../../shared/lib/hash";
function doMpaNavigation(url) {
return [
urlToUrlWithoutFlightMarker(url).toString(),
undefined
];
}
/**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
*/ export async function fetchServerResponse(url, flightRouterState, nextUrl, currentBuildId, prefetchKind) {
const headers = {
// Enable flight response
[RSC]: "1",
// Provide the current router state
[NEXT_ROUTER_STATE_TREE]: encodeURIComponent(JSON.stringify(flightRouterState))
};
/**
* Three cases:
* - `prefetchKind` is `undefined`, it means it's a normal navigation, so we want to prefetch the page data fully
* - `prefetchKind` is `full` - we want to prefetch the whole page so same as above
* - `prefetchKind` is `auto` - if the page is dynamic, prefetch the page data partially, if static prefetch the page data fully
*/ if (prefetchKind === PrefetchKind.AUTO) {
headers[NEXT_ROUTER_PREFETCH] = "1";
}
if (nextUrl) {
headers[NEXT_URL] = nextUrl;
}
const uniqueCacheQuery = hexHash([
headers[NEXT_ROUTER_PREFETCH] || "0",
headers[NEXT_ROUTER_STATE_TREE],
headers[NEXT_URL]
].join(","));
try {
let fetchUrl = new URL(url);
if (process.env.NODE_ENV === "production") {
if (process.env.__NEXT_CONFIG_OUTPUT === "export") {
if (fetchUrl.pathname.endsWith("/")) {
fetchUrl.pathname += "index.txt";
} else {
fetchUrl.pathname += ".txt";
}
}
}
// Add unique cache query to avoid caching conflicts on CDN which don't respect to Vary header
fetchUrl.searchParams.set(NEXT_RSC_UNION_QUERY, uniqueCacheQuery);
const res = await fetch(fetchUrl, {
// Backwards compat for older browsers. `same-origin` is the default in modern browsers.
credentials: "same-origin",
headers
});
const responseUrl = urlToUrlWithoutFlightMarker(res.url);
const canonicalUrl = res.redirected ? responseUrl : undefined;
const contentType = res.headers.get("content-type") || "";
let isFlightResponse = contentType === RSC_CONTENT_TYPE_HEADER;
if (process.env.NODE_ENV === "production") {
if (process.env.__NEXT_CONFIG_OUTPUT === "export") {
if (!isFlightResponse) {
isFlightResponse = contentType.startsWith("text/plain");
}
}
}
// If fetch returns something different than flight response handle it like a mpa navigation
// If the fetch was not 200, we also handle it like a mpa navigation
if (!isFlightResponse || !res.ok) {
return doMpaNavigation(responseUrl.toString());
}
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const [buildId, flightData] = await createFromFetch(Promise.resolve(res), {
callServer
});
if (currentBuildId !== buildId) {
return doMpaNavigation(res.url);
}
return [
flightData,
canonicalUrl
];
} catch (err) {
console.error("Failed to fetch RSC payload. Falling back to browser navigation.", err);
// If fetch fails handle it like a mpa navigation
// TODO-APP: Add a test for the case where a CORS request fails, e.g. external url redirect coming from the response.
// See https://github.com/vercel/next.js/issues/43605#issuecomment-1451617521 for a reproduction.
return [
url.toString(),
undefined
];
}
}
//# sourceMappingURL=fetch-server-response.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/fetch-server-response.ts"],"names":["createFromFetch","process","env","NEXT_RUNTIME","require","NEXT_ROUTER_PREFETCH","NEXT_ROUTER_STATE_TREE","NEXT_RSC_UNION_QUERY","NEXT_URL","RSC","RSC_CONTENT_TYPE_HEADER","urlToUrlWithoutFlightMarker","callServer","PrefetchKind","hexHash","doMpaNavigation","url","toString","undefined","fetchServerResponse","flightRouterState","nextUrl","currentBuildId","prefetchKind","headers","encodeURIComponent","JSON","stringify","AUTO","uniqueCacheQuery","join","fetchUrl","URL","NODE_ENV","__NEXT_CONFIG_OUTPUT","pathname","endsWith","searchParams","set","res","fetch","credentials","responseUrl","canonicalUrl","redirected","contentType","get","isFlightResponse","startsWith","ok","buildId","flightData","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,QAAQ,CAACC,QAAQ,CAAC,MAAM;oBACnCL,SAASI,QAAQ,IAAI;gBACvB,OAAO;oBACLJ,SAASI,QAAQ,IAAI;gBACvB;YACF;QACF;QAEA,8FAA8F;QAC9FJ,SAASM,YAAY,CAACC,GAAG,CAAC/B,sBAAsBsB;QAEhD,MAAMU,MAAM,MAAMC,MAAMT,UAAU;YAChC,wFAAwF;YACxFU,aAAa;YACbjB;QACF;QAEA,MAAMkB,cAAc/B,4BAA4B4B,IAAIvB,GAAG;QACvD,MAAM2B,eAAeJ,IAAIK,UAAU,GAAGF,cAAcxB;QAEpD,MAAM2B,cAAcN,IAAIf,OAAO,CAACsB,GAAG,CAAC,mBAAmB;QACvD,IAAIC,mBAAmBF,gBAAgBnC;QAEvC,IAAIT,QAAQC,GAAG,CAAC+B,QAAQ,KAAK,cAAc;YACzC,IAAIhC,QAAQC,GAAG,CAACgC,oBAAoB,KAAK,UAAU;gBACjD,IAAI,CAACa,kBAAkB;oBACrBA,mBAAmBF,YAAYG,UAAU,CAAC;gBAC5C;YACF;QACF;QAEA,4FAA4F;QAC5F,oEAAoE;QACpE,IAAI,CAACD,oBAAoB,CAACR,IAAIU,EAAE,EAAE;YAChC,OAAOlC,gBAAgB2B,YAAYzB,QAAQ;QAC7C;QAEA,2EAA2E;QAC3E,MAAM,CAACiC,SAASC,WAAW,GAAuB,MAAMnD,gBACtDoD,QAAQC,OAAO,CAACd,MAChB;YACE3B;QACF;QAGF,IAAIU,mBAAmB4B,SAAS;YAC9B,OAAOnC,gBAAgBwB,IAAIvB,GAAG;QAChC;QAEA,OAAO;YAACmC;YAAYR;SAAa;IACnC,EAAE,OAAOW,KAAK;QACZC,QAAQC,KAAK,CACX,oEACAF;QAEF,iDAAiD;QACjD,qHAAqH;QACrH,iGAAiG;QACjG,OAAO;YAACtC,IAAIC,QAAQ;YAAIC;SAAU;IACpC;AACF"}

View File

@@ -0,0 +1,62 @@
import { CacheStates } from "../../../shared/lib/app-router-context.shared-runtime";
import { createRouterCacheKey } from "./create-router-cache-key";
/**
* Kick off fetch based on the common layout between two routes. Fill cache with data property holding the in-progress fetch.
*/ export function fillCacheWithDataProperty(newCache, existingCache, flightSegmentPath, fetchResponse, bailOnParallelRoutes) {
if (bailOnParallelRoutes === void 0) bailOnParallelRoutes = false;
const isLastEntry = flightSegmentPath.length <= 2;
const [parallelRouteKey, segment] = flightSegmentPath;
const cacheKey = createRouterCacheKey(segment);
const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
if (!existingChildSegmentMap || bailOnParallelRoutes && existingCache.parallelRoutes.size > 1) {
// Bailout because the existing cache does not have the path to the leaf node
// or the existing cache has multiple parallel routes
// Will trigger lazy fetch in layout-router because of missing segment
return {
bailOptimistic: true
};
}
let childSegmentMap = newCache.parallelRoutes.get(parallelRouteKey);
if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
childSegmentMap = new Map(existingChildSegmentMap);
newCache.parallelRoutes.set(parallelRouteKey, childSegmentMap);
}
const existingChildCacheNode = existingChildSegmentMap.get(cacheKey);
let childCacheNode = childSegmentMap.get(cacheKey);
// In case of last segment start off the fetch at this level and don't copy further down.
if (isLastEntry) {
if (!childCacheNode || !childCacheNode.data || childCacheNode === existingChildCacheNode) {
childSegmentMap.set(cacheKey, {
status: CacheStates.DATA_FETCH,
data: fetchResponse(),
subTreeData: null,
parallelRoutes: new Map()
});
}
return;
}
if (!childCacheNode || !existingChildCacheNode) {
// Start fetch in the place where the existing cache doesn't have the data yet.
if (!childCacheNode) {
childSegmentMap.set(cacheKey, {
status: CacheStates.DATA_FETCH,
data: fetchResponse(),
subTreeData: null,
parallelRoutes: new Map()
});
}
return;
}
if (childCacheNode === existingChildCacheNode) {
childCacheNode = {
status: childCacheNode.status,
data: childCacheNode.data,
subTreeData: childCacheNode.subTreeData,
parallelRoutes: new Map(childCacheNode.parallelRoutes)
};
childSegmentMap.set(cacheKey, childCacheNode);
}
return fillCacheWithDataProperty(childCacheNode, existingChildCacheNode, flightSegmentPath.slice(2), fetchResponse);
}
//# sourceMappingURL=fill-cache-with-data-property.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/fill-cache-with-data-property.ts"],"names":["CacheStates","createRouterCacheKey","fillCacheWithDataProperty","newCache","existingCache","flightSegmentPath","fetchResponse","bailOnParallelRoutes","isLastEntry","length","parallelRouteKey","segment","cacheKey","existingChildSegmentMap","parallelRoutes","get","size","bailOptimistic","childSegmentMap","Map","set","existingChildCacheNode","childCacheNode","data","status","DATA_FETCH","subTreeData","slice"],"mappings":"AACA,SAEEA,WAAW,QACN,wDAAuD;AAC9D,SAASC,oBAAoB,QAAQ,4BAA2B;AAGhE;;CAEC,GACD,OAAO,SAASC,0BACdC,QAAmB,EACnBC,aAAwB,EACxBC,iBAAoC,EACpCC,aAA2D,EAC3DC,oBAAqC;IAArCA,IAAAA,iCAAAA,uBAAgC;IAEhC,MAAMC,cAAcH,kBAAkBI,MAAM,IAAI;IAEhD,MAAM,CAACC,kBAAkBC,QAAQ,GAAGN;IACpC,MAAMO,WAAWX,qBAAqBU;IAEtC,MAAME,0BACJT,cAAcU,cAAc,CAACC,GAAG,CAACL;IAEnC,IACE,CAACG,2BACAN,wBAAwBH,cAAcU,cAAc,CAACE,IAAI,GAAG,GAC7D;QACA,6EAA6E;QAC7E,qDAAqD;QACrD,sEAAsE;QACtE,OAAO;YAAEC,gBAAgB;QAAK;IAChC;IAEA,IAAIC,kBAAkBf,SAASW,cAAc,CAACC,GAAG,CAACL;IAElD,IAAI,CAACQ,mBAAmBA,oBAAoBL,yBAAyB;QACnEK,kBAAkB,IAAIC,IAAIN;QAC1BV,SAASW,cAAc,CAACM,GAAG,CAACV,kBAAkBQ;IAChD;IAEA,MAAMG,yBAAyBR,wBAAwBE,GAAG,CAACH;IAC3D,IAAIU,iBAAiBJ,gBAAgBH,GAAG,CAACH;IAEzC,yFAAyF;IACzF,IAAIJ,aAAa;QACf,IACE,CAACc,kBACD,CAACA,eAAeC,IAAI,IACpBD,mBAAmBD,wBACnB;YACAH,gBAAgBE,GAAG,CAACR,UAAU;gBAC5BY,QAAQxB,YAAYyB,UAAU;gBAC9BF,MAAMjB;gBACNoB,aAAa;gBACbZ,gBAAgB,IAAIK;YACtB;QACF;QACA;IACF;IAEA,IAAI,CAACG,kBAAkB,CAACD,wBAAwB;QAC9C,+EAA+E;QAC/E,IAAI,CAACC,gBAAgB;YACnBJ,gBAAgBE,GAAG,CAACR,UAAU;gBAC5BY,QAAQxB,YAAYyB,UAAU;gBAC9BF,MAAMjB;gBACNoB,aAAa;gBACbZ,gBAAgB,IAAIK;YACtB;QACF;QACA;IACF;IAEA,IAAIG,mBAAmBD,wBAAwB;QAC7CC,iBAAiB;YACfE,QAAQF,eAAeE,MAAM;YAC7BD,MAAMD,eAAeC,IAAI;YACzBG,aAAaJ,eAAeI,WAAW;YACvCZ,gBAAgB,IAAIK,IAAIG,eAAeR,cAAc;QACvD;QACAI,gBAAgBE,GAAG,CAACR,UAAUU;IAChC;IAEA,OAAOpB,0BACLoB,gBACAD,wBACAhB,kBAAkBsB,KAAK,CAAC,IACxBrB;AAEJ"}

View File

@@ -0,0 +1,58 @@
import { CacheStates } from "../../../shared/lib/app-router-context.shared-runtime";
import { invalidateCacheByRouterState } from "./invalidate-cache-by-router-state";
import { fillLazyItemsTillLeafWithHead } from "./fill-lazy-items-till-leaf-with-head";
import { createRouterCacheKey } from "./create-router-cache-key";
/**
* Fill cache with subTreeData based on flightDataPath
*/ export function fillCacheWithNewSubTreeData(newCache, existingCache, flightDataPath, wasPrefetched) {
const isLastEntry = flightDataPath.length <= 5;
const [parallelRouteKey, segment] = flightDataPath;
const cacheKey = createRouterCacheKey(segment);
const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
if (!existingChildSegmentMap) {
// Bailout because the existing cache does not have the path to the leaf node
// Will trigger lazy fetch in layout-router because of missing segment
return;
}
let childSegmentMap = newCache.parallelRoutes.get(parallelRouteKey);
if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
childSegmentMap = new Map(existingChildSegmentMap);
newCache.parallelRoutes.set(parallelRouteKey, childSegmentMap);
}
const existingChildCacheNode = existingChildSegmentMap.get(cacheKey);
let childCacheNode = childSegmentMap.get(cacheKey);
if (isLastEntry) {
if (!childCacheNode || !childCacheNode.data || childCacheNode === existingChildCacheNode) {
childCacheNode = {
status: CacheStates.READY,
data: null,
subTreeData: flightDataPath[3],
// Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode ? new Map(existingChildCacheNode.parallelRoutes) : new Map()
};
if (existingChildCacheNode) {
invalidateCacheByRouterState(childCacheNode, existingChildCacheNode, flightDataPath[2]);
}
fillLazyItemsTillLeafWithHead(childCacheNode, existingChildCacheNode, flightDataPath[2], flightDataPath[4], wasPrefetched);
childSegmentMap.set(cacheKey, childCacheNode);
}
return;
}
if (!childCacheNode || !existingChildCacheNode) {
// Bailout because the existing cache does not have the path to the leaf node
// Will trigger lazy fetch in layout-router because of missing segment
return;
}
if (childCacheNode === existingChildCacheNode) {
childCacheNode = {
status: childCacheNode.status,
data: childCacheNode.data,
subTreeData: childCacheNode.subTreeData,
parallelRoutes: new Map(childCacheNode.parallelRoutes)
};
childSegmentMap.set(cacheKey, childCacheNode);
}
fillCacheWithNewSubTreeData(childCacheNode, existingChildCacheNode, flightDataPath.slice(2), wasPrefetched);
}
//# sourceMappingURL=fill-cache-with-new-subtree-data.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/fill-cache-with-new-subtree-data.ts"],"names":["CacheStates","invalidateCacheByRouterState","fillLazyItemsTillLeafWithHead","createRouterCacheKey","fillCacheWithNewSubTreeData","newCache","existingCache","flightDataPath","wasPrefetched","isLastEntry","length","parallelRouteKey","segment","cacheKey","existingChildSegmentMap","parallelRoutes","get","childSegmentMap","Map","set","existingChildCacheNode","childCacheNode","data","status","READY","subTreeData","slice"],"mappings":"AAAA,SAEEA,WAAW,QACN,wDAAu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,wBAAwB;QAC9C,6EAA6E;QAC7E,sEAAsE;QACtE;IACF;IAEA,IAAIC,mBAAmBD,wBAAwB;QAC7CC,iBAAiB;YACfE,QAAQF,eAAeE,MAAM;YAC7BD,MAAMD,eAAeC,IAAI;YACzBG,aAAaJ,eAAeI,WAAW;YACvCV,gBAAgB,IAAIG,IAAIG,eAAeN,cAAc;QACvD;QACAE,gBAAgBE,GAAG,CAACN,UAAUQ;IAChC;IAEAjB,4BACEiB,gBACAD,wBACAb,eAAemB,KAAK,CAAC,IACrBlB;AAEJ"}

View File

@@ -0,0 +1,59 @@
import { CacheStates } from "../../../shared/lib/app-router-context.shared-runtime";
import { createRouterCacheKey } from "./create-router-cache-key";
export function fillLazyItemsTillLeafWithHead(newCache, existingCache, routerState, head, wasPrefetched) {
const isLastSegment = Object.keys(routerState[1]).length === 0;
if (isLastSegment) {
newCache.head = head;
return;
}
// Remove segment that we got data for so that it is filled in during rendering of subTreeData.
for(const key in routerState[1]){
const parallelRouteState = routerState[1][key];
const segmentForParallelRoute = parallelRouteState[0];
const cacheKey = createRouterCacheKey(segmentForParallelRoute);
if (existingCache) {
const existingParallelRoutesCacheNode = existingCache.parallelRoutes.get(key);
if (existingParallelRoutesCacheNode) {
let parallelRouteCacheNode = new Map(existingParallelRoutesCacheNode);
const existingCacheNode = parallelRouteCacheNode.get(cacheKey);
const newCacheNode = wasPrefetched && existingCacheNode ? {
status: existingCacheNode.status,
data: existingCacheNode.data,
subTreeData: existingCacheNode.subTreeData,
parallelRoutes: new Map(existingCacheNode.parallelRoutes)
} : {
status: CacheStates.LAZY_INITIALIZED,
data: null,
subTreeData: null,
parallelRoutes: new Map(existingCacheNode == null ? void 0 : existingCacheNode.parallelRoutes)
};
// Overrides the cache key with the new cache node.
parallelRouteCacheNode.set(cacheKey, newCacheNode);
// Traverse deeper to apply the head / fill lazy items till the head.
fillLazyItemsTillLeafWithHead(newCacheNode, existingCacheNode, parallelRouteState, head, wasPrefetched);
newCache.parallelRoutes.set(key, parallelRouteCacheNode);
continue;
}
}
const newCacheNode = {
status: CacheStates.LAZY_INITIALIZED,
data: null,
subTreeData: null,
parallelRoutes: new Map()
};
const existingParallelRoutes = newCache.parallelRoutes.get(key);
if (existingParallelRoutes) {
existingParallelRoutes.set(cacheKey, newCacheNode);
} else {
newCache.parallelRoutes.set(key, new Map([
[
cacheKey,
newCacheNode
]
]));
}
fillLazyItemsTillLeafWithHead(newCacheNode, undefined, parallelRouteState, head, wasPrefetched);
}
}
//# sourceMappingURL=fill-lazy-items-till-leaf-with-head.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/fill-lazy-items-till-leaf-with-head.ts"],"names":["CacheStates","createRouterCacheKey","fillLazyItemsTillLeafWithHead","newCache","existingCache","routerState","head","wasPrefetched","isLastSegment","Object","keys","length","key","parallelRouteState","segmentForParallelRoute","cacheKey","existingParallelRoutesCacheNode","parallelRoutes","get","parallelRouteCacheNode","Map","existingCacheNode","newCacheNode","status","data","subTreeData","LAZY_INITIALIZED","set","existingParallelRoutes","undefined"],"mappings":"AAAA,SAEEA,WAAW,QACN,wDAAuD;AAE9D,SAASC,oBAAoB,QAAQ,4BAA2B;AAEhE,OAAO,SAASC,8BACdC,QAAmB,EACnBC,aAAoC,EACpCC,WAA8B,EAC9BC,IAAqB,EACrBC,aAAuB;IAEvB,MAAMC,gBAAgBC,OAAOC,IAAI,CAACL,WAAW,CAAC,EAAE,EAAEM,MAAM,KAAK;IAC7D,IAAIH,eAAe;QACjBL,SAASG,IAAI,GAAGA;QAChB;IACF;IACA,+FAA+F;IAC/F,IAAK,MAAMM,OAAOP,WAAW,CAAC,EAAE,CAAE;QAChC,MAAMQ,qBAAqBR,WAAW,CAAC,EAAE,CAACO,IAAI;QAC9C,MAAME,0BAA0BD,kBAAkB,CAAC,EAAE;QACrD,MAAME,WAAWd,qBAAqBa;QAEtC,IAAIV,eAAe;YACjB,MAAMY,kCACJZ,cAAca,cAAc,CAACC,GAAG,CAACN;YACnC,IAAII,iCAAiC;gBACnC,IAAIG,yBAAyB,IAAIC,IAAIJ;gBACrC,MAAMK,oBAAoBF,uBAAuBD,GAAG,CAACH;gBACrD,MAAMO,eACJf,iBAAiBc,oBACZ;oBACCE,QAAQF,kBAAkBE,MAAM;oBAChCC,MAAMH,kBAAkBG,IAAI;oBAC5BC,aAAaJ,kBAAkBI,WAAW;oBAC1CR,gBAAgB,IAAIG,IAAIC,kBAAkBJ,cAAc;gBAC1D,IACA;oBACEM,QAAQvB,YAAY0B,gBAAgB;oBACpCF,MAAM;oBACNC,aAAa;oBACbR,gBAAgB,IAAIG,IAAIC,qCAAAA,kBAAmBJ,cAAc;gBAC3D;gBACN,mDAAmD;gBACnDE,uBAAuBQ,GAAG,CAACZ,UAAUO;gBACrC,qEAAqE;gBACrEpB,8BACEoB,cACAD,mBACAR,oBACAP,MACAC;gBAGFJ,SAASc,cAAc,CAACU,GAAG,CAACf,KAAKO;gBACjC;YACF;QACF;QAEA,MAAMG,eAA0B;YAC9BC,QAAQvB,YAAY0B,gBAAgB;YACpCF,MAAM;YACNC,aAAa;YACbR,gBAAgB,IAAIG;QACtB;QAEA,MAAMQ,yBAAyBzB,SAASc,cAAc,CAACC,GAAG,CAACN;QAC3D,IAAIgB,wBAAwB;YAC1BA,uBAAuBD,GAAG,CAACZ,UAAUO;QACvC,OAAO;YACLnB,SAASc,cAAc,CAACU,GAAG,CAACf,KAAK,IAAIQ,IAAI;gBAAC;oBAACL;oBAAUO;iBAAa;aAAC;QACrE;QAEApB,8BACEoB,cACAO,WACAhB,oBACAP,MACAC;IAEJ;AACF"}

View File

@@ -0,0 +1,31 @@
const FIVE_MINUTES = 5 * 60 * 1000;
const THIRTY_SECONDS = 30 * 1000;
export var PrefetchCacheEntryStatus;
(function(PrefetchCacheEntryStatus) {
PrefetchCacheEntryStatus["fresh"] = "fresh";
PrefetchCacheEntryStatus["reusable"] = "reusable";
PrefetchCacheEntryStatus["expired"] = "expired";
PrefetchCacheEntryStatus["stale"] = "stale";
})(PrefetchCacheEntryStatus || (PrefetchCacheEntryStatus = {}));
export function getPrefetchEntryCacheStatus(param) {
let { kind, prefetchTime, lastUsedTime } = param;
// if the cache entry was prefetched or read less than 30s ago, then we want to re-use it
if (Date.now() < (lastUsedTime != null ? lastUsedTime : prefetchTime) + THIRTY_SECONDS) {
return lastUsedTime ? "reusable" : "fresh";
}
// if the cache entry was prefetched less than 5 mins ago, then we want to re-use only the loading state
if (kind === "auto") {
if (Date.now() < prefetchTime + FIVE_MINUTES) {
return "stale";
}
}
// if the cache entry was prefetched less than 5 mins ago and was a "full" prefetch, then we want to re-use it "full
if (kind === "full") {
if (Date.now() < prefetchTime + FIVE_MINUTES) {
return "reusable";
}
}
return "expired";
}
//# sourceMappingURL=get-prefetch-cache-entry-status.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/get-prefetch-cache-entry-status.ts"],"names":["FIVE_MINUTES","THIRTY_SECONDS","PrefetchCacheEntryStatus","fresh","reusable","expired","stale","getPrefetchEntryCacheStatus","kind","prefetchTime","lastUsedTime","Date","now"],"mappings":"AAEA,MAAMA,eAAe,IAAI,KAAK;AAC9B,MAAMC,iBAAiB,KAAK;WAErB;UAAKC,wBAAwB;IAAxBA,yBACVC,WAAAA;IADUD,yBAEVE,cAAAA;IAFUF,yBAGVG,aAAAA;IAHUH,yBAIVI,WAAAA;GAJUJ,6BAAAA;AAOZ,OAAO,SAASK,4BAA4B,KAIvB;IAJuB,IAAA,EAC1CC,IAAI,EACJC,YAAY,EACZC,YAAY,EACO,GAJuB;IAK1C,yFAAyF;IACzF,IAAIC,KAAKC,GAAG,KAAK,AAACF,CAAAA,uBAAAA,eAAgBD,YAAW,IAAKR,gBAAgB;QAChE,OAAOS,eAZE,aADH;IAgBR;IAEA,wGAAwG;IACxG,IAAIF,SAAS,QAAQ;QACnB,IAAIG,KAAKC,GAAG,KAAKH,eAAeT,cAAc;YAC5C,OAlBI;QAmBN;IACF;IAEA,oHAAoH;IACpH,IAAIQ,SAAS,QAAQ;QACnB,IAAIG,KAAKC,GAAG,KAAKH,eAAeT,cAAc;YAC5C,OA3BO;QA4BT;IACF;IAEA,OA9BU;AA+BZ"}

View File

@@ -0,0 +1,33 @@
import { computeChangedPath } from "./compute-changed-path";
export function handleMutable(state, mutable) {
var _mutable_canonicalUrl;
var _mutable_shouldScroll;
// shouldScroll is true by default, can override to false.
const shouldScroll = (_mutable_shouldScroll = mutable.shouldScroll) != null ? _mutable_shouldScroll : true;
var _mutable_scrollableSegments, _computeChangedPath;
return {
buildId: state.buildId,
// Set href.
canonicalUrl: mutable.canonicalUrl != null ? mutable.canonicalUrl === state.canonicalUrl ? state.canonicalUrl : mutable.canonicalUrl : state.canonicalUrl,
pushRef: {
pendingPush: mutable.pendingPush != null ? mutable.pendingPush : state.pushRef.pendingPush,
mpaNavigation: mutable.mpaNavigation != null ? mutable.mpaNavigation : state.pushRef.mpaNavigation
},
// All navigation requires scroll and focus management to trigger.
focusAndScrollRef: {
apply: shouldScroll ? (mutable == null ? void 0 : mutable.scrollableSegments) !== undefined ? true : state.focusAndScrollRef.apply : false,
onlyHashChange: !!mutable.hashFragment && state.canonicalUrl.split("#")[0] === ((_mutable_canonicalUrl = mutable.canonicalUrl) == null ? void 0 : _mutable_canonicalUrl.split("#")[0]),
hashFragment: shouldScroll ? // #top is handled in layout-router.
mutable.hashFragment && mutable.hashFragment !== "" ? decodeURIComponent(mutable.hashFragment.slice(1)) : state.focusAndScrollRef.hashFragment : null,
segmentPaths: shouldScroll ? (_mutable_scrollableSegments = mutable == null ? void 0 : mutable.scrollableSegments) != null ? _mutable_scrollableSegments : state.focusAndScrollRef.segmentPaths : []
},
// Apply cache.
cache: mutable.cache ? mutable.cache : state.cache,
prefetchCache: mutable.prefetchCache ? mutable.prefetchCache : state.prefetchCache,
// Apply patched router state.
tree: mutable.patchedTree !== undefined ? mutable.patchedTree : state.tree,
nextUrl: mutable.patchedTree !== undefined ? (_computeChangedPath = computeChangedPath(state.tree, mutable.patchedTree)) != null ? _computeChangedPath : state.canonicalUrl : state.nextUrl
};
}
//# sourceMappingURL=handle-mutable.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/handle-mutable.ts"],"names":["computeChangedPath","handleMutable","state","mutable","shouldScroll","buildId","canonicalUrl","pushRef","pendingPush","mpaNavigation","focusAndScrollRef","apply","scrollableSegments","undefined","onlyHashChange","hashFragment","split","decodeURIComponent","slice","segmentPaths","cache","prefetchCache","tree","patchedTree","nextUrl"],"mappings":"AAAA,SAASA,kBAAkB,QAAQ,yBAAwB;AAO3D,OAAO,SAASC,cACdC,KAA2B,EAC3BC,OAAgB;QAmCRA;QAhCaA;IADrB,0DAA0D;IAC1D,MAAMC,eAAeD,CAAAA,wBAAAA,QAAQC,YAAY,YAApBD,wBAAwB;QA2CrCA,6BAaAH;IAtDR,OAAO;QACLK,SAASH,MAAMG,OAAO;QACtB,YAAY;QACZC,cACEH,QAAQG,YAAY,IAAI,OACpBH,QAAQG,YAAY,KAAKJ,MAAMI,YAAY,GACzCJ,MAAMI,YAAY,GAClBH,QAAQG,YAAY,GACtBJ,MAAMI,YAAY;QACxBC,SAAS;YACPC,aACEL,QAAQK,WAAW,IAAI,OACnBL,QAAQK,WAAW,GACnBN,MAAMK,OAAO,CAACC,WAAW;YAC/BC,eACEN,QAAQM,aAAa,IAAI,OACrBN,QAAQM,aAAa,GACrBP,MAAMK,OAAO,CAACE,aAAa;QACnC;QACA,kEAAkE;QAClEC,mBAAmB;YACjBC,OAAOP,eACHD,CAAAA,2BAAAA,QAASS,kBAAkB,MAAKC,YAC9B,OACAX,MAAMQ,iBAAiB,CAACC,KAAK,GAE/B;YACJG,gBACE,CAAC,CAACX,QAAQY,YAAY,IACtBb,MAAMI,YAAY,CAACU,KAAK,CAAC,IAAI,CAAC,EAAE,OAC9Bb,wBAAAA,QAAQG,YAAY,qBAApBH,sBAAsBa,KAAK,CAAC,IAAI,CAAC,EAAE;YACvCD,cAAcX,eAEV,oCAAoC;YACpCD,QAAQY,YAAY,IAAIZ,QAAQY,YAAY,KAAK,KAE/CE,mBAAmBd,QAAQY,YAAY,CAACG,KAAK,CAAC,MAC9ChB,MAAMQ,iBAAiB,CAACK,YAAY,GAEtC;YACJI,cAAcf,eACVD,CAAAA,8BAAAA,2BAAAA,QAASS,kBAAkB,YAA3BT,8BAA+BD,MAAMQ,iBAAiB,CAACS,YAAY,GAEnE,EAAE;QACR;QACA,eAAe;QACfC,OAAOjB,QAAQiB,KAAK,GAAGjB,QAAQiB,KAAK,GAAGlB,MAAMkB,KAAK;QAClDC,eAAelB,QAAQkB,aAAa,GAChClB,QAAQkB,aAAa,GACrBnB,MAAMmB,aAAa;QACvB,8BAA8B;QAC9BC,MAAMnB,QAAQoB,WAAW,KAAKV,YAAYV,QAAQoB,WAAW,GAAGrB,MAAMoB,IAAI;QAC1EE,SACErB,QAAQoB,WAAW,KAAKV,YACpBb,CAAAA,sBAAAA,mBAAmBE,MAAMoB,IAAI,EAAEnB,QAAQoB,WAAW,aAAlDvB,sBACAE,MAAMI,YAAY,GAClBJ,MAAMsB,OAAO;IACrB;AACF"}

View File

@@ -0,0 +1,43 @@
import { createRouterCacheKey } from "./create-router-cache-key";
/**
* Fill cache up to the end of the flightSegmentPath, invalidating anything below it.
*/ export function invalidateCacheBelowFlightSegmentPath(newCache, existingCache, flightSegmentPath) {
const isLastEntry = flightSegmentPath.length <= 2;
const [parallelRouteKey, segment] = flightSegmentPath;
const cacheKey = createRouterCacheKey(segment);
const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
if (!existingChildSegmentMap) {
// Bailout because the existing cache does not have the path to the leaf node
// Will trigger lazy fetch in layout-router because of missing segment
return;
}
let childSegmentMap = newCache.parallelRoutes.get(parallelRouteKey);
if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
childSegmentMap = new Map(existingChildSegmentMap);
newCache.parallelRoutes.set(parallelRouteKey, childSegmentMap);
}
// In case of last entry don't copy further down.
if (isLastEntry) {
childSegmentMap.delete(cacheKey);
return;
}
const existingChildCacheNode = existingChildSegmentMap.get(cacheKey);
let childCacheNode = childSegmentMap.get(cacheKey);
if (!childCacheNode || !existingChildCacheNode) {
// Bailout because the existing cache does not have the path to the leaf node
// Will trigger lazy fetch in layout-router because of missing segment
return;
}
if (childCacheNode === existingChildCacheNode) {
childCacheNode = {
status: childCacheNode.status,
data: childCacheNode.data,
subTreeData: childCacheNode.subTreeData,
parallelRoutes: new Map(childCacheNode.parallelRoutes)
};
childSegmentMap.set(cacheKey, childCacheNode);
}
invalidateCacheBelowFlightSegmentPath(childCacheNode, existingChildCacheNode, flightSegmentPath.slice(2));
}
//# sourceMappingURL=invalidate-cache-below-flight-segmentpath.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/invalidate-cache-below-flight-segmentpath.ts"],"names":["createRouterCacheKey","invalidateCacheBelowFlightSegmentPath","newCache","existingCache","flightSegmentPath","isLastEntry","length","parallelRouteKey","segment","cacheKey","existingChildSegmentMap","parallelRoutes","get","childSegmentMap","Map","set","delete","existingChildCacheNode","childCacheNode","status","data","subTreeData","slice"],"mappings":"AAEA,SAASA,oBAAoB,QAAQ,4BAA2B;AAEhE;;CAEC,GACD,OAAO,SAASC,sCACdC,QAAmB,EACnBC,aAAwB,EACxBC,iBAAoC;IAEpC,MAAMC,cAAcD,kBAAkBE,MAAM,IAAI;IAChD,MAAM,CAACC,kBAAkBC,QAAQ,GAAGJ;IAEpC,MAAMK,WAAWT,qBAAqBQ;IAEtC,MAAME,0BACJP,cAAcQ,cAAc,CAACC,GAAG,CAACL;IAEnC,IAAI,CAACG,yBAAyB;QAC5B,6EAA6E;QAC7E,sEAAsE;QACtE;IACF;IAEA,IAAIG,kBAAkBX,SAASS,cAAc,CAACC,GAAG,CAACL;IAClD,IAAI,CAACM,mBAAmBA,oBAAoBH,yBAAyB;QACnEG,kBAAkB,IAAIC,IAAIJ;QAC1BR,SAASS,cAAc,CAACI,GAAG,CAACR,kBAAkBM;IAChD;IAEA,iDAAiD;IACjD,IAAIR,aAAa;QACfQ,gBAAgBG,MAAM,CAACP;QACvB;IACF;IAEA,MAAMQ,yBAAyBP,wBAAwBE,GAAG,CAACH;IAC3D,IAAIS,iBAAiBL,gBAAgBD,GAAG,CAACH;IAEzC,IAAI,CAACS,kBAAkB,CAACD,wBAAwB;QAC9C,6EAA6E;QAC7E,sEAAsE;QACtE;IACF;IAEA,IAAIC,mBAAmBD,wBAAwB;QAC7CC,iBAAiB;YACfC,QAAQD,eAAeC,MAAM;YAC7BC,MAAMF,eAAeE,IAAI;YACzBC,aAAaH,eAAeG,WAAW;YACvCV,gBAAgB,IAAIG,IAAII,eAAeP,cAAc;QACvD;QACAE,gBAAgBE,GAAG,CAACN,UAAUS;IAChC;IAEAjB,sCACEiB,gBACAD,wBACAb,kBAAkBkB,KAAK,CAAC;AAE5B"}

View File

@@ -0,0 +1,18 @@
import { createRouterCacheKey } from "./create-router-cache-key";
/**
* Invalidate cache one level down from the router state.
*/ export function invalidateCacheByRouterState(newCache, existingCache, routerState) {
// Remove segment that we got data for so that it is filled in during rendering of subTreeData.
for(const key in routerState[1]){
const segmentForParallelRoute = routerState[1][key][0];
const cacheKey = createRouterCacheKey(segmentForParallelRoute);
const existingParallelRoutesCacheNode = existingCache.parallelRoutes.get(key);
if (existingParallelRoutesCacheNode) {
let parallelRouteCacheNode = new Map(existingParallelRoutesCacheNode);
parallelRouteCacheNode.delete(cacheKey);
newCache.parallelRoutes.set(key, parallelRouteCacheNode);
}
}
}
//# sourceMappingURL=invalidate-cache-by-router-state.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/invalidate-cache-by-router-state.ts"],"names":["createRouterCacheKey","invalidateCacheByRouterState","newCache","existingCache","routerState","key","segmentForParallelRoute","cacheKey","existingParallelRoutesCacheNode","parallelRoutes","get","parallelRouteCacheNode","Map","delete","set"],"mappings":"AAEA,SAASA,oBAAoB,QAAQ,4BAA2B;AAEhE;;CAEC,GACD,OAAO,SAASC,6BACdC,QAAmB,EACnBC,aAAwB,EACxBC,WAA8B;IAE9B,+FAA+F;IAC/F,IAAK,MAAMC,OAAOD,WAAW,CAAC,EAAE,CAAE;QAChC,MAAME,0BAA0BF,WAAW,CAAC,EAAE,CAACC,IAAI,CAAC,EAAE;QACtD,MAAME,WAAWP,qBAAqBM;QACtC,MAAME,kCACJL,cAAcM,cAAc,CAACC,GAAG,CAACL;QACnC,IAAIG,iCAAiC;YACnC,IAAIG,yBAAyB,IAAIC,IAAIJ;YACrCG,uBAAuBE,MAAM,CAACN;YAC9BL,SAASO,cAAc,CAACK,GAAG,CAACT,KAAKM;QACnC;IACF;AACF"}

View File

@@ -0,0 +1,35 @@
export function isNavigatingToNewRootLayout(currentTree, nextTree) {
// Compare segments
const currentTreeSegment = currentTree[0];
const nextTreeSegment = nextTree[0];
// If any segment is different before we find the root layout, the root layout has changed.
// E.g. /same/(group1)/layout.js -> /same/(group2)/layout.js
// First segment is 'same' for both, keep looking. (group1) changed to (group2) before the root layout was found, it must have changed.
if (Array.isArray(currentTreeSegment) && Array.isArray(nextTreeSegment)) {
// Compare dynamic param name and type but ignore the value, different values would not affect the current root layout
// /[name] - /slug1 and /slug2, both values (slug1 & slug2) still has the same layout /[name]/layout.js
if (currentTreeSegment[0] !== nextTreeSegment[0] || currentTreeSegment[2] !== nextTreeSegment[2]) {
return true;
}
} else if (currentTreeSegment !== nextTreeSegment) {
return true;
}
// Current tree root layout found
if (currentTree[4]) {
// If the next tree doesn't have the root layout flag, it must have changed.
return !nextTree[4];
}
// Current tree didn't have its root layout here, must have changed.
if (nextTree[4]) {
return true;
}
// We can't assume it's `parallelRoutes.children` here in case the root layout is `app/@something/layout.js`
// But it's not possible to be more than one parallelRoutes before the root layout is found
// TODO-APP: change to traverse all parallel routes
const currentTreeChild = Object.values(currentTree[1])[0];
const nextTreeChild = Object.values(nextTree[1])[0];
if (!currentTreeChild || !nextTreeChild) return true;
return isNavigatingToNewRootLayout(currentTreeChild, nextTreeChild);
}
//# sourceMappingURL=is-navigating-to-new-root-layout.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/is-navigating-to-new-root-layout.ts"],"names":["isNavigatingToNewRootLayout","currentTree","nextTree","currentTreeSegment","nextTreeSegment","Array","isArray","currentTreeChild","Object","values","nextTreeChild"],"mappings":"AAEA,OAAO,SAASA,4BACdC,WAA8B,EAC9BC,QAA2B;IAE3B,mBAAmB;IACnB,MAAMC,qBAAqBF,WAAW,CAAC,EAAE;IACzC,MAAMG,kBAAkBF,QAAQ,CAAC,EAAE;IACnC,2FAA2F;IAC3F,4DAA4D;IAC5D,uIAAuI;IACvI,IAAIG,MAAMC,OAAO,CAACH,uBAAuBE,MAAMC,OAAO,CAACF,kBAAkB;QACvE,sHAAsH;QACtH,uGAAuG;QACvG,IACED,kBAAkB,CAAC,EAAE,KAAKC,eAAe,CAAC,EAAE,IAC5CD,kBAAkB,CAAC,EAAE,KAAKC,eAAe,CAAC,EAAE,EAC5C;YACA,OAAO;QACT;IACF,OAAO,IAAID,uBAAuBC,iBAAiB;QACjD,OAAO;IACT;IAEA,iCAAiC;IACjC,IAAIH,WAAW,CAAC,EAAE,EAAE;QAClB,4EAA4E;QAC5E,OAAO,CAACC,QAAQ,CAAC,EAAE;IACrB;IACA,qEAAqE;IACrE,IAAIA,QAAQ,CAAC,EAAE,EAAE;QACf,OAAO;IACT;IACA,4GAA4G;IAC5G,2FAA2F;IAC3F,mDAAmD;IACnD,MAAMK,mBAAmBC,OAAOC,MAAM,CAACR,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE;IACzD,MAAMS,gBAAgBF,OAAOC,MAAM,CAACP,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE;IACnD,IAAI,CAACK,oBAAoB,CAACG,eAAe,OAAO;IAChD,OAAOV,4BAA4BO,kBAAkBG;AACvD"}

View File

@@ -0,0 +1,13 @@
/**
* Read record value or throw Promise if it's not resolved yet.
*/ export function readRecordValue(thenable) {
// @ts-expect-error TODO: fix type
if (thenable.status === "fulfilled") {
// @ts-expect-error TODO: fix type
return thenable.value;
} else {
throw thenable;
}
}
//# sourceMappingURL=read-record-value.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/read-record-value.ts"],"names":["readRecordValue","thenable","status","value"],"mappings":"AAAA;;CAEC,GACD,OAAO,SAASA,gBAAmBC,QAAoB;IACrD,kCAAkC;IAClC,IAAIA,SAASC,MAAM,KAAK,aAAa;QACnC,kCAAkC;QAClC,OAAOD,SAASE,KAAK;IACvB,OAAO;QACL,MAAMF;IACR;AACF"}

View File

@@ -0,0 +1,77 @@
import { fetchServerResponse } from "../fetch-server-response";
import { createRecordFromThenable } from "../create-record-from-thenable";
import { readRecordValue } from "../read-record-value";
import { createHrefFromUrl } from "../create-href-from-url";
import { applyRouterStatePatchToTree } from "../apply-router-state-patch-to-tree";
import { isNavigatingToNewRootLayout } from "../is-navigating-to-new-root-layout";
import { handleExternalUrl } from "./navigate-reducer";
import { handleMutable } from "../handle-mutable";
import { applyFlightData } from "../apply-flight-data";
// A version of refresh reducer that keeps the cache around instead of wiping all of it.
function fastRefreshReducerImpl(state, action) {
const { cache, mutable, origin } = action;
const href = state.canonicalUrl;
const isForCurrentTree = JSON.stringify(mutable.previousTree) === JSON.stringify(state.tree);
if (isForCurrentTree) {
return handleMutable(state, mutable);
}
if (!cache.data) {
// TODO-APP: verify that `href` is not an external url.
// Fetch data from the root of the tree.
cache.data = createRecordFromThenable(fetchServerResponse(new URL(href, origin), [
state.tree[0],
state.tree[1],
state.tree[2],
"refetch"
], state.nextUrl, state.buildId));
}
const [flightData, canonicalUrlOverride] = readRecordValue(cache.data);
// Handle case when navigating to page in `pages` from `app`
if (typeof flightData === "string") {
return handleExternalUrl(state, mutable, flightData, state.pushRef.pendingPush);
}
// Remove cache.data as it has been resolved at this point.
cache.data = null;
let currentTree = state.tree;
let currentCache = state.cache;
for (const flightDataPath of flightData){
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
console.log("REFRESH FAILED");
return state;
}
// Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath;
const newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
[
""
], currentTree, treePatch);
if (newTree === null) {
throw new Error("SEGMENT MISMATCH");
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush);
}
const canonicalUrlOverrideHref = canonicalUrlOverride ? createHrefFromUrl(canonicalUrlOverride) : undefined;
if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref;
}
const applied = applyFlightData(currentCache, cache, flightDataPath);
if (applied) {
mutable.cache = cache;
currentCache = cache;
}
mutable.previousTree = currentTree;
mutable.patchedTree = newTree;
mutable.canonicalUrl = href;
currentTree = newTree;
}
return handleMutable(state, mutable);
}
function fastRefreshReducerNoop(state, _action) {
return state;
}
export const fastRefreshReducer = process.env.NODE_ENV === "production" ? fastRefreshReducerNoop : fastRefreshReducerImpl;
//# sourceMappingURL=fast-refresh-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/fast-refresh-reducer.ts"],"names":["fetchServerResponse","createRecordFromThenable","readRecordValue","createHrefFromUrl","applyRouterStatePatchToTree","isNavigatingToNewRootLayout","handleExternalUrl","handleMutable","applyFlightData","fastRefreshReducerImpl","state","action","cache","mutable","origin","href","canonicalUrl","isForCurrentTree","JSON","stringify","previousTree","tree","data","URL","nextUrl","buildId","flightData","canonicalUrlOverride","pushRef","pendingPush","currentTree","currentCache","flightDataPath","length","console","log","treePatch","newTree","Error","canonicalUrlOverrideHref","undefined","applied","patchedTree","fastRefreshReducerNoop","_action","fastRefreshReducer","process","env","NODE_ENV"],"mappings":"AAAA,SAASA,mBAAm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N,WAAY;QACvC,oFAAoF;QACpF,IAAIM,eAAeC,MAAM,KAAK,GAAG;YAC/B,oCAAoC;YACpCC,QAAQC,GAAG,CAAC;YACZ,OAAOzB;QACT;QAEA,2GAA2G;QAC3G,MAAM,CAAC0B,UAAU,GAAGJ;QACpB,MAAMK,UAAUjC,4BACd,sBAAsB;QACtB;YAAC;SAAG,EACJ0B,aACAM;QAGF,IAAIC,YAAY,MAAM;YACpB,MAAM,IAAIC,MAAM;QAClB;QAEA,IAAIjC,4BAA4ByB,aAAaO,UAAU;YACrD,OAAO/B,kBAAkBI,OAAOG,SAASE,MAAML,MAAMkB,OAAO,CAACC,WAAW;QAC1E;QAEA,MAAMU,2BAA2BZ,uBAC7BxB,kBAAkBwB,wBAClBa;QAEJ,IAAIb,sBAAsB;YACxBd,QAAQG,YAAY,GAAGuB;QACzB;QACA,MAAME,UAAUjC,gBAAgBuB,cAAcnB,OAAOoB;QAErD,IAAIS,SAAS;YACX5B,QAAQD,KAAK,GAAGA;YAChBmB,eAAenB;QACjB;QAEAC,QAAQO,YAAY,GAAGU;QACvBjB,QAAQ6B,WAAW,GAAGL;QACtBxB,QAAQG,YAAY,GAAGD;QAEvBe,cAAcO;IAChB;IACA,OAAO9B,cAAcG,OAAOG;AAC9B;AAEA,SAAS8B,uBACPjC,KAA2B,EAC3BkC,OAA0B;IAE1B,OAAOlC;AACT;AAEA,OAAO,MAAMmC,qBACXC,QAAQC,GAAG,CAACC,QAAQ,KAAK,eACrBL,yBACAlC,uBAAsB"}

View File

@@ -0,0 +1,26 @@
import { createRouterCacheKey } from "../create-router-cache-key";
export function findHeadInCache(cache, parallelRoutes) {
const isLastItem = Object.keys(parallelRoutes).length === 0;
if (isLastItem) {
return cache.head;
}
for(const key in parallelRoutes){
const [segment, childParallelRoutes] = parallelRoutes[key];
const childSegmentMap = cache.parallelRoutes.get(key);
if (!childSegmentMap) {
continue;
}
const cacheKey = createRouterCacheKey(segment);
const cacheNode = childSegmentMap.get(cacheKey);
if (!cacheNode) {
continue;
}
const item = findHeadInCache(cacheNode, childParallelRoutes);
if (item) {
return item;
}
}
return undefined;
}
//# sourceMappingURL=find-head-in-cache.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/find-head-in-cache.ts"],"names":["createRouterCacheKey","findHeadInCache","cache","parallelRoutes","isLastItem","Object","keys","length","head","key","segment","childParallelRoutes","childSegmentMap","get","cacheKey","cacheNode","item","undefined"],"mappings":"AAEA,SAASA,oBAAoB,QAAQ,6BAA4B;AAEjE,OAAO,SAASC,gBACdC,KAAgB,EAChBC,cAAoC;IAEpC,MAAMC,aAAaC,OAAOC,IAAI,CAACH,gBAAgBI,MAAM,KAAK;IAC1D,IAAIH,YAAY;QACd,OAAOF,MAAMM,IAAI;IACnB;IACA,IAAK,MAAMC,OAAON,eAAgB;QAChC,MAAM,CAACO,SAASC,oBAAoB,GAAGR,cAAc,CAACM,IAAI;QAC1D,MAAMG,kBAAkBV,MAAMC,cAAc,CAACU,GAAG,CAACJ;QACjD,IAAI,CAACG,iBAAiB;YACpB;QACF;QAEA,MAAME,WAAWd,qBAAqBU;QAEtC,MAAMK,YAAYH,gBAAgBC,GAAG,CAACC;QACtC,IAAI,CAACC,WAAW;YACd;QACF;QAEA,MAAMC,OAAOf,gBAAgBc,WAAWJ;QACxC,IAAIK,MAAM;YACR,OAAOA;QACT;IACF;IAEA,OAAOC;AACT"}

View File

@@ -0,0 +1,5 @@
export function getSegmentValue(segment) {
return Array.isArray(segment) ? segment[1] : segment;
}
//# sourceMappingURL=get-segment-value.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/get-segment-value.ts"],"names":["getSegmentValue","segment","Array","isArray"],"mappings":"AAEA,OAAO,SAASA,gBAAgBC,OAAgB;IAC9C,OAAOC,MAAMC,OAAO,CAACF,WAAWA,OAAO,CAAC,EAAE,GAAGA;AAC/C"}

View File

@@ -0,0 +1,237 @@
import { CacheStates } from "../../../../shared/lib/app-router-context.shared-runtime";
import { fetchServerResponse } from "../fetch-server-response";
import { createRecordFromThenable } from "../create-record-from-thenable";
import { readRecordValue } from "../read-record-value";
import { createHrefFromUrl } from "../create-href-from-url";
import { invalidateCacheBelowFlightSegmentPath } from "../invalidate-cache-below-flight-segmentpath";
import { fillCacheWithDataProperty } from "../fill-cache-with-data-property";
import { createOptimisticTree } from "../create-optimistic-tree";
import { applyRouterStatePatchToTree } from "../apply-router-state-patch-to-tree";
import { shouldHardNavigate } from "../should-hard-navigate";
import { isNavigatingToNewRootLayout } from "../is-navigating-to-new-root-layout";
import { PrefetchKind } from "../router-reducer-types";
import { handleMutable } from "../handle-mutable";
import { applyFlightData } from "../apply-flight-data";
import { PrefetchCacheEntryStatus, getPrefetchEntryCacheStatus } from "../get-prefetch-cache-entry-status";
import { prunePrefetchCache } from "./prune-prefetch-cache";
import { prefetchQueue } from "./prefetch-reducer";
export function handleExternalUrl(state, mutable, url, pendingPush) {
mutable.previousTree = state.tree;
mutable.mpaNavigation = true;
mutable.canonicalUrl = url;
mutable.pendingPush = pendingPush;
mutable.scrollableSegments = undefined;
return handleMutable(state, mutable);
}
function generateSegmentsFromPatch(flightRouterPatch) {
const segments = [];
const [segment, parallelRoutes] = flightRouterPatch;
if (Object.keys(parallelRoutes).length === 0) {
return [
[
segment
]
];
}
for (const [parallelRouteKey, parallelRoute] of Object.entries(parallelRoutes)){
for (const childSegment of generateSegmentsFromPatch(parallelRoute)){
// If the segment is empty, it means we are at the root of the tree
if (segment === "") {
segments.push([
parallelRouteKey,
...childSegment
]);
} else {
segments.push([
segment,
parallelRouteKey,
...childSegment
]);
}
}
}
return segments;
}
function addRefetchToLeafSegments(newCache, currentCache, flightSegmentPath, treePatch, data) {
let appliedPatch = false;
newCache.status = CacheStates.READY;
newCache.subTreeData = currentCache.subTreeData;
newCache.parallelRoutes = new Map(currentCache.parallelRoutes);
const segmentPathsToFill = generateSegmentsFromPatch(treePatch).map((segment)=>[
...flightSegmentPath,
...segment
]);
for (const segmentPaths of segmentPathsToFill){
const res = fillCacheWithDataProperty(newCache, currentCache, segmentPaths, data);
if (!(res == null ? void 0 : res.bailOptimistic)) {
appliedPatch = true;
}
}
return appliedPatch;
}
export function navigateReducer(state, action) {
const { url, isExternalUrl, navigateType, cache, mutable, forceOptimisticNavigation, shouldScroll } = action;
const { pathname, hash } = url;
const href = createHrefFromUrl(url);
const pendingPush = navigateType === "push";
// we want to prune the prefetch cache on every navigation to avoid it growing too large
prunePrefetchCache(state.prefetchCache);
const isForCurrentTree = JSON.stringify(mutable.previousTree) === JSON.stringify(state.tree);
if (isForCurrentTree) {
return handleMutable(state, mutable);
}
if (isExternalUrl) {
return handleExternalUrl(state, mutable, url.toString(), pendingPush);
}
let prefetchValues = state.prefetchCache.get(createHrefFromUrl(url, false));
if (forceOptimisticNavigation && (prefetchValues == null ? void 0 : prefetchValues.kind) !== PrefetchKind.TEMPORARY) {
const segments = pathname.split("/");
// TODO-APP: figure out something better for index pages
segments.push("__PAGE__");
// Optimistic tree case.
// If the optimistic tree is deeper than the current state leave that deeper part out of the fetch
const optimisticTree = createOptimisticTree(segments, state.tree, false);
// we need a copy of the cache in case we need to revert to it
const temporaryCacheNode = {
...cache
};
// Copy subTreeData for the root node of the cache.
// Note: didn't do it above because typescript doesn't like it.
temporaryCacheNode.status = CacheStates.READY;
temporaryCacheNode.subTreeData = state.cache.subTreeData;
temporaryCacheNode.parallelRoutes = new Map(state.cache.parallelRoutes);
let data;
const fetchResponse = ()=>{
if (!data) {
data = createRecordFromThenable(fetchServerResponse(url, optimisticTree, state.nextUrl, state.buildId));
}
return data;
};
// TODO-APP: segments.slice(1) strips '', we can get rid of '' altogether.
// TODO-APP: re-evaluate if we need to strip the last segment
const optimisticFlightSegmentPath = segments.slice(1).map((segment)=>[
"children",
segment
]).flat();
// Copy existing cache nodes as far as possible and fill in `data` property with the started data fetch.
// The `data` property is used to suspend in layout-router during render if it hasn't resolved yet by the time it renders.
const res = fillCacheWithDataProperty(temporaryCacheNode, state.cache, optimisticFlightSegmentPath, fetchResponse, true);
// If optimistic fetch couldn't happen it falls back to the non-optimistic case.
if (!(res == null ? void 0 : res.bailOptimistic)) {
mutable.previousTree = state.tree;
mutable.patchedTree = optimisticTree;
mutable.pendingPush = pendingPush;
mutable.hashFragment = hash;
mutable.shouldScroll = shouldScroll;
mutable.scrollableSegments = [];
mutable.cache = temporaryCacheNode;
mutable.canonicalUrl = href;
state.prefetchCache.set(createHrefFromUrl(url, false), {
data: createRecordFromThenable(Promise.resolve(data)),
// this will make sure that the entry will be discarded after 30s
kind: PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: state.tree,
lastUsedTime: Date.now()
});
return handleMutable(state, mutable);
}
}
// If we don't have a prefetch value, we need to create one
if (!prefetchValues) {
const data = createRecordFromThenable(fetchServerResponse(url, state.tree, state.nextUrl, state.buildId, // in dev, there's never gonna be a prefetch entry so we want to prefetch here
// in order to simulate the behavior of the prefetch cache
process.env.NODE_ENV === "development" ? PrefetchKind.AUTO : undefined));
const newPrefetchValue = {
data: createRecordFromThenable(Promise.resolve(data)),
// this will make sure that the entry will be discarded after 30s
kind: process.env.NODE_ENV === "development" ? PrefetchKind.AUTO : PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: state.tree,
lastUsedTime: null
};
state.prefetchCache.set(createHrefFromUrl(url, false), newPrefetchValue);
prefetchValues = newPrefetchValue;
}
const prefetchEntryCacheStatus = getPrefetchEntryCacheStatus(prefetchValues);
// The one before last item is the router state tree patch
const { treeAtTimeOfPrefetch, data } = prefetchValues;
prefetchQueue.bump(data);
// Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves.
const [flightData, canonicalUrlOverride] = readRecordValue(data);
// we only want to mark this once
if (!prefetchValues.lastUsedTime) {
// important: we should only mark the cache node as dirty after we unsuspend from the call above
prefetchValues.lastUsedTime = Date.now();
}
// Handle case when navigating to page in `pages` from `app`
if (typeof flightData === "string") {
return handleExternalUrl(state, mutable, flightData, pendingPush);
}
let currentTree = state.tree;
let currentCache = state.cache;
let scrollableSegments = [];
for (const flightDataPath of flightData){
const flightSegmentPath = flightDataPath.slice(0, -4);
// The one before last item is the router state tree patch
const treePatch = flightDataPath.slice(-3)[0];
// TODO-APP: remove ''
const flightSegmentPathWithLeadingEmpty = [
"",
...flightSegmentPath
];
// Create new tree based on the flightSegmentPath and router state patch
let newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
flightSegmentPathWithLeadingEmpty, currentTree, treePatch);
// If the tree patch can't be applied to the current tree then we use the tree at time of prefetch
// TODO-APP: This should instead fill in the missing pieces in `currentTree` with the data from `treeAtTimeOfPrefetch`, then apply the patch.
if (newTree === null) {
newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
flightSegmentPathWithLeadingEmpty, treeAtTimeOfPrefetch, treePatch);
}
if (newTree !== null) {
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush);
}
let applied = applyFlightData(currentCache, cache, flightDataPath, prefetchValues.kind === "auto" && prefetchEntryCacheStatus === PrefetchCacheEntryStatus.reusable);
if (!applied && prefetchEntryCacheStatus === PrefetchCacheEntryStatus.stale) {
applied = addRefetchToLeafSegments(cache, currentCache, flightSegmentPath, treePatch, // eslint-disable-next-line no-loop-func
()=>fetchServerResponse(url, currentTree, state.nextUrl, state.buildId));
}
const hardNavigate = shouldHardNavigate(// TODO-APP: remove ''
flightSegmentPathWithLeadingEmpty, currentTree);
if (hardNavigate) {
cache.status = CacheStates.READY;
// Copy subTreeData for the root node of the cache.
cache.subTreeData = currentCache.subTreeData;
invalidateCacheBelowFlightSegmentPath(cache, currentCache, flightSegmentPath);
// Ensure the existing cache value is used when the cache was not invalidated.
mutable.cache = cache;
} else if (applied) {
mutable.cache = cache;
}
currentCache = cache;
currentTree = newTree;
for (const subSegment of generateSegmentsFromPatch(treePatch)){
const scrollableSegmentPath = [
...flightSegmentPath,
...subSegment
];
// Filter out the __DEFAULT__ paths as they shouldn't be scrolled to in this case.
if (scrollableSegmentPath[scrollableSegmentPath.length - 1] !== "__DEFAULT__") {
scrollableSegments.push(scrollableSegmentPath);
}
}
}
}
mutable.previousTree = state.tree;
mutable.patchedTree = currentTree;
mutable.canonicalUrl = canonicalUrlOverride ? createHrefFromUrl(canonicalUrlOverride) : href;
mutable.pendingPush = pendingPush;
mutable.scrollableSegments = scrollableSegments;
mutable.hashFragment = hash;
mutable.shouldScroll = shouldScroll;
return handleMutable(state, mutable);
}
//# sourceMappingURL=navigate-reducer.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,49 @@
import { createHrefFromUrl } from "../create-href-from-url";
import { fetchServerResponse } from "../fetch-server-response";
import { PrefetchKind } from "../router-reducer-types";
import { createRecordFromThenable } from "../create-record-from-thenable";
import { prunePrefetchCache } from "./prune-prefetch-cache";
import { NEXT_RSC_UNION_QUERY } from "../../app-router-headers";
import { PromiseQueue } from "../../promise-queue";
export const prefetchQueue = new PromiseQueue(5);
export function prefetchReducer(state, action) {
// let's prune the prefetch cache before we do anything else
prunePrefetchCache(state.prefetchCache);
const { url } = action;
url.searchParams.delete(NEXT_RSC_UNION_QUERY);
const href = createHrefFromUrl(url, // Ensures the hash is not part of the cache key as it does not affect fetching the server
false);
const cacheEntry = state.prefetchCache.get(href);
if (cacheEntry) {
/**
* If the cache entry present was marked as temporary, it means that we prefetched it from the navigate reducer,
* where we didn't have the prefetch intent. We want to update it to the new, more accurate, kind here.
*/ if (cacheEntry.kind === PrefetchKind.TEMPORARY) {
state.prefetchCache.set(href, {
...cacheEntry,
kind: action.kind
});
}
/**
* if the prefetch action was a full prefetch and that the current cache entry wasn't one, we want to re-prefetch,
* otherwise we can re-use the current cache entry
**/ if (!(cacheEntry.kind === PrefetchKind.AUTO && action.kind === PrefetchKind.FULL)) {
return state;
}
}
// fetchServerResponse is intentionally not awaited so that it can be unwrapped in the navigate-reducer
const serverResponse = createRecordFromThenable(prefetchQueue.enqueue(()=>fetchServerResponse(url, // initialTree is used when history.state.tree is missing because the history state is set in `useEffect` below, it being missing means this is the hydration case.
state.tree, state.nextUrl, state.buildId, action.kind)));
// Create new tree based on the flightSegmentPath and router state patch
state.prefetchCache.set(href, {
// Create new tree based on the flightSegmentPath and router state patch
treeAtTimeOfPrefetch: state.tree,
data: serverResponse,
kind: action.kind,
prefetchTime: Date.now(),
lastUsedTime: null
});
return state;
}
//# sourceMappingURL=prefetch-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/prefetch-reducer.ts"],"names":["createHrefFromUrl","fetchServerResponse","PrefetchKind","createRecordFromThenable","prunePrefetchCache","NEXT_RSC_UNION_QUERY","PromiseQueue","prefetchQueue","prefetchReducer","state","action","prefetchCache","url","searchParams","delete","href","cacheEntry","get","kind","TEMPORARY","set","AUTO","FULL","serverResponse","enqueue","tree","nextUrl","buildId","treeAtTimeOfPrefetch","data","prefetchTime","Date","now","lastUsedTime"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,0BAAyB;AAC3D,SAASC,mBAAmB,QAAQ,2BAA0B;AAC9D,SAIEC,YAAY,QACP,0BAAyB;AAChC,SAASC,wBAAwB,QAAQ,iCAAgC;AACzE,SAASC,kBAAkB,QAAQ,yBAAwB;AAC3D,SAASC,oBAAoB,QAAQ,2BAA0B;AAC/D,SAASC,YAAY,QAAQ,sBAAqB;AAElD,OAAO,MAAMC,gBAAgB,IAAID,aAAa,GAAE;AAEhD,OAAO,SAASE,gBACdC,KAA2B,EAC3BC,MAAsB;IAEtB,4DAA4D;IAC5DN,mBAAmBK,MAAME,aAAa;IAEtC,MAAM,EAAEC,GAAG,EAAE,GAAGF;IAChBE,IAAIC,YAAY,CAACC,MAAM,CAACT;IAExB,MAAMU,OAAOf,kBACXY,KACA,0FAA0F;IAC1F;IAGF,MAAMI,aAAaP,MAAME,aAAa,CAACM,GAAG,CAACF;IAC3C,IAAIC,YAAY;QACd;;;KAGC,GACD,IAAIA,WAAWE,IAAI,KAAKhB,aAAaiB,SAAS,EAAE;YAC9CV,MAAME,aAAa,CAACS,GAAG,CAACL,MAAM;gBAC5B,GAAGC,UAAU;gBACbE,MAAMR,OAAOQ,IAAI;YACnB;QACF;QAEA;;;MAGE,GACF,IACE,CACEF,CAAAA,WAAWE,IAAI,KAAKhB,aAAamB,IAAI,IACrCX,OAAOQ,IAAI,KAAKhB,aAAaoB,IAAI,AAAD,GAElC;YACA,OAAOb;QACT;IACF;IAEA,uGAAuG;IACvG,MAAMc,iBAAiBpB,yBACrBI,cAAciB,OAAO,CAAC,IACpBvB,oBACEW,KACA,mKAAmK;QACnKH,MAAMgB,IAAI,EACVhB,MAAMiB,OAAO,EACbjB,MAAMkB,OAAO,EACbjB,OAAOQ,IAAI;IAKjB,wEAAwE;IACxET,MAAME,aAAa,CAACS,GAAG,CAACL,MAAM;QAC5B,wEAAwE;QACxEa,sBAAsBnB,MAAMgB,IAAI;QAChCI,MAAMN;QACNL,MAAMR,OAAOQ,IAAI;QACjBY,cAAcC,KAAKC,GAAG;QACtBC,cAAc;IAChB;IAEA,OAAOxB;AACT"}

View File

@@ -0,0 +1,10 @@
import { PrefetchCacheEntryStatus, getPrefetchEntryCacheStatus } from "../get-prefetch-cache-entry-status";
export function prunePrefetchCache(prefetchCache) {
for (const [href, prefetchCacheEntry] of prefetchCache){
if (getPrefetchEntryCacheStatus(prefetchCacheEntry) === PrefetchCacheEntryStatus.expired) {
prefetchCache.delete(href);
}
}
}
//# sourceMappingURL=prune-prefetch-cache.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/prune-prefetch-cache.ts"],"names":["PrefetchCacheEntryStatus","getPrefetchEntryCacheStatus","prunePrefetchCache","prefetchCache","href","prefetchCacheEntry","expired","delete"],"mappings":"AACA,SACEA,wBAAwB,EACxBC,2BAA2B,QACtB,qCAAoC;AAE3C,OAAO,SAASC,mBACdC,aAA4C;IAE5C,KAAK,MAAM,CAACC,MAAMC,mBAAmB,IAAIF,cAAe;QACtD,IACEF,4BAA4BI,wBAC5BL,yBAAyBM,OAAO,EAChC;YACAH,cAAcI,MAAM,CAACH;QACvB;IACF;AACF"}

View File

@@ -0,0 +1,78 @@
import { fetchServerResponse } from "../fetch-server-response";
import { createRecordFromThenable } from "../create-record-from-thenable";
import { readRecordValue } from "../read-record-value";
import { createHrefFromUrl } from "../create-href-from-url";
import { applyRouterStatePatchToTree } from "../apply-router-state-patch-to-tree";
import { isNavigatingToNewRootLayout } from "../is-navigating-to-new-root-layout";
import { handleExternalUrl } from "./navigate-reducer";
import { handleMutable } from "../handle-mutable";
import { CacheStates } from "../../../../shared/lib/app-router-context.shared-runtime";
import { fillLazyItemsTillLeafWithHead } from "../fill-lazy-items-till-leaf-with-head";
export function refreshReducer(state, action) {
const { cache, mutable, origin } = action;
const href = state.canonicalUrl;
let currentTree = state.tree;
const isForCurrentTree = JSON.stringify(mutable.previousTree) === JSON.stringify(currentTree);
if (isForCurrentTree) {
return handleMutable(state, mutable);
}
if (!cache.data) {
// TODO-APP: verify that `href` is not an external url.
// Fetch data from the root of the tree.
cache.data = createRecordFromThenable(fetchServerResponse(new URL(href, origin), [
currentTree[0],
currentTree[1],
currentTree[2],
"refetch"
], state.nextUrl, state.buildId));
}
const [flightData, canonicalUrlOverride] = readRecordValue(cache.data);
// Handle case when navigating to page in `pages` from `app`
if (typeof flightData === "string") {
return handleExternalUrl(state, mutable, flightData, state.pushRef.pendingPush);
}
// Remove cache.data as it has been resolved at this point.
cache.data = null;
for (const flightDataPath of flightData){
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
console.log("REFRESH FAILED");
return state;
}
// Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath;
const newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
[
""
], currentTree, treePatch);
if (newTree === null) {
throw new Error("SEGMENT MISMATCH");
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush);
}
const canonicalUrlOverrideHref = canonicalUrlOverride ? createHrefFromUrl(canonicalUrlOverride) : undefined;
if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref;
}
// The one before last item is the router state tree patch
const [subTreeData, head] = flightDataPath.slice(-2);
// Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) {
cache.status = CacheStates.READY;
cache.subTreeData = subTreeData;
fillLazyItemsTillLeafWithHead(cache, // Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined, treePatch, head);
mutable.cache = cache;
mutable.prefetchCache = new Map();
}
mutable.previousTree = currentTree;
mutable.patchedTree = newTree;
mutable.canonicalUrl = href;
currentTree = newTree;
}
return handleMutable(state, mutable);
}
//# sourceMappingURL=refresh-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/refresh-reducer.ts"],"names":["fetchServerResponse","createRecordFromThenable","readRecordValue","createHrefFromUrl","applyRouterStatePatchToTree","isNavigatingToNewRootLayout","handleExternalUrl","handleMutable","CacheStates","fillLazyItemsTillLeafWithHead","refreshReducer","state","action","cache","mutable","origin","href","canonicalUrl","currentTree","tree","isForCurrentTree","JSON","stringify","previousTree","data","URL","nextUrl","buildId","flightData","canonicalUrlOverride","pushRef","pendingPush","flightDataPath","length","console","log","treePatch","newTree","Error","canonicalUrlOverrideHref","undefined","subTreeData","head","slice","status","READY","prefetchCache","Map","patchedTree"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,2BAA0B;AAC9D,SAASC,wBAAwB,QAAQ,iCAAgC;AACzE,SAASC,eAAe,QAAQ,uBAAsB;AACtD,SAASC,iBAAiB,QAAQ,0BAAyB;AAC3D,SAASC,2BAA2B,QAAQ,sCAAqC;AACjF,SAASC,2BAA2B,QAAQ,sCAAqC;AAMjF,SAASC,iBAAiB,QAA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pB,MAAM,IAAIC,MAAM;QAClB;QAEA,IAAIjC,4BAA4Ba,aAAamB,UAAU;YACrD,OAAO/B,kBAAkBK,OAAOG,SAASE,MAAML,MAAMmB,OAAO,CAACC,WAAW;QAC1E;QAEA,MAAMQ,2BAA2BV,uBAC7B1B,kBAAkB0B,wBAClBW;QAEJ,IAAIX,sBAAsB;YACxBf,QAAQG,YAAY,GAAGsB;QACzB;QAEA,0DAA0D;QAC1D,MAAM,CAACE,aAAaC,KAAK,GAAGV,eAAeW,KAAK,CAAC,CAAC;QAElD,8FAA8F;QAC9F,IAAIF,gBAAgB,MAAM;YACxB5B,MAAM+B,MAAM,GAAGpC,YAAYqC,KAAK;YAChChC,MAAM4B,WAAW,GAAGA;YACpBhC,8BACEI,OACA,4FAA4F;YAC5F2B,WACAJ,WACAM;YAEF5B,QAAQD,KAAK,GAAGA;YAChBC,QAAQgC,aAAa,GAAG,IAAIC;QAC9B;QAEAjC,QAAQS,YAAY,GAAGL;QACvBJ,QAAQkC,WAAW,GAAGX;QACtBvB,QAAQG,YAAY,GAAGD;QAEvBE,cAAcmB;IAChB;IAEA,OAAO9B,cAAcI,OAAOG;AAC9B"}

View File

@@ -0,0 +1,19 @@
import { createHrefFromUrl } from "../create-href-from-url";
export function restoreReducer(state, action) {
const { url, tree } = action;
const href = createHrefFromUrl(url);
return {
buildId: state.buildId,
// Set canonical url
canonicalUrl: href,
pushRef: state.pushRef,
focusAndScrollRef: state.focusAndScrollRef,
cache: state.cache,
prefetchCache: state.prefetchCache,
// Restore provided tree
tree: tree,
nextUrl: url.pathname
};
}
//# sourceMappingURL=restore-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/restore-reducer.ts"],"names":["createHrefFromUrl","restoreReducer","state","action","url","tree","href","buildId","canonicalUrl","pushRef","focusAndScrollRef","cache","prefetchCache","nextUrl","pathname"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,0BAAyB;AAO3D,OAAO,SAASC,eACdC,KAA2B,EAC3BC,MAAqB;IAErB,MAAM,EAAEC,GAAG,EAAEC,IAAI,EAAE,GAAGF;IACtB,MAAMG,OAAON,kBAAkBI;IAE/B,OAAO;QACLG,SAASL,MAAMK,OAAO;QACtB,oBAAoB;QACpBC,cAAcF;QACdG,SAASP,MAAMO,OAAO;QACtBC,mBAAmBR,MAAMQ,iBAAiB;QAC1CC,OAAOT,MAAMS,KAAK;QAClBC,eAAeV,MAAMU,aAAa;QAClC,wBAAwB;QACxBP,MAAMA;QACNQ,SAAST,IAAIU,QAAQ;IACvB;AACF"}

View File

@@ -0,0 +1,196 @@
import { callServer } from "../../../app-call-server";
import { ACTION, NEXT_ROUTER_STATE_TREE, NEXT_URL, RSC_CONTENT_TYPE_HEADER } from "../../app-router-headers";
import { createRecordFromThenable } from "../create-record-from-thenable";
import { readRecordValue } from "../read-record-value";
// // eslint-disable-next-line import/no-extraneous-dependencies
// import { createFromFetch } from 'react-server-dom-webpack/client'
// // eslint-disable-next-line import/no-extraneous-dependencies
// import { encodeReply } from 'react-server-dom-webpack/client'
const { createFromFetch, encodeReply } = !!process.env.NEXT_RUNTIME ? require("react-server-dom-webpack/client.edge") : require("react-server-dom-webpack/client");
import { addBasePath } from "../../../add-base-path";
import { createHrefFromUrl } from "../create-href-from-url";
import { handleExternalUrl } from "./navigate-reducer";
import { applyRouterStatePatchToTree } from "../apply-router-state-patch-to-tree";
import { isNavigatingToNewRootLayout } from "../is-navigating-to-new-root-layout";
import { CacheStates } from "../../../../shared/lib/app-router-context.shared-runtime";
import { handleMutable } from "../handle-mutable";
import { fillLazyItemsTillLeafWithHead } from "../fill-lazy-items-till-leaf-with-head";
async function fetchServerAction(state, param) {
let { actionId, actionArgs } = param;
const body = await encodeReply(actionArgs);
const res = await fetch("", {
method: "POST",
headers: {
Accept: RSC_CONTENT_TYPE_HEADER,
[ACTION]: actionId,
[NEXT_ROUTER_STATE_TREE]: encodeURIComponent(JSON.stringify(state.tree)),
...process.env.__NEXT_ACTIONS_DEPLOYMENT_ID && process.env.NEXT_DEPLOYMENT_ID ? {
"x-deployment-id": process.env.NEXT_DEPLOYMENT_ID
} : {},
...state.nextUrl ? {
[NEXT_URL]: state.nextUrl
} : {}
},
body
});
const location = res.headers.get("x-action-redirect");
let revalidatedParts;
try {
const revalidatedHeader = JSON.parse(res.headers.get("x-action-revalidated") || "[[],0,0]");
revalidatedParts = {
paths: revalidatedHeader[0] || [],
tag: !!revalidatedHeader[1],
cookie: revalidatedHeader[2]
};
} catch (e) {
revalidatedParts = {
paths: [],
tag: false,
cookie: false
};
}
const redirectLocation = location ? new URL(addBasePath(location), // Ensure relative redirects in Server Actions work, e.g. redirect('./somewhere-else')
new URL(state.canonicalUrl, window.location.href)) : undefined;
let isFlightResponse = res.headers.get("content-type") === RSC_CONTENT_TYPE_HEADER;
if (isFlightResponse) {
const response = await createFromFetch(Promise.resolve(res), {
callServer
});
if (location) {
// if it was a redirection, then result is just a regular RSC payload
const [, actionFlightData] = response != null ? response : [];
return {
actionFlightData: actionFlightData,
redirectLocation,
revalidatedParts
};
}
// otherwise it's a tuple of [actionResult, actionFlightData]
const [actionResult, [, actionFlightData]] = response != null ? response : [];
return {
actionResult,
actionFlightData,
redirectLocation,
revalidatedParts
};
}
return {
redirectLocation,
revalidatedParts
};
}
/*
* This reducer is responsible for calling the server action and processing any side-effects from the server action.
* It does not mutate the state by itself but rather delegates to other reducers to do the actual mutation.
*/ export function serverActionReducer(state, action) {
const { mutable, cache, resolve, reject } = action;
const href = state.canonicalUrl;
let currentTree = state.tree;
const isForCurrentTree = JSON.stringify(mutable.previousTree) === JSON.stringify(currentTree);
if (isForCurrentTree) {
return handleMutable(state, mutable);
}
if (mutable.inFlightServerAction) {
// unblock if a navigation event comes through
// while we've suspended on an action
if (mutable.globalMutable.pendingNavigatePath && mutable.globalMutable.pendingNavigatePath !== href) {
mutable.inFlightServerAction.then(()=>{
if (mutable.actionResultResolved) return;
// if the server action resolves after a navigation took place,
// reset ServerActionMutable values & trigger a refresh so that any stale data gets updated
mutable.inFlightServerAction = null;
mutable.globalMutable.pendingNavigatePath = undefined;
mutable.globalMutable.refresh();
mutable.actionResultResolved = true;
});
return state;
}
} else {
mutable.inFlightServerAction = createRecordFromThenable(fetchServerAction(state, action));
}
// TODO-APP: Make try/catch wrap only readRecordValue so that other errors bubble up through the reducer instead.
try {
// suspends until the server action is resolved.
const { actionResult, actionFlightData: flightData, redirectLocation } = readRecordValue(mutable.inFlightServerAction);
// Make sure the redirection is a push instead of a replace.
// Issue: https://github.com/vercel/next.js/issues/53911
if (redirectLocation) {
state.pushRef.pendingPush = true;
mutable.pendingPush = true;
}
mutable.previousTree = state.tree;
if (!flightData) {
if (!mutable.actionResultResolved) {
resolve(actionResult);
mutable.actionResultResolved = true;
}
// If there is a redirect but no flight data we need to do a mpaNavigation.
if (redirectLocation) {
return handleExternalUrl(state, mutable, redirectLocation.href, state.pushRef.pendingPush);
}
return state;
}
if (typeof flightData === "string") {
// Handle case when navigating to page in `pages` from `app`
return handleExternalUrl(state, mutable, flightData, state.pushRef.pendingPush);
}
// Remove cache.data as it has been resolved at this point.
mutable.inFlightServerAction = null;
for (const flightDataPath of flightData){
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
console.log("SERVER ACTION APPLY FAILED");
return state;
}
// Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath;
const newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
[
""
], currentTree, treePatch);
if (newTree === null) {
throw new Error("SEGMENT MISMATCH");
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush);
}
// The one before last item is the router state tree patch
const [subTreeData, head] = flightDataPath.slice(-2);
// Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) {
cache.status = CacheStates.READY;
cache.subTreeData = subTreeData;
fillLazyItemsTillLeafWithHead(cache, // Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined, treePatch, head);
mutable.cache = cache;
mutable.prefetchCache = new Map();
}
mutable.previousTree = currentTree;
mutable.patchedTree = newTree;
mutable.canonicalUrl = href;
currentTree = newTree;
}
if (redirectLocation) {
const newHref = createHrefFromUrl(redirectLocation, false);
mutable.canonicalUrl = newHref;
}
if (!mutable.actionResultResolved) {
resolve(actionResult);
mutable.actionResultResolved = true;
}
return handleMutable(state, mutable);
} catch (e) {
if (e.status === "rejected") {
if (!mutable.actionResultResolved) {
reject(e.value);
mutable.actionResultResolved = true;
}
// When the server action is rejected we don't update the state and instead call the reject handler of the promise.
return state;
}
throw e;
}
}
//# sourceMappingURL=server-action-reducer.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,56 @@
import { createHrefFromUrl } from "../create-href-from-url";
import { applyRouterStatePatchToTree } from "../apply-router-state-patch-to-tree";
import { isNavigatingToNewRootLayout } from "../is-navigating-to-new-root-layout";
import { handleExternalUrl } from "./navigate-reducer";
import { applyFlightData } from "../apply-flight-data";
import { handleMutable } from "../handle-mutable";
export function serverPatchReducer(state, action) {
const { flightData, previousTree, overrideCanonicalUrl, cache, mutable } = action;
const isForCurrentTree = JSON.stringify(previousTree) === JSON.stringify(state.tree);
// When a fetch is slow to resolve it could be that you navigated away while the request was happening or before the reducer runs.
// In that case opt-out of applying the patch given that the data could be stale.
if (!isForCurrentTree) {
// TODO-APP: Handle tree mismatch
console.log("TREE MISMATCH");
// Keep everything as-is.
return state;
}
if (mutable.previousTree) {
return handleMutable(state, mutable);
}
// Handle case when navigating to page in `pages` from `app`
if (typeof flightData === "string") {
return handleExternalUrl(state, mutable, flightData, state.pushRef.pendingPush);
}
let currentTree = state.tree;
let currentCache = state.cache;
for (const flightDataPath of flightData){
// Slices off the last segment (which is at -4) as it doesn't exist in the tree yet
const flightSegmentPath = flightDataPath.slice(0, -4);
const [treePatch] = flightDataPath.slice(-3, -2);
const newTree = applyRouterStatePatchToTree(// TODO-APP: remove ''
[
"",
...flightSegmentPath
], currentTree, treePatch);
if (newTree === null) {
throw new Error("SEGMENT MISMATCH");
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, state.canonicalUrl, state.pushRef.pendingPush);
}
const canonicalUrlOverrideHref = overrideCanonicalUrl ? createHrefFromUrl(overrideCanonicalUrl) : undefined;
if (canonicalUrlOverrideHref) {
mutable.canonicalUrl = canonicalUrlOverrideHref;
}
applyFlightData(currentCache, cache, flightDataPath);
mutable.previousTree = currentTree;
mutable.patchedTree = newTree;
mutable.cache = cache;
currentCache = cache;
currentTree = newTree;
}
return handleMutable(state, mutable);
}
//# sourceMappingURL=server-patch-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/client/components/router-reducer/reducers/server-patch-reducer.ts"],"names":["createHrefFromUrl","applyRouterStatePatchToTree","isNavigatingToNewRootLayout","handleExternalUrl","applyFlightData","handleMutable","serverPatchReducer","state","action","flightData","previousTree","overrideCanonicalUrl","cache","mutable","isForCurrentTree","JSON","stringify","tree","console","log","pushRef","pendingPush","currentTree","currentCache","flightDataPath","flightSegmentPath","slice","treePatch","newTree","Error","canonicalUrl","canonicalUrlOverrideHref","undefined","patchedTree"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,0BAAyB;AAC3D,SAASC,2BAA2B,QAAQ,sCAAqC;AACjF,SAASC,2BAA2B,QAAQ,sCAAqC;AAMjF,SAASC,iBAAiB,QAAQ,qBAAoB;AACtD,SAASC,eAAe,QAAQ,uBAAsB;AACtD,SAASC,aAAa,QAAQ,oBAAmB;AAEjD,OAAO,SAASC,mBACdC,KAA2B,EAC3BC,MAAyB;IAEzB,MAAM,EAAEC,UAAU,EAAEC,YAAY,EAAEC,oBAAoB,EAAEC,KAAK,EAAEC,OAAO,EAAE,GACtEL;IAEF,MAAMM,mBACJC,KAAKC,SAAS,CAACN,kBAAkBK,KAAKC,SAAS,CAACT,MAAMU,IAAI;IAE5D,kIAAkI;IAClI,iFAAiF;IACjF,IAAI,CAACH,kBAAkB;QACrB,iCAAiC;QACjCI,QAAQC,GAAG,CAAC;QACZ,yBAAyB;QACzB,OAAOZ;IACT;IAEA,IAAIM,QAAQH,YAAY,EAAE;QACxB,OAAOL,cAAcE,OAAOM;IAC9B;IAEA,4DAA4D;IAC5D,IAAI,OAAOJ,eAAe,UAAU;QAClC,OAAON,kBACLI,OACAM,SACAJ,YACAF,MAAMa,OAAO,CAACC,WAAW;IAE7B;IAEA,IAAIC,cAAcf,MAAMU,IAAI;IAC5B,IAAIM,eAAehB,MAAMK,KAAK;IAE9B,KAAK,MAAMY,kBAAkBf,WAAY;QACvC,mFAAmF;QACnF,MAAMgB,oBAAoBD,eAAeE,KAAK,CAAC,GAAG,CAAC;QAEnD,MAAM,CAACC,UAAU,GAAGH,eAAeE,KAAK,CAAC,CAAC,GAAG,CAAC;QAC9C,MAAME,UAAU3B,4BACd,sBAAsB;QACtB;YAAC;eAAOwB;SAAkB,EAC1BH,aACAK;QAGF,IAAIC,YAAY,MAAM;YACpB,MAAM,IAAIC,MAAM;QAClB;QAEA,IAAI3B,4BAA4BoB,aAAaM,UAAU;YACrD,OAAOzB,kBACLI,OACAM,SACAN,MAAMuB,YAAY,EAClBvB,MAAMa,OAAO,CAACC,WAAW;QAE7B;QAEA,MAAMU,2BAA2BpB,uBAC7BX,kBAAkBW,wBAClBqB;QAEJ,IAAID,0BAA0B;YAC5BlB,QAAQiB,YAAY,GAAGC;QACzB;QAEA3B,gBAAgBmB,cAAcX,OAAOY;QAErCX,QAAQH,YAAY,GAAGY;QACvBT,QAAQoB,WAAW,GAAGL;QACtBf,QAAQD,KAAK,GAAGA;QAEhBW,eAAeX;QACfU,cAAcM;IAChB;IAEA,OAAOvB,cAAcE,OAAOM;AAC9B"}

View File

@@ -0,0 +1,15 @@
export const ACTION_REFRESH = "refresh";
export const ACTION_NAVIGATE = "navigate";
export const ACTION_RESTORE = "restore";
export const ACTION_SERVER_PATCH = "server-patch";
export const ACTION_PREFETCH = "prefetch";
export const ACTION_FAST_REFRESH = "fast-refresh";
export const ACTION_SERVER_ACTION = "server-action";
export var PrefetchKind;
(function(PrefetchKind) {
PrefetchKind["AUTO"] = "auto";
PrefetchKind["FULL"] = "full";
PrefetchKind["TEMPORARY"] = "temporary";
})(PrefetchKind || (PrefetchKind = {}));
//# sourceMappingURL=router-reducer-types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/router-reducer-types.ts"],"names":["ACTION_REFRESH","ACTION_NAVIGATE","ACTION_RESTORE","ACTION_SERVER_PATCH","ACTION_PREFETCH","ACTION_FAST_REFRESH","ACTION_SERVER_ACTION","PrefetchKind","AUTO","FULL","TEMPORARY"],"mappings":"AAQA,OAAO,MAAMA,iBAAiB,UAAS;AACvC,OAAO,MAAMC,kBAAkB,WAAU;AACzC,OAAO,MAAMC,iBAAiB,UAAS;AACvC,OAAO,MAAMC,sBAAsB,eAAc;AACjD,OAAO,MAAMC,kBAAkB,WAAU;AACzC,OAAO,MAAMC,sBAAsB,eAAc;AACjD,OAAO,MAAMC,uBAAuB,gBAAe;WA2J5C;UAAKC,YAAY;IAAZA,aACVC,UAAO;IADGD,aAEVE,UAAO;IAFGF,aAGVG,eAAY;GAHFH,iBAAAA"}

View File

@@ -0,0 +1,52 @@
import { ACTION_NAVIGATE, ACTION_SERVER_PATCH, ACTION_RESTORE, ACTION_REFRESH, ACTION_PREFETCH, ACTION_FAST_REFRESH, ACTION_SERVER_ACTION } from "./router-reducer-types";
import { navigateReducer } from "./reducers/navigate-reducer";
import { serverPatchReducer } from "./reducers/server-patch-reducer";
import { restoreReducer } from "./reducers/restore-reducer";
import { refreshReducer } from "./reducers/refresh-reducer";
import { prefetchReducer } from "./reducers/prefetch-reducer";
import { fastRefreshReducer } from "./reducers/fast-refresh-reducer";
import { serverActionReducer } from "./reducers/server-action-reducer";
/**
* Reducer that handles the app-router state updates.
*/ function clientReducer(state, action) {
switch(action.type){
case ACTION_NAVIGATE:
{
return navigateReducer(state, action);
}
case ACTION_SERVER_PATCH:
{
return serverPatchReducer(state, action);
}
case ACTION_RESTORE:
{
return restoreReducer(state, action);
}
case ACTION_REFRESH:
{
return refreshReducer(state, action);
}
case ACTION_FAST_REFRESH:
{
return fastRefreshReducer(state, action);
}
case ACTION_PREFETCH:
{
return prefetchReducer(state, action);
}
case ACTION_SERVER_ACTION:
{
return serverActionReducer(state, action);
}
// This case should never be hit as dispatch is strongly typed.
default:
throw new Error("Unknown action");
}
}
function serverReducer(state, _action) {
return state;
}
// we don't run the client reducer on the server, so we use a noop function for better tree shaking
export const reducer = typeof window === "undefined" ? serverReducer : clientReducer;
//# sourceMappingURL=router-reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/router-reducer.ts"],"names":["ACTION_NAVIGATE","ACTION_SERVER_PATCH","ACTION_RESTORE","ACTION_REFRESH","ACTION_PREFETCH","ACTION_FAST_REFRESH","ACTION_SERVER_ACTION","navigateReducer","serverPatchReducer","restoreReducer","refreshReducer","prefetchReducer","fastRefreshReducer","serverActionReducer","clientReducer","state","action","type","Error","serverReducer","_action","reducer","window"],"mappings":"AAAA,SACEA,eAAe,EACfC,mBAAmB,EACnBC,cAAc,EACdC,cAAc,EACdC,eAAe,EAIfC,mBAAmB,EACnBC,oBAAoB,QACf,yBAAwB;AAC/B,SAASC,eAAe,QAAQ,8BAA6B;AAC7D,SAASC,kBAAkB,QAAQ,kCAAiC;AACpE,SAASC,cAAc,QAAQ,6BAA4B;AAC3D,SAASC,cAAc,QAAQ,6BAA4B;AAC3D,SAASC,eAAe,QAAQ,8BAA6B;AAC7D,SAASC,kBAAkB,QAAQ,kCAAiC;AACpE,SAASC,mBAAmB,QAAQ,mCAAkC;AAEtE;;CAEC,GACD,SAASC,cACPC,KAA2B,EAC3BC,MAAsB;IAEtB,OAAQA,OAAOC,IAAI;QACjB,KAAKjB;YAAiB;gBACpB,OAAOO,gBAAgBQ,OAAOC;YAChC;QACA,KAAKf;YAAqB;gBACxB,OAAOO,mBAAmBO,OAAOC;YACnC;QACA,KAAKd;YAAgB;gBACnB,OAAOO,eAAeM,OAAOC;YAC/B;QACA,KAAKb;YAAgB;gBACnB,OAAOO,eAAeK,OAAOC;YAC/B;QACA,KAAKX;YAAqB;gBACxB,OAAOO,mBAAmBG,OAAOC;YACnC;QACA,KAAKZ;YAAiB;gBACpB,OAAOO,gBAAgBI,OAAOC;YAChC;QACA,KAAKV;YAAsB;gBACzB,OAAOO,oBAAoBE,OAAOC;YACpC;QACA,+DAA+D;QAC/D;YACE,MAAM,IAAIE,MAAM;IACpB;AACF;AAEA,SAASC,cACPJ,KAA2B,EAC3BK,OAAuB;IAEvB,OAAOL;AACT;AAEA,mGAAmG;AACnG,OAAO,MAAMM,UACX,OAAOC,WAAW,cAAcH,gBAAgBL,cAAa"}

View File

@@ -0,0 +1,23 @@
import { matchSegment } from "../match-segments";
// TODO-APP: flightSegmentPath will be empty in case of static response, needs to be handled.
export function shouldHardNavigate(flightSegmentPath, flightRouterState) {
const [segment, parallelRoutes] = flightRouterState;
// TODO-APP: Check if `as` can be replaced.
const [currentSegment, parallelRouteKey] = flightSegmentPath;
// Check if current segment matches the existing segment.
if (!matchSegment(currentSegment, segment)) {
// If dynamic parameter in tree doesn't match up with segment path a hard navigation is triggered.
if (Array.isArray(currentSegment)) {
return true;
}
// If the existing segment did not match soft navigation is triggered.
return false;
}
const lastSegment = flightSegmentPath.length <= 2;
if (lastSegment) {
return false;
}
return shouldHardNavigate(flightSegmentPath.slice(2), parallelRoutes[parallelRouteKey]);
}
//# sourceMappingURL=should-hard-navigate.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/router-reducer/should-hard-navigate.ts"],"names":["matchSegment","shouldHardNavigate","flightSegmentPath","flightRouterState","segment","parallelRoutes","currentSegment","parallelRouteKey","Array","isArray","lastSegment","length","slice"],"mappings":"AAKA,SAASA,YAAY,QAAQ,oBAAmB;AAEhD,6FAA6F;AAC7F,OAAO,SAASC,mBACdC,iBAAiC,EACjCC,iBAAoC;IAEpC,MAAM,CAACC,SAASC,eAAe,GAAGF;IAClC,2CAA2C;IAC3C,MAAM,CAACG,gBAAgBC,iBAAiB,GAAGL;IAK3C,yDAAyD;IACzD,IAAI,CAACF,aAAaM,gBAAgBF,UAAU;QAC1C,kGAAkG;QAClG,IAAII,MAAMC,OAAO,CAACH,iBAAiB;YACjC,OAAO;QACT;QAEA,sEAAsE;QACtE,OAAO;IACT;IACA,MAAMI,cAAcR,kBAAkBS,MAAM,IAAI;IAEhD,IAAID,aAAa;QACf,OAAO;IACT;IAEA,OAAOT,mBACLC,kBAAkBU,KAAK,CAAC,IACxBP,cAAc,CAACE,iBAAiB;AAEpC"}