UNPKG

next

Version:

The React Framework

165 lines (163 loc) • 6.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { DYNAMIC_STALETIME_MS: null, STATIC_STALETIME_MS: null, generateSegmentsFromPatch: null, handleExternalUrl: null, navigateReducer: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { DYNAMIC_STALETIME_MS: function() { return DYNAMIC_STALETIME_MS; }, STATIC_STALETIME_MS: function() { return STATIC_STALETIME_MS; }, generateSegmentsFromPatch: function() { return generateSegmentsFromPatch; }, handleExternalUrl: function() { return handleExternalUrl; }, navigateReducer: function() { return navigateReducer; } }); const _createhreffromurl = require("../create-href-from-url"); const _handlemutable = require("../handle-mutable"); const _navigation = require("../../segment-cache/navigation"); const _types = require("../../segment-cache/types"); const _cache = require("../../segment-cache/cache"); const DYNAMIC_STALETIME_MS = Number(process.env.__NEXT_CLIENT_ROUTER_DYNAMIC_STALETIME) * 1000; const STATIC_STALETIME_MS = (0, _cache.getStaleTimeMs)(Number(process.env.__NEXT_CLIENT_ROUTER_STATIC_STALETIME)); function handleExternalUrl(state, mutable, url, pendingPush) { mutable.mpaNavigation = true; mutable.canonicalUrl = url; mutable.pendingPush = pendingPush; mutable.scrollableSegments = undefined; return (0, _handlemutable.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 handleNavigationResult(url, state, mutable, pendingPush, result) { switch(result.tag){ case _types.NavigationResultTag.MPA: { // Perform an MPA navigation. const newUrl = result.data; return handleExternalUrl(state, mutable, newUrl, pendingPush); } case _types.NavigationResultTag.NoOp: { // The server responded with no change to the current page. However, if // the URL changed, we still need to update that. const newCanonicalUrl = result.data.canonicalUrl; mutable.canonicalUrl = newCanonicalUrl; // Check if the only thing that changed was the hash fragment. const oldUrl = new URL(state.canonicalUrl, url); const onlyHashChange = // We don't need to compare the origins, because client-driven // navigations are always same-origin. url.pathname === oldUrl.pathname && url.search === oldUrl.search && url.hash !== oldUrl.hash; if (onlyHashChange) { // The only updated part of the URL is the hash. mutable.onlyHashChange = true; mutable.shouldScroll = result.data.shouldScroll; mutable.hashFragment = url.hash; // Setting this to an empty array triggers a scroll for all new and // updated segments. See `ScrollAndFocusHandler` for more details. mutable.scrollableSegments = []; } return (0, _handlemutable.handleMutable)(state, mutable); } case _types.NavigationResultTag.Success: { // Received a new result. mutable.cache = result.data.cacheNode; mutable.patchedTree = result.data.flightRouterState; mutable.renderedSearch = result.data.renderedSearch; mutable.canonicalUrl = result.data.canonicalUrl; mutable.scrollableSegments = result.data.scrollableSegments; mutable.shouldScroll = result.data.shouldScroll; mutable.hashFragment = result.data.hash; return (0, _handlemutable.handleMutable)(state, mutable); } case _types.NavigationResultTag.Async: { return result.data.then((asyncResult)=>handleNavigationResult(url, state, mutable, pendingPush, asyncResult), // If the navigation failed, return the current state. // TODO: This matches the current behavior but we need to do something // better here if the network fails. ()=>{ return state; }); } default: { result; return state; } } } function navigateReducer(state, action) { const { url, isExternalUrl, navigateType, shouldScroll } = action; const mutable = {}; const href = (0, _createhreffromurl.createHrefFromUrl)(url); const pendingPush = navigateType === 'push'; mutable.preserveCustomHistoryState = false; mutable.pendingPush = pendingPush; if (isExternalUrl) { return handleExternalUrl(state, mutable, url.toString(), pendingPush); } // Handles case where `<meta http-equiv="refresh">` tag is present, // which will trigger an MPA navigation. if (document.getElementById('__next-page-redirect')) { return handleExternalUrl(state, mutable, href, pendingPush); } // Temporary glue code between the router reducer and the new navigation // implementation. Eventually we'll rewrite the router reducer to a // state machine. const currentUrl = new URL(state.canonicalUrl, location.origin); const result = (0, _navigation.navigate)(url, currentUrl, state.cache, state.tree, state.nextUrl, shouldScroll, mutable); return handleNavigationResult(url, state, mutable, pendingPush, result); } 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; } //# sourceMappingURL=navigate-reducer.js.map