rwsdk 1.0.0-beta.4 → 1.0.0-beta.5

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,6 +1,7 @@
1
1
  import "./setWebpackRequire";
2
2
  export { default as React } from "react";
3
3
  export { ClientOnly } from "./ClientOnly.js";
4
+ export { initClientNavigation, navigate } from "./navigation.js";
4
5
  import type { HydrationOptions, Transport } from "./types";
5
6
  export declare const fetchTransport: Transport;
6
7
  export declare const initClient: ({ transport, hydrateRootOptions, handleResponse, }?: {
@@ -11,6 +11,7 @@ import { createFromFetch, createFromReadableStream, encodeReply, } from "react-s
11
11
  import { rscStream } from "rsc-html-stream/client";
12
12
  export { default as React } from "react";
13
13
  export { ClientOnly } from "./ClientOnly.js";
14
+ export { initClientNavigation, navigate } from "./navigation.js";
14
15
  export const fetchTransport = (transportContext) => {
15
16
  const fetchCallServer = async (id, args) => {
16
17
  const url = new URL(window.location.href);
@@ -4,6 +4,14 @@ export interface ClientNavigationOptions {
4
4
  scrollBehavior?: "auto" | "smooth" | "instant";
5
5
  }
6
6
  export declare function validateClickEvent(event: MouseEvent, target: HTMLElement): boolean;
7
+ export interface NavigateOptions {
8
+ history?: "push" | "replace";
9
+ info?: {
10
+ scrollToTop?: boolean;
11
+ scrollBehavior?: "auto" | "smooth" | "instant";
12
+ };
13
+ }
14
+ export declare function navigate(href: string, options?: NavigateOptions): Promise<void>;
7
15
  export declare function initClientNavigation(opts?: ClientNavigationOptions): {
8
16
  handleResponse: (response: Response) => boolean;
9
17
  };
@@ -1,10 +1,3 @@
1
- function saveScrollPosition(x, y) {
2
- window.history.replaceState({
3
- ...window.history.state,
4
- scrollX: x,
5
- scrollY: y,
6
- }, "", window.location.href);
7
- }
8
1
  export function validateClickEvent(event, target) {
9
2
  // should this only work for left click?
10
3
  if (event.button !== 0) {
@@ -37,19 +30,43 @@ export function validateClickEvent(event, target) {
37
30
  }
38
31
  return true;
39
32
  }
33
+ let IS_CLIENT_NAVIGATION = false;
34
+ export async function navigate(href, options = { history: "push" }) {
35
+ if (!IS_CLIENT_NAVIGATION) {
36
+ window.location.href = href;
37
+ return;
38
+ }
39
+ saveScrollPosition(window.scrollX, window.scrollY);
40
+ const url = window.location.origin + href;
41
+ if (options.history === "push") {
42
+ window.history.pushState({ path: href }, "", url);
43
+ }
44
+ else {
45
+ window.history.replaceState({ path: href }, "", url);
46
+ }
47
+ await globalThis.__rsc_callServer;
48
+ const scrollToTop = options.info?.scrollToTop ?? true;
49
+ const scrollBehavior = options.info?.scrollBehavior ?? "instant";
50
+ if (scrollToTop && history.scrollRestoration === "auto") {
51
+ window.scrollTo({
52
+ top: 0,
53
+ left: 0,
54
+ behavior: scrollBehavior,
55
+ });
56
+ saveScrollPosition(0, 0);
57
+ }
58
+ }
59
+ function saveScrollPosition(x, y) {
60
+ window.history.replaceState({
61
+ ...window.history.state,
62
+ scrollX: x,
63
+ scrollY: y,
64
+ }, "", window.location.href);
65
+ }
40
66
  export function initClientNavigation(opts = {}) {
41
- const options = {
42
- onNavigate: async function onNavigate() {
43
- // @ts-expect-error
44
- await globalThis.__rsc_callServer();
45
- },
46
- scrollToTop: true,
47
- scrollBehavior: "instant",
48
- ...opts,
49
- };
67
+ IS_CLIENT_NAVIGATION = true;
50
68
  history.scrollRestoration = "auto";
51
69
  document.addEventListener("click", async function handleClickEvent(event) {
52
- // Prevent default navigation
53
70
  if (!validateClickEvent(event, event.target)) {
54
71
  return;
55
72
  }
@@ -57,28 +74,18 @@ export function initClientNavigation(opts = {}) {
57
74
  const el = event.target;
58
75
  const a = el.closest("a");
59
76
  const href = a?.getAttribute("href");
60
- saveScrollPosition(window.scrollX, window.scrollY);
61
- window.history.pushState({ path: href }, "", window.location.origin + href);
62
- await options.onNavigate();
63
- if (options.scrollToTop && history.scrollRestoration === "auto") {
64
- window.scrollTo({
65
- top: 0,
66
- left: 0,
67
- behavior: options.scrollBehavior,
68
- });
69
- saveScrollPosition(0, 0);
70
- }
71
- history.scrollRestoration = "auto";
77
+ navigate(href);
72
78
  }, true);
73
79
  window.addEventListener("popstate", async function handlePopState() {
74
- saveScrollPosition(window.scrollX, window.scrollY);
75
- await options.onNavigate();
80
+ // @ts-expect-error
81
+ await globalThis.__rsc_callServer();
76
82
  });
77
83
  // Return a handleResponse function for use with initClient
78
84
  return {
79
85
  handleResponse: function handleResponse(response) {
80
86
  if (!response.ok) {
81
87
  // Redirect to the current page (window.location) to show the error
88
+ // This means the page that produced the error is called twice.
82
89
  window.location.href = window.location.href;
83
90
  return false;
84
91
  }
@@ -1,2 +1,3 @@
1
1
  import "./types/ssr";
2
2
  export * from "../lib/streams/consumeEventStream";
3
+ export declare const navigate: () => void;
@@ -1,2 +1,5 @@
1
1
  import "./types/ssr";
2
2
  export * from "../lib/streams/consumeEventStream";
3
+ export const navigate = () => {
4
+ /* stub */
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.5",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {