123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.reducer = reducer;
- exports.ACTION_PREFETCH = exports.ACTION_SERVER_PATCH = exports.ACTION_RESTORE = exports.ACTION_NAVIGATE = exports.ACTION_RELOAD = void 0;
- var _extends = require("@swc/helpers/lib/_extends.js").default;
- var _react = require("react");
- var _matchSegments = require("./match-segments");
- var _appRouterClient = require("./app-router.client");
-
- function invalidateCacheByRouterState(newCache, existingCache, routerState) {
-
- for(const key in routerState[1]){
- const segmentForParallelRoute = routerState[1][key][0];
- const cacheKey = Array.isArray(segmentForParallelRoute) ? segmentForParallelRoute[1] : segmentForParallelRoute;
- const existingParallelRoutesCacheNode = existingCache.parallelRoutes.get(key);
- if (existingParallelRoutesCacheNode) {
- let parallelRouteCacheNode = new Map(existingParallelRoutesCacheNode);
- parallelRouteCacheNode.delete(cacheKey);
- newCache.parallelRoutes.set(key, parallelRouteCacheNode);
- }
- }
- }
- function fillCacheWithNewSubTreeData(newCache, existingCache, flightDataPath) {
- const isLastEntry = flightDataPath.length <= 4;
- const [parallelRouteKey, segment] = flightDataPath;
- const segmentForCache = Array.isArray(segment) ? segment[1] : segment;
- const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
- if (!existingChildSegmentMap) {
-
-
- return;
- }
- let childSegmentMap = newCache.parallelRoutes.get(parallelRouteKey);
- if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
- childSegmentMap = new Map(existingChildSegmentMap);
- newCache.parallelRoutes.set(parallelRouteKey, childSegmentMap);
- }
- const existingChildCacheNode = existingChildSegmentMap.get(segmentForCache);
- let childCacheNode = childSegmentMap.get(segmentForCache);
-
- if (isLastEntry) {
- if (!childCacheNode || !childCacheNode.data || childCacheNode === existingChildCacheNode) {
- childCacheNode = {
- data: null,
- subTreeData: flightDataPath[3],
-
- parallelRoutes: existingChildCacheNode ? new Map(existingChildCacheNode.parallelRoutes) : new Map()
- };
- if (existingChildCacheNode) {
- invalidateCacheByRouterState(childCacheNode, existingChildCacheNode, flightDataPath[2]);
- }
- childSegmentMap.set(segmentForCache, childCacheNode);
- }
- return;
- }
- if (!childCacheNode || !existingChildCacheNode) {
-
-
- return;
- }
- if (childCacheNode === existingChildCacheNode) {
- childCacheNode = {
- data: childCacheNode.data,
- subTreeData: childCacheNode.subTreeData,
- parallelRoutes: new Map(childCacheNode.parallelRoutes)
- };
- childSegmentMap.set(segmentForCache, childCacheNode);
- }
- fillCacheWithNewSubTreeData(childCacheNode, existingChildCacheNode, flightDataPath.slice(2));
- }
- function invalidateCacheBelowFlightSegmentPath(newCache, existingCache, flightSegmentPath) {
- const isLastEntry = flightSegmentPath.length <= 2;
- const [parallelRouteKey, segment] = flightSegmentPath;
- const segmentForCache = Array.isArray(segment) ? segment[1] : segment;
- const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
- if (!existingChildSegmentMap) {
-
-
- return;
- }
- let childSegmentMap = newCache.parallelRoutes.get(parallelRouteKey);
- if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
- childSegmentMap = new Map(existingChildSegmentMap);
- newCache.parallelRoutes.set(parallelRouteKey, childSegmentMap);
- }
-
- if (isLastEntry) {
- childSegmentMap.delete(segmentForCache);
- return;
- }
- const existingChildCacheNode = existingChildSegmentMap.get(segmentForCache);
- let childCacheNode = childSegmentMap.get(segmentForCache);
- if (!childCacheNode || !existingChildCacheNode) {
-
-
- return;
- }
- if (childCacheNode === existingChildCacheNode) {
- childCacheNode = {
- data: childCacheNode.data,
- subTreeData: childCacheNode.subTreeData,
- parallelRoutes: new Map(childCacheNode.parallelRoutes)
- };
- childSegmentMap.set(segmentForCache, childCacheNode);
- }
- invalidateCacheBelowFlightSegmentPath(childCacheNode, existingChildCacheNode, flightSegmentPath.slice(2));
- }
- function fillCacheWithPrefetchedSubTreeData(existingCache, flightDataPath) {
- const isLastEntry = flightDataPath.length <= 4;
- const [parallelRouteKey, segment] = flightDataPath;
- const segmentForCache = Array.isArray(segment) ? segment[1] : segment;
- const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
- if (!existingChildSegmentMap) {
-
- return;
- }
- const existingChildCacheNode = existingChildSegmentMap.get(segmentForCache);
-
- if (isLastEntry) {
- if (!existingChildCacheNode) {
- existingChildSegmentMap.set(segmentForCache, {
- data: null,
- subTreeData: flightDataPath[3],
- parallelRoutes: new Map()
- });
- }
- return;
- }
- if (!existingChildCacheNode) {
-
- return;
- }
- fillCacheWithPrefetchedSubTreeData(existingChildCacheNode, flightDataPath.slice(2));
- }
- function fillCacheWithDataProperty(newCache, existingCache, segments, fetchResponse) {
- const isLastEntry = segments.length === 1;
- const parallelRouteKey = 'children';
- const [segment] = segments;
- const existingChildSegmentMap = existingCache.parallelRoutes.get(parallelRouteKey);
- if (!existingChildSegmentMap) {
-
-
- 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(segment);
- let childCacheNode = childSegmentMap.get(segment);
-
- if (isLastEntry) {
- if (!childCacheNode || !childCacheNode.data || childCacheNode === existingChildCacheNode) {
- childSegmentMap.set(segment, {
- data: fetchResponse(),
- subTreeData: null,
- parallelRoutes: new Map()
- });
- }
- return;
- }
- if (!childCacheNode || !existingChildCacheNode) {
-
- if (!childCacheNode) {
- childSegmentMap.set(segment, {
- data: fetchResponse(),
- subTreeData: null,
- parallelRoutes: new Map()
- });
- }
- return;
- }
- if (childCacheNode === existingChildCacheNode) {
- childCacheNode = {
- data: childCacheNode.data,
- subTreeData: childCacheNode.subTreeData,
- parallelRoutes: new Map(childCacheNode.parallelRoutes)
- };
- childSegmentMap.set(segment, childCacheNode);
- }
- return fillCacheWithDataProperty(childCacheNode, existingChildCacheNode, segments.slice(1), fetchResponse);
- }
- function createOptimisticTree(segments, flightRouterState, _isFirstSegment, parentRefetch, _href) {
- const [existingSegment, existingParallelRoutes] = flightRouterState || [
- null,
- {},
- ];
- const segment = segments[0];
- const isLastSegment = segments.length === 1;
- const segmentMatches = existingSegment !== null && (0, _matchSegments).matchSegment(existingSegment, segment);
- const shouldRefetchThisLevel = !flightRouterState || !segmentMatches;
- let parallelRoutes = {};
- if (existingSegment !== null && segmentMatches) {
- parallelRoutes = existingParallelRoutes;
- }
- let childTree;
- if (!isLastSegment) {
- const childItem = createOptimisticTree(segments.slice(1), parallelRoutes ? parallelRoutes.children : null, false, parentRefetch || shouldRefetchThisLevel);
- childTree = childItem;
- }
- const result = [
- segment,
- _extends({}, parallelRoutes, childTree ? {
- children: childTree
- } : {}),
- ];
- if (!parentRefetch && shouldRefetchThisLevel) {
- result[3] = 'refetch';
- }
-
-
-
-
-
- return result;
- }
- function applyRouterStatePatchToTree(flightSegmentPath, flightRouterState, treePatch) {
- const [segment, parallelRoutes ] = flightRouterState;
-
- if (flightSegmentPath.length === 1) {
- const tree = [
- ...treePatch
- ];
-
-
-
-
- return tree;
- }
- const [currentSegment, parallelRouteKey] = flightSegmentPath;
-
- if (!(0, _matchSegments).matchSegment(currentSegment, segment)) {
- return null;
- }
- const lastSegment = flightSegmentPath.length === 2;
- let parallelRoutePatch;
- if (lastSegment) {
- parallelRoutePatch = treePatch;
- } else {
- parallelRoutePatch = applyRouterStatePatchToTree(flightSegmentPath.slice(2), parallelRoutes[parallelRouteKey], treePatch);
- if (parallelRoutePatch === null) {
- return null;
- }
- }
- const tree = [
- flightSegmentPath[0],
- _extends({}, parallelRoutes, {
- [parallelRouteKey]: parallelRoutePatch
- }),
- ];
-
-
-
-
- return tree;
- }
- function shouldHardNavigate(flightSegmentPath, flightRouterState, treePatch) {
- const [segment, parallelRoutes] = flightRouterState;
-
- const [currentSegment, parallelRouteKey] = flightSegmentPath;
-
- if (Array.isArray(currentSegment) && !(0, _matchSegments).matchSegment(currentSegment, segment)) {
- return true;
- }
- const lastSegment = flightSegmentPath.length <= 2;
- if (lastSegment) {
- return false;
- }
- return shouldHardNavigate(flightSegmentPath.slice(2), parallelRoutes[parallelRouteKey], treePatch);
- }
- const ACTION_RELOAD = 'reload';
- exports.ACTION_RELOAD = ACTION_RELOAD;
- const ACTION_NAVIGATE = 'navigate';
- exports.ACTION_NAVIGATE = ACTION_NAVIGATE;
- const ACTION_RESTORE = 'restore';
- exports.ACTION_RESTORE = ACTION_RESTORE;
- const ACTION_SERVER_PATCH = 'server-patch';
- exports.ACTION_SERVER_PATCH = ACTION_SERVER_PATCH;
- const ACTION_PREFETCH = 'prefetch';
- exports.ACTION_PREFETCH = ACTION_PREFETCH;
- function reducer(state, action) {
- switch(action.type){
- case ACTION_NAVIGATE:
- {
- const { url , navigateType , cache , mutable , forceOptimisticNavigation } = action;
- const { pathname , search , hash } = url;
- const href = pathname + search + hash;
- const pendingPush = navigateType === 'push';
-
- if (mutable.patchedTree && JSON.stringify(mutable.previousTree) === JSON.stringify(state.tree)) {
- return {
-
- canonicalUrl: href,
-
- pushRef: {
- pendingPush,
- mpaNavigation: false
- },
-
- focusAndScrollRef: {
- apply: true
- },
-
- cache: mutable.useExistingCache ? state.cache : cache,
- prefetchCache: state.prefetchCache,
-
- tree: mutable.patchedTree
- };
- }
- const prefetchValues = state.prefetchCache.get(href);
- if (prefetchValues) {
-
- const { flightSegmentPath , treePatch } = prefetchValues;
-
- const newTree = applyRouterStatePatchToTree(
- [
- '',
- ...flightSegmentPath
- ], state.tree, treePatch);
- if (newTree !== null) {
- mutable.previousTree = state.tree;
- mutable.patchedTree = newTree;
- const hardNavigate =
- search !== location.search || shouldHardNavigate(
- [
- '',
- ...flightSegmentPath
- ], state.tree, newTree);
- if (hardNavigate) {
-
-
- cache.subTreeData = state.cache.subTreeData;
- invalidateCacheBelowFlightSegmentPath(cache, state.cache, flightSegmentPath);
- } else {
- mutable.useExistingCache = true;
- }
- return {
-
- canonicalUrl: href,
-
- pushRef: {
- pendingPush,
- mpaNavigation: false
- },
-
- focusAndScrollRef: {
- apply: true
- },
-
- cache: mutable.useExistingCache ? state.cache : cache,
- prefetchCache: state.prefetchCache,
-
- tree: newTree
- };
- }
- }
-
-
-
-
- if (forceOptimisticNavigation) {
- const segments = pathname.split('/');
-
- segments.push('');
-
-
- const optimisticTree = createOptimisticTree(segments, state.tree, true, false, href);
-
-
-
- cache.subTreeData = state.cache.subTreeData;
-
-
- const res = fillCacheWithDataProperty(cache, state.cache, segments.slice(1), ()=>(0, _appRouterClient).fetchServerResponse(url, optimisticTree));
-
- if (!(res == null ? void 0 : res.bailOptimistic)) {
- mutable.previousTree = state.tree;
- mutable.patchedTree = optimisticTree;
- return {
-
- canonicalUrl: href,
-
- pushRef: {
- pendingPush,
- mpaNavigation: false
- },
-
- focusAndScrollRef: {
- apply: true
- },
-
- cache: cache,
- prefetchCache: state.prefetchCache,
-
- tree: optimisticTree
- };
- }
- }
-
-
- if (!cache.data) {
- cache.data = (0, _appRouterClient).fetchServerResponse(url, state.tree);
- }
-
- const [flightData] = (0, _react).experimental_use(cache.data);
-
- if (typeof flightData === 'string') {
- return {
- canonicalUrl: flightData,
-
- pushRef: {
- pendingPush: true,
- mpaNavigation: true
- },
-
- focusAndScrollRef: {
- apply: false
- },
- cache: state.cache,
- prefetchCache: state.prefetchCache,
- tree: state.tree
- };
- }
-
- cache.data = null;
-
- const flightDataPath = flightData[0];
-
- const [treePatch] = flightDataPath.slice(-2);
-
- const flightSegmentPath = flightDataPath.slice(0, -3);
-
- const newTree = applyRouterStatePatchToTree(
- [
- '',
- ...flightSegmentPath
- ], state.tree, treePatch);
- if (newTree === null) {
- throw new Error('SEGMENT MISMATCH');
- }
- mutable.previousTree = state.tree;
- mutable.patchedTree = newTree;
-
- cache.subTreeData = state.cache.subTreeData;
-
- fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath);
- return {
-
- canonicalUrl: href,
-
- pushRef: {
- pendingPush,
- mpaNavigation: false
- },
-
- focusAndScrollRef: {
- apply: true
- },
-
- cache: cache,
- prefetchCache: state.prefetchCache,
-
- tree: newTree
- };
- }
- case ACTION_SERVER_PATCH:
- {
- const { flightData , previousTree , cache , mutable } = action;
-
-
- if (JSON.stringify(previousTree) !== JSON.stringify(state.tree)) {
-
- console.log('TREE MISMATCH');
-
- return state;
- }
-
- if (mutable.patchedTree) {
- return {
-
- canonicalUrl: state.canonicalUrl,
-
- pushRef: state.pushRef,
-
- focusAndScrollRef: state.focusAndScrollRef,
-
- tree: mutable.patchedTree,
- prefetchCache: state.prefetchCache,
-
- cache: cache
- };
- }
-
- if (typeof flightData === 'string') {
- return {
-
- canonicalUrl: flightData,
-
- pushRef: {
- pendingPush: true,
- mpaNavigation: true
- },
-
- focusAndScrollRef: {
- apply: false
- },
-
- cache: state.cache,
- prefetchCache: state.prefetchCache,
- tree: state.tree
- };
- }
-
- const flightDataPath = flightData[0];
-
- const treePath = flightDataPath.slice(0, -3);
- const [treePatch] = flightDataPath.slice(-2);
- const newTree = applyRouterStatePatchToTree(
- [
- '',
- ...treePath
- ], state.tree, treePatch);
- if (newTree === null) {
- throw new Error('SEGMENT MISMATCH');
- }
- mutable.patchedTree = newTree;
-
- cache.subTreeData = state.cache.subTreeData;
- fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath);
- return {
-
- canonicalUrl: state.canonicalUrl,
-
- pushRef: state.pushRef,
-
- focusAndScrollRef: state.focusAndScrollRef,
-
- tree: newTree,
- prefetchCache: state.prefetchCache,
-
- cache: cache
- };
- }
- case ACTION_RESTORE:
- {
- const { url , tree } = action;
- const href = url.pathname + url.search + url.hash;
- return {
-
- canonicalUrl: href,
- pushRef: state.pushRef,
- focusAndScrollRef: state.focusAndScrollRef,
- cache: state.cache,
- prefetchCache: state.prefetchCache,
-
- tree: tree
- };
- }
- case ACTION_RELOAD:
- {
- const { cache , mutable } = action;
- const href = state.canonicalUrl;
-
- if (mutable.patchedTree && JSON.stringify(mutable.previousTree) === JSON.stringify(state.tree)) {
- return {
-
- canonicalUrl: href,
-
- pushRef: state.pushRef,
-
-
- focusAndScrollRef: {
- apply: true
- },
- cache: cache,
- prefetchCache: state.prefetchCache,
- tree: mutable.patchedTree
- };
- }
- if (!cache.data) {
-
- cache.data = (0, _appRouterClient).fetchServerResponse(new URL(href, location.origin), [
- state.tree[0],
- state.tree[1],
- state.tree[2],
- 'refetch',
- ]);
- }
- const [flightData] = (0, _react).experimental_use(cache.data);
-
- if (typeof flightData === 'string') {
- return {
- canonicalUrl: flightData,
- pushRef: {
- pendingPush: true,
- mpaNavigation: true
- },
- focusAndScrollRef: {
- apply: false
- },
- cache: state.cache,
- prefetchCache: state.prefetchCache,
- tree: state.tree
- };
- }
-
- cache.data = null;
-
- const flightDataPath = flightData[0];
-
- if (flightDataPath.length !== 2) {
-
- console.log('RELOAD FAILED');
- return state;
- }
-
- const [treePatch, subTreeData] = flightDataPath;
- const newTree = applyRouterStatePatchToTree(
- [
- ''
- ], state.tree, treePatch);
- if (newTree === null) {
- throw new Error('SEGMENT MISMATCH');
- }
- mutable.previousTree = state.tree;
- mutable.patchedTree = newTree;
-
- cache.subTreeData = subTreeData;
- return {
-
- canonicalUrl: href,
-
- pushRef: state.pushRef,
-
- focusAndScrollRef: {
- apply: false
- },
-
- cache: cache,
- prefetchCache: state.prefetchCache,
-
- tree: newTree
- };
- }
- case ACTION_PREFETCH:
- {
- const { url , flightData } = action;
-
- if (typeof flightData === 'string') {
- return state;
- }
- const { pathname , search , hash } = url;
- const href = pathname + search + hash;
-
- const flightDataPath = flightData[0];
-
- const [treePatch, subTreeData] = flightDataPath.slice(-2);
-
-
- if (subTreeData !== null) {
- fillCacheWithPrefetchedSubTreeData(state.cache, flightDataPath);
- }
-
- state.prefetchCache.set(href, {
-
- flightSegmentPath: flightDataPath.slice(0, -2),
- treePatch
- });
- return state;
- }
-
- default:
- throw new Error('Unknown action');
- }
- }
- if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
- Object.defineProperty(exports.default, '__esModule', { value: true });
- Object.assign(exports.default, exports);
- module.exports = exports.default;
- }
|