next
Version:
The React Framework
50 lines (49 loc) • 2.7 kB
JavaScript
import { computeChangedPath } from './compute-changed-path';
function isNotUndefined(value) {
return typeof value !== 'undefined';
}
export function handleMutable(state, mutable) {
// shouldScroll is true by default, can override to false.
const shouldScroll = mutable.shouldScroll ?? true;
let previousNextUrl = state.previousNextUrl;
let nextUrl = state.nextUrl;
if (isNotUndefined(mutable.patchedTree)) {
// If we received a patched tree, we need to compute the changed path.
const changedPath = computeChangedPath(state.tree, mutable.patchedTree);
if (changedPath) {
// If the tree changed, we need to update the nextUrl
previousNextUrl = nextUrl;
nextUrl = changedPath;
} else if (!nextUrl) {
// if the tree ends up being the same (ie, no changed path), and we don't have a nextUrl, then we should use the canonicalUrl
nextUrl = state.canonicalUrl;
}
// otherwise this will be a no-op and continue to use the existing nextUrl
}
return {
// Set href.
canonicalUrl: mutable.canonicalUrl ?? state.canonicalUrl,
renderedSearch: mutable.renderedSearch ?? state.renderedSearch,
pushRef: {
pendingPush: isNotUndefined(mutable.pendingPush) ? mutable.pendingPush : state.pushRef.pendingPush,
mpaNavigation: isNotUndefined(mutable.mpaNavigation) ? mutable.mpaNavigation : state.pushRef.mpaNavigation,
preserveCustomHistoryState: isNotUndefined(mutable.preserveCustomHistoryState) ? mutable.preserveCustomHistoryState : state.pushRef.preserveCustomHistoryState
},
// All navigation requires scroll and focus management to trigger.
focusAndScrollRef: {
apply: shouldScroll ? isNotUndefined(mutable?.scrollableSegments) ? true : state.focusAndScrollRef.apply : false,
onlyHashChange: mutable.onlyHashChange || false,
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 ?? state.focusAndScrollRef.segmentPaths : []
},
// Apply cache.
cache: mutable.cache ? mutable.cache : state.cache,
// Apply patched router state.
tree: isNotUndefined(mutable.patchedTree) ? mutable.patchedTree : state.tree,
nextUrl,
previousNextUrl: previousNextUrl,
debugInfo: mutable.collectedDebugInfo ?? null
};
}
//# sourceMappingURL=handle-mutable.js.map