@upstart.gg/vite-plugins 0.1.61 → 0.1.62

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.
@@ -62,6 +62,43 @@ export function waitForHydration(callback: () => void): void {
62
62
  }
63
63
  }
64
64
 
65
+ /**
66
+ * Report the react-router id of the rendered route to the parent editor, so it can edit
67
+ * that route's `meta` export (page title, description…).
68
+ *
69
+ * The id is read from react-router's own data router rather than from `history.pushState`:
70
+ * the router pushes the new URL BEFORE it commits the new matches, so a route id read at
71
+ * pushState time would still be the previous page's. Subscribing gives us the id once the
72
+ * navigation is complete. The router global only appears during hydration, hence the retry.
73
+ */
74
+ function initRouteReporter(): void {
75
+ let lastReported: string | undefined;
76
+
77
+ const report = () => {
78
+ // biome-ignore lint/suspicious/noExplicitAny: react-router does not type its window globals
79
+ const matches = (window as any).__reactRouterDataRouter?.state?.matches;
80
+ const routeId = Array.isArray(matches) ? matches[matches.length - 1]?.route?.id : undefined;
81
+ if (typeof routeId !== "string" || routeId === lastReported) return;
82
+ lastReported = routeId;
83
+ sendToParent({ type: "editor-route", routeId });
84
+ };
85
+
86
+ const attach = (): boolean => {
87
+ // biome-ignore lint/suspicious/noExplicitAny: react-router does not type its window globals
88
+ const router = (window as any).__reactRouterDataRouter;
89
+ if (typeof router?.subscribe !== "function") return false;
90
+ router.subscribe(() => report());
91
+ report();
92
+ return true;
93
+ };
94
+
95
+ if (attach()) return;
96
+ let attempts = 0;
97
+ const timer = setInterval(() => {
98
+ if (attach() || ++attempts > 25) clearInterval(timer);
99
+ }, 200);
100
+ }
101
+
65
102
  /**
66
103
  * Initialize the Upstart editor runtime.
67
104
  */
@@ -115,6 +152,7 @@ export function initUpstartEditor(): void {
115
152
  initArrayControls();
116
153
  initFormGuard();
117
154
  initErrorHandler();
155
+ initRouteReporter();
118
156
 
119
157
  sendToParent({ type: "editor-ready", path: currentPath() });
120
158
  } catch (error) {
@@ -92,6 +92,8 @@ export type EditorMessage =
92
92
  }
93
93
  | { type: "editor-ready"; path?: string }
94
94
  | { type: "editor-navigated"; path?: string }
95
+ /** React-router id of the route currently rendered (e.g. "routes/_layout._index"). */
96
+ | { type: "editor-route"; routeId: string }
95
97
  | { type: "scroll-position"; x: number; y: number }
96
98
  | { type: "editor-error"; error: string }
97
99
  | { type: "selection-changed"; selection: SelectionRef[] }