vike-lite-react 1.0.10 → 1.0.12

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,4 +1,4 @@
1
- import { t as PageContextReactContext } from "./globalContext-BIY5-Fdk.mjs";
1
+ import { t as PageContextReactContext } from "./globalContext-6JEqyfKe.mjs";
2
2
  import { jsx } from "react/jsx-runtime";
3
3
  //#region src/hooks/PageContextProvider.tsx
4
4
  function PageContextProvider({ value, children }) {
@@ -1,16 +1,9 @@
1
- import { t as PageContextProvider } from "../../PageContextProvider-C21BRCMp.mjs";
1
+ import { t as PageContextProvider } from "../../PageContextProvider-DfE3nMiU.mjs";
2
2
  import { Component, useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { createRoot, hydrateRoot } from "react-dom/client";
4
- import { matchRoute } from "vike-lite/__internal/shared";
4
+ import { BASE_URL, matchRoute, stripBase } from "vike-lite/__internal/shared";
5
+ import { createLinkClickHandler, createLinkPrefetchHandler, finalizeNavigation } from "vike-lite/__internal/client";
5
6
  import { jsx, jsxs } from "react/jsx-runtime";
6
- //#region src/__internal/shared/stripBase.ts
7
- function stripBase(pathname) {
8
- const { BASE_URL } = import.meta.env;
9
- if (BASE_URL === "/" || !pathname.startsWith(BASE_URL)) return pathname;
10
- const stripped = pathname.slice(BASE_URL.length);
11
- return stripped.startsWith("/") ? stripped : "/" + stripped;
12
- }
13
- //#endregion
14
7
  //#region src/__internal/client/onRenderClient.tsx
15
8
  var RootErrorBoundary = class extends Component {
16
9
  static getDerivedStateFromError(error) {
@@ -37,19 +30,11 @@ function RouterApp(props) {
37
30
  }, []);
38
31
  const matchedRoute = useMemo(() => matchRoute(currentPathname, props.routes), [currentPathname, props.routes]);
39
32
  useEffect(() => {
40
- const handleLinkClick = (e) => {
41
- if (!(e.target instanceof Element)) return;
42
- const target = e.target.closest("a");
43
- if (!target?.href || target.target === "_blank" || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return;
44
- const url = new URL(target.href);
45
- if (url.origin !== globalThis.location.origin) return;
46
- if (url.pathname === globalThis.location.pathname && url.search === globalThis.location.search) return;
47
- e.preventDefault();
48
- globalThis.history.pushState({ triggeredBy: "vike-lite" }, "", url.href);
33
+ const handleLinkClick = createLinkClickHandler((url) => {
49
34
  if (!url.hash) shouldScrollToTop.current = true;
50
35
  setCurrentUrl(url.href);
51
36
  setCurrentPathname(stripBase(url.pathname));
52
- };
37
+ });
53
38
  const prefetchedModules = /* @__PURE__ */ new Set();
54
39
  function prefetchRoute(route) {
55
40
  const modules = [
@@ -65,19 +50,10 @@ function RouterApp(props) {
65
50
  });
66
51
  }
67
52
  }
68
- const handleLinkPrefetch = (e) => {
69
- if (!(e.target instanceof Element)) return;
70
- const target = e.target.closest("a");
71
- if (!target?.href) return;
72
- if (target.target && target.target !== "_self") return;
73
- if (target.hasAttribute("download")) return;
74
- const url = new URL(target.href);
75
- if (url.origin !== globalThis.location.origin) return;
76
- if (url.pathname === globalThis.location.pathname && url.search === globalThis.location.search) return;
53
+ const handleLinkPrefetch = createLinkPrefetchHandler((url) => {
77
54
  const matched = matchRoute(stripBase(url.pathname), props.routes);
78
- if (!matched) return;
79
- prefetchRoute(matched.route);
80
- };
55
+ if (matched) prefetchRoute(matched.route);
56
+ });
81
57
  const handlePopState = () => {
82
58
  setCurrentUrl(globalThis.location.href);
83
59
  setCurrentPathname(stripBase(globalThis.location.pathname));
@@ -112,7 +88,7 @@ function RouterApp(props) {
112
88
  useEffect(() => {
113
89
  const pathname = currentPathname;
114
90
  const isReload = reloadTick > 0;
115
- if (isFirstRun.current && !isReload && globalThis.__PAGE_CONTEXT__?.urlPathname === pathname) {
91
+ if (!isReload && isFirstRun.current && globalThis.__PAGE_CONTEXT__?.urlPathname === pathname) {
116
92
  isFirstRun.current = false;
117
93
  globalThis.__PAGE_CONTEXT__.urlPathname = void 0;
118
94
  return;
@@ -122,52 +98,41 @@ function RouterApp(props) {
122
98
  const signal = controller.signal;
123
99
  const urlFull = currentUrl;
124
100
  const matched = matchedRoute;
101
+ const renderErrorPage = async (is404, message) => {
102
+ if (!props.errorRoute) return;
103
+ const [ErrorPageMod, ErrorLayoutMod, ErrorHeadMod] = await Promise.all([
104
+ props.errorRoute.Page(),
105
+ props.errorRoute.Layout?.() ?? null,
106
+ props.errorRoute.Head?.() ?? null
107
+ ]);
108
+ if (signal.aborted) return;
109
+ setPageContext(() => ({
110
+ ...pageContext,
111
+ urlOriginal: urlFull,
112
+ urlPathname: pathname,
113
+ routeParams: {},
114
+ is404,
115
+ is500: !is404,
116
+ errorMessage: message,
117
+ isClientSide: true,
118
+ isHydration: false
119
+ }));
120
+ setView({
121
+ Page: ErrorPageMod.Page ?? ErrorPageMod.default,
122
+ Layout: ErrorLayoutMod?.Layout ?? ErrorLayoutMod?.default ?? null,
123
+ Head: ErrorHeadMod?.Head ?? ErrorHeadMod?.default ?? null
124
+ });
125
+ document.title = is404 ? "Not Found" : "Server Error";
126
+ finalizeNavigation(shouldScrollToTop.current);
127
+ };
125
128
  const loadRoute = async () => {
126
129
  const contextOverride = pendingContextOverride.current;
127
130
  pendingContextOverride.current = null;
128
- function finalizeNavigation() {
129
- if (shouldScrollToTop.current) {
130
- globalThis.scrollTo(0, 0);
131
- shouldScrollToTop.current = false;
132
- } else if (globalThis.location.hash) requestAnimationFrame(() => {
133
- try {
134
- document.querySelector(decodeURIComponent(globalThis.location.hash))?.scrollIntoView();
135
- } catch {}
136
- });
137
- }
138
- const renderErrorPage = async (is404, message) => {
139
- if (!props.errorRoute) return;
140
- const [ErrorPageMod, ErrorLayoutMod, ErrorHeadMod] = await Promise.all([
141
- props.errorRoute.Page(),
142
- props.errorRoute.Layout?.() ?? null,
143
- props.errorRoute.Head?.() ?? null
144
- ]);
145
- if (signal.aborted) return;
146
- setPageContext(() => ({
147
- urlOriginal: urlFull,
148
- urlPathname: pathname,
149
- routeParams: {},
150
- is404,
151
- is500: !is404,
152
- errorMessage: message,
153
- isClientSide: true,
154
- isHydration: false
155
- }));
156
- setView({
157
- Page: ErrorPageMod.Page ?? ErrorPageMod.default,
158
- Layout: ErrorLayoutMod?.Layout ?? ErrorLayoutMod?.default ?? null,
159
- Head: ErrorHeadMod?.Head ?? ErrorHeadMod?.default ?? null
160
- });
161
- document.title = is404 ? "Not Found" : "Server Error";
162
- finalizeNavigation();
163
- };
164
131
  if (!matched) return renderErrorPage(true);
165
132
  const { route, routeParams } = matched;
166
133
  try {
167
134
  const urlObj = new URL(urlFull);
168
- const jsonTarget = pathname === "/" ? "/index" : pathname;
169
- const { BASE_URL } = import.meta.env;
170
- const jsonUrl = `${BASE_URL.endsWith("/") ? BASE_URL.slice(0, -1) : BASE_URL}${jsonTarget}.pageContext.json${urlObj.search}`;
135
+ const jsonUrl = `${BASE_URL}${pathname === "/" ? "/index" : pathname}.pageContext.json${urlObj.search}`;
171
136
  let ctx = null;
172
137
  if (route.data || route.title) {
173
138
  const res = await fetch(jsonUrl, {
@@ -219,7 +184,7 @@ function RouterApp(props) {
219
184
  if (globalThis.location.hash) return;
220
185
  document.querySelector("#root")?.focus({ preventScroll: true });
221
186
  });
222
- finalizeNavigation();
187
+ finalizeNavigation(shouldScrollToTop.current);
223
188
  } catch (error) {
224
189
  if (error.name === "AbortError") return;
225
190
  const message = error.message || "";
@@ -1,4 +1,4 @@
1
- import { t as PageContextProvider } from "../../PageContextProvider-C21BRCMp.mjs";
1
+ import { t as PageContextProvider } from "../../PageContextProvider-DfE3nMiU.mjs";
2
2
  import { jsx } from "react/jsx-runtime";
3
3
  import { renderToString } from "react-dom/server";
4
4
  //#region src/__internal/server/onRenderHtml.tsx
@@ -0,0 +1,6 @@
1
+ import { createContext } from "react";
2
+ //#region src/hooks/globalContext.tsx
3
+ const KEY = Symbol.for("vike-lite-react:context");
4
+ const PageContextReactContext = globalThis[KEY] ??= createContext(null);
5
+ //#endregion
6
+ export { PageContextReactContext as t };
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import "react";
1
2
  import { PageContext, PageContextClient } from "vike-lite";
2
3
  //#region src/hooks/useData.d.ts
3
4
  declare function useData<Data = unknown>(): [Data, (updater: Data | ((prev: Data) => Data)) => void];
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PageContextReactContext } from "./globalContext-BIY5-Fdk.mjs";
1
+ import { t as PageContextReactContext } from "./globalContext-6JEqyfKe.mjs";
2
2
  import { useCallback, useContext, useEffect, useMemo, useState } from "react";
3
3
  //#region src/hooks/useData.tsx
4
4
  function useData() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-lite-react",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -62,8 +62,8 @@
62
62
  "@vitejs/plugin-react": "^6.0.3",
63
63
  "react": "^19.2.7",
64
64
  "react-dom": "^19.2.7",
65
- "tsdown": "^0.22.8",
66
- "vike-lite": "1.15.6",
65
+ "tsdown": "^0.22.9",
66
+ "vike-lite": "1.15.9",
67
67
  "vite": "^8.1.5"
68
68
  },
69
69
  "peerDependencies": {
@@ -1,8 +0,0 @@
1
- import { createContext } from "react";
2
- //#region src/hooks/globalContext.tsx
3
- const KEY = "__vike_lite_react_context__";
4
- const g = globalThis;
5
- if (!Object.hasOwn(g, KEY)) g[KEY] = createContext(null);
6
- const PageContextReactContext = g[KEY];
7
- //#endregion
8
- export { PageContextReactContext as t };