@webround/core 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  // packages/core/src/context/WrAppContext.tsx
3
3
  import { createContext, useContext, useState, useCallback, useEffect, useMemo, useSyncExternalStore, lazy, Suspense, } from 'react';
4
- import { createBrowserRouter, RouterProvider, useLocation, useParams } from 'react-router-dom';
4
+ import { createBrowserRouter, matchRoutes, RouterProvider, useLocation, useParams } from 'react-router-dom';
5
5
  import { WrScroll } from '../scroll';
6
- import { wrResolveRoutes, wrBuildRegistry, wrBuildLazyRegistry, wrBuildPageConfigMap, wrExpandRoutesForLangs, wrExtractLang, wrBuildLangPath, } from '../routing';
6
+ import { wrResolveRoutes, wrBuildRegistry, wrBuildLazyRegistry, wrBuildPageConfigMap, wrExpandRoutesForLangs, wrExtractLang, wrBuildLangPath, wrHomeOf, } from '../routing';
7
7
  import { WrPageProvider } from './WrPageContext';
8
8
  import { WrLayout } from './WrLayout';
9
9
  import { WrSharedContext } from '../components/WrShared';
@@ -50,6 +50,24 @@ function WrRouteElement({ route, pageConfigMap, lazyFallback: LazyFallback, Page
50
50
  })();
51
51
  return (_jsxs(PageProvider, { config: config, pageName: pageName, children: [_jsx(WrScroll, {}), _jsxs(WrSharedContext.Provider, { value: shared, children: [_jsx(WrSeoHead, { site: seo, config: config, pathname: pathname, lang: lang, langs: langs }), _jsx(WrLayout, { Header: Header, Footer: Footer, showHeader: route.header, showFooter: route.footer, pageConfigMap: pageConfigMap, ScopeProvider: PageProvider, children: body })] })] }, pageName));
52
52
  }
53
+ /**
54
+ * An address that leads to no page is the home page. The address is replaced BEFORE the router reads it, so the first render is the home page's: what was served for that address is the home page's
55
+ * snapshot, and the app takes it over (hydrates it) only if it draws the same page. A redirect after the first render would draw nothing at first, and React neither takes over nor removes what it did not claim.
56
+ */
57
+ function replaceUnknownAddress(routes, notFound, langs) {
58
+ if (typeof window === 'undefined')
59
+ return;
60
+ const { pathname, search, hash } = window.location;
61
+ const matches = matchRoutes(routes, pathname);
62
+ const match = matches?.[matches.length - 1];
63
+ // `/:lang/…` answers for any word in that place: only the languages of the site are one (see WrRouteElement)
64
+ const unknownLang = !!langs && match?.params.lang !== undefined && !langs.available.includes(match.params.lang);
65
+ if (match?.route !== notFound && !unknownLang)
66
+ return;
67
+ const home = wrHomeOf(pathname, langs);
68
+ if (home !== pathname)
69
+ window.history.replaceState(window.history.state, '', `${home}${search}${hash}`);
70
+ }
53
71
  export function WrAppProvider({ routesFile, eagerModules, lazyModules, configModules, langs, initialLang = 'default', lazyFallback = WrDefaultPageFallback, onRouterReady, PageProvider = WrPageProvider, Header, Footer, seo, children, }) {
54
72
  const router = useMemo(() => {
55
73
  const registry = wrBuildRegistry(eagerModules);
@@ -60,14 +78,17 @@ export function WrAppProvider({ routesFile, eagerModules, lazyModules, configMod
60
78
  console.error('[wr] unresolved route -> component mappings:', errors.missingComponents);
61
79
  }
62
80
  const expandedRoutes = wrExpandRoutesForLangs(routes, langs);
63
- const builtRouter = createBrowserRouter([
81
+ // An address that leads to no page takes the visitor to the home page
82
+ const notFound = { path: '*', element: _jsx(WrNotFound, { langs: langs }) };
83
+ const routeObjects = [
64
84
  ...expandedRoutes.map((route) => ({
65
85
  path: route.pattern,
66
86
  element: _jsx(WrRouteElement, { route: route, pageConfigMap: pageConfigMap, lazyFallback: lazyFallback, PageProvider: PageProvider, Header: Header, Footer: Footer, seo: seo, langs: langs })
67
87
  })),
68
- // An address that leads to no page takes the visitor to the home page
69
- { path: '*', element: _jsx(WrNotFound, { langs: langs }) },
70
- ]);
88
+ notFound,
89
+ ];
90
+ replaceUnknownAddress(routeObjects, notFound, langs);
91
+ const builtRouter = createBrowserRouter(routeObjects);
71
92
  onRouterReady?.(builtRouter.navigate);
72
93
  return builtRouter;
73
94
  }, [routesFile, eagerModules, lazyModules, configModules, langs, lazyFallback, onRouterReady, PageProvider, Header, Footer, seo]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webround/core",
3
- "version": "0.3.10",
3
+ "version": "0.3.11",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",