@remix-run/router 0.0.0-experimental-48058118

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.
package/history.ts ADDED
@@ -0,0 +1,721 @@
1
+ ////////////////////////////////////////////////////////////////////////////////
2
+ //#region Types and Constants
3
+ ////////////////////////////////////////////////////////////////////////////////
4
+
5
+ /**
6
+ * Actions represent the type of change to a location value.
7
+ */
8
+ export enum Action {
9
+ /**
10
+ * A POP indicates a change to an arbitrary index in the history stack, such
11
+ * as a back or forward navigation. It does not describe the direction of the
12
+ * navigation, only that the current index changed.
13
+ *
14
+ * Note: This is the default action for newly created history objects.
15
+ */
16
+ Pop = "POP",
17
+
18
+ /**
19
+ * A PUSH indicates a new entry being added to the history stack, such as when
20
+ * a link is clicked and a new page loads. When this happens, all subsequent
21
+ * entries in the stack are lost.
22
+ */
23
+ Push = "PUSH",
24
+
25
+ /**
26
+ * A REPLACE indicates the entry at the current index in the history stack
27
+ * being replaced by a new one.
28
+ */
29
+ Replace = "REPLACE",
30
+ }
31
+
32
+ /**
33
+ * The pathname, search, and hash values of a URL.
34
+ */
35
+ export interface Path {
36
+ /**
37
+ * A URL pathname, beginning with a /.
38
+ */
39
+ pathname: string;
40
+
41
+ /**
42
+ * A URL search string, beginning with a ?.
43
+ */
44
+ search: string;
45
+
46
+ /**
47
+ * A URL fragment identifier, beginning with a #.
48
+ */
49
+ hash: string;
50
+ }
51
+
52
+ /**
53
+ * An entry in a history stack. A location contains information about the
54
+ * URL path, as well as possibly some arbitrary state and a key.
55
+ */
56
+ export interface Location extends Path {
57
+ /**
58
+ * A value of arbitrary data associated with this location.
59
+ */
60
+ state: any;
61
+
62
+ /**
63
+ * A unique string associated with this location. May be used to safely store
64
+ * and retrieve data in some other storage API, like `localStorage`.
65
+ *
66
+ * Note: This value is always "default" on the initial location.
67
+ */
68
+ key: string;
69
+ }
70
+
71
+ /**
72
+ * A change to the current location.
73
+ */
74
+ export interface Update {
75
+ /**
76
+ * The action that triggered the change.
77
+ */
78
+ action: Action;
79
+
80
+ /**
81
+ * The new location.
82
+ */
83
+ location: Location;
84
+
85
+ /**
86
+ * The delta between this location and the former location in the history stack
87
+ */
88
+ delta: number | null;
89
+ }
90
+
91
+ /**
92
+ * A function that receives notifications about location changes.
93
+ */
94
+ export interface Listener {
95
+ (update: Update): void;
96
+ }
97
+
98
+ /**
99
+ * Describes a location that is the destination of some navigation, either via
100
+ * `history.push` or `history.replace`. May be either a URL or the pieces of a
101
+ * URL path.
102
+ */
103
+ export type To = string | Partial<Path>;
104
+
105
+ /**
106
+ * A history is an interface to the navigation stack. The history serves as the
107
+ * source of truth for the current location, as well as provides a set of
108
+ * methods that may be used to change it.
109
+ *
110
+ * It is similar to the DOM's `window.history` object, but with a smaller, more
111
+ * focused API.
112
+ */
113
+ export interface History {
114
+ /**
115
+ * The last action that modified the current location. This will always be
116
+ * Action.Pop when a history instance is first created. This value is mutable.
117
+ */
118
+ readonly action: Action;
119
+
120
+ /**
121
+ * The current location. This value is mutable.
122
+ */
123
+ readonly location: Location;
124
+
125
+ /**
126
+ * Returns a valid href for the given `to` value that may be used as
127
+ * the value of an <a href> attribute.
128
+ *
129
+ * @param to - The destination URL
130
+ */
131
+ createHref(to: To): string;
132
+
133
+ /**
134
+ * Returns a URL for the given `to` value
135
+ *
136
+ * @param to - The destination URL
137
+ */
138
+ createURL(to: To): URL;
139
+
140
+ /**
141
+ * Encode a location the same way window.history would do (no-op for memory
142
+ * history) so we ensure our PUSH/REPLACE navigations for data routers
143
+ * behave the same as POP
144
+ *
145
+ * @param to Unencoded path
146
+ */
147
+ encodeLocation(to: To): Path;
148
+
149
+ /**
150
+ * Pushes a new location onto the history stack, increasing its length by one.
151
+ * If there were any entries in the stack after the current one, they are
152
+ * lost.
153
+ *
154
+ * @param to - The new URL
155
+ * @param state - Data to associate with the new location
156
+ */
157
+ push(to: To, state?: any): void;
158
+
159
+ /**
160
+ * Replaces the current location in the history stack with a new one. The
161
+ * location that was replaced will no longer be available.
162
+ *
163
+ * @param to - The new URL
164
+ * @param state - Data to associate with the new location
165
+ */
166
+ replace(to: To, state?: any): void;
167
+
168
+ /**
169
+ * Navigates `n` entries backward/forward in the history stack relative to the
170
+ * current index. For example, a "back" navigation would use go(-1).
171
+ *
172
+ * @param delta - The delta in the stack index
173
+ */
174
+ go(delta: number): void;
175
+
176
+ /**
177
+ * Sets up a listener that will be called whenever the current location
178
+ * changes.
179
+ *
180
+ * @param listener - A function that will be called when the location changes
181
+ * @returns unlisten - A function that may be used to stop listening
182
+ */
183
+ listen(listener: Listener): () => void;
184
+ }
185
+
186
+ type HistoryState = {
187
+ usr: any;
188
+ key?: string;
189
+ idx: number;
190
+ };
191
+
192
+ const PopStateEventType = "popstate";
193
+ //#endregion
194
+
195
+ ////////////////////////////////////////////////////////////////////////////////
196
+ //#region Memory History
197
+ ////////////////////////////////////////////////////////////////////////////////
198
+
199
+ /**
200
+ * A user-supplied object that describes a location. Used when providing
201
+ * entries to `createMemoryHistory` via its `initialEntries` option.
202
+ */
203
+ export type InitialEntry = string | Partial<Location>;
204
+
205
+ export type MemoryHistoryOptions = {
206
+ initialEntries?: InitialEntry[];
207
+ initialIndex?: number;
208
+ v5Compat?: boolean;
209
+ };
210
+
211
+ /**
212
+ * A memory history stores locations in memory. This is useful in stateful
213
+ * environments where there is no web browser, such as node tests or React
214
+ * Native.
215
+ */
216
+ export interface MemoryHistory extends History {
217
+ /**
218
+ * The current index in the history stack.
219
+ */
220
+ readonly index: number;
221
+ }
222
+
223
+ /**
224
+ * Memory history stores the current location in memory. It is designed for use
225
+ * in stateful non-browser environments like tests and React Native.
226
+ */
227
+ export function createMemoryHistory(
228
+ options: MemoryHistoryOptions = {}
229
+ ): MemoryHistory {
230
+ let { initialEntries = ["/"], initialIndex, v5Compat = false } = options;
231
+ let entries: Location[]; // Declare so we can access from createMemoryLocation
232
+ entries = initialEntries.map((entry, index) =>
233
+ createMemoryLocation(
234
+ entry,
235
+ typeof entry === "string" ? null : entry.state,
236
+ index === 0 ? "default" : undefined
237
+ )
238
+ );
239
+ let index = clampIndex(
240
+ initialIndex == null ? entries.length - 1 : initialIndex
241
+ );
242
+ let action = Action.Pop;
243
+ let listener: Listener | null = null;
244
+
245
+ function clampIndex(n: number): number {
246
+ return Math.min(Math.max(n, 0), entries.length - 1);
247
+ }
248
+ function getCurrentLocation(): Location {
249
+ return entries[index];
250
+ }
251
+ function createMemoryLocation(
252
+ to: To,
253
+ state: any = null,
254
+ key?: string
255
+ ): Location {
256
+ let location = createLocation(
257
+ entries ? getCurrentLocation().pathname : "/",
258
+ to,
259
+ state,
260
+ key
261
+ );
262
+ warning(
263
+ location.pathname.charAt(0) === "/",
264
+ `relative pathnames are not supported in memory history: ${JSON.stringify(
265
+ to
266
+ )}`
267
+ );
268
+ return location;
269
+ }
270
+
271
+ function createHref(to: To) {
272
+ return typeof to === "string" ? to : createPath(to);
273
+ }
274
+
275
+ let history: MemoryHistory = {
276
+ get index() {
277
+ return index;
278
+ },
279
+ get action() {
280
+ return action;
281
+ },
282
+ get location() {
283
+ return getCurrentLocation();
284
+ },
285
+ createHref,
286
+ createURL(to) {
287
+ return new URL(createHref(to), "http://localhost");
288
+ },
289
+ encodeLocation(to: To) {
290
+ let path = typeof to === "string" ? parsePath(to) : to;
291
+ return {
292
+ pathname: path.pathname || "",
293
+ search: path.search || "",
294
+ hash: path.hash || "",
295
+ };
296
+ },
297
+ push(to, state) {
298
+ action = Action.Push;
299
+ let nextLocation = createMemoryLocation(to, state);
300
+ index += 1;
301
+ entries.splice(index, entries.length, nextLocation);
302
+ if (v5Compat && listener) {
303
+ listener({ action, location: nextLocation, delta: 1 });
304
+ }
305
+ },
306
+ replace(to, state) {
307
+ action = Action.Replace;
308
+ let nextLocation = createMemoryLocation(to, state);
309
+ entries[index] = nextLocation;
310
+ if (v5Compat && listener) {
311
+ listener({ action, location: nextLocation, delta: 0 });
312
+ }
313
+ },
314
+ go(delta) {
315
+ action = Action.Pop;
316
+ let nextIndex = clampIndex(index + delta);
317
+ let nextLocation = entries[nextIndex];
318
+ index = nextIndex;
319
+ if (listener) {
320
+ listener({ action, location: nextLocation, delta });
321
+ }
322
+ },
323
+ listen(fn: Listener) {
324
+ listener = fn;
325
+ return () => {
326
+ listener = null;
327
+ };
328
+ },
329
+ };
330
+
331
+ return history;
332
+ }
333
+ //#endregion
334
+
335
+ ////////////////////////////////////////////////////////////////////////////////
336
+ //#region Browser History
337
+ ////////////////////////////////////////////////////////////////////////////////
338
+
339
+ /**
340
+ * A browser history stores the current location in regular URLs in a web
341
+ * browser environment. This is the standard for most web apps and provides the
342
+ * cleanest URLs the browser's address bar.
343
+ *
344
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
345
+ */
346
+ export interface BrowserHistory extends UrlHistory {}
347
+
348
+ export type BrowserHistoryOptions = UrlHistoryOptions;
349
+
350
+ /**
351
+ * Browser history stores the location in regular URLs. This is the standard for
352
+ * most web apps, but it requires some configuration on the server to ensure you
353
+ * serve the same app at multiple URLs.
354
+ *
355
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
356
+ */
357
+ export function createBrowserHistory(
358
+ options: BrowserHistoryOptions = {}
359
+ ): BrowserHistory {
360
+ function createBrowserLocation(
361
+ window: Window,
362
+ globalHistory: Window["history"]
363
+ ) {
364
+ let { pathname, search, hash } = window.location;
365
+ return createLocation(
366
+ "",
367
+ { pathname, search, hash },
368
+ // state defaults to `null` because `window.history.state` does
369
+ (globalHistory.state && globalHistory.state.usr) || null,
370
+ (globalHistory.state && globalHistory.state.key) || "default"
371
+ );
372
+ }
373
+
374
+ function createBrowserHref(window: Window, to: To) {
375
+ return typeof to === "string" ? to : createPath(to);
376
+ }
377
+
378
+ return getUrlBasedHistory(
379
+ createBrowserLocation,
380
+ createBrowserHref,
381
+ null,
382
+ options
383
+ );
384
+ }
385
+ //#endregion
386
+
387
+ ////////////////////////////////////////////////////////////////////////////////
388
+ //#region Hash History
389
+ ////////////////////////////////////////////////////////////////////////////////
390
+
391
+ /**
392
+ * A hash history stores the current location in the fragment identifier portion
393
+ * of the URL in a web browser environment.
394
+ *
395
+ * This is ideal for apps that do not control the server for some reason
396
+ * (because the fragment identifier is never sent to the server), including some
397
+ * shared hosting environments that do not provide fine-grained controls over
398
+ * which pages are served at which URLs.
399
+ *
400
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
401
+ */
402
+ export interface HashHistory extends UrlHistory {}
403
+
404
+ export type HashHistoryOptions = UrlHistoryOptions;
405
+
406
+ /**
407
+ * Hash history stores the location in window.location.hash. This makes it ideal
408
+ * for situations where you don't want to send the location to the server for
409
+ * some reason, either because you do cannot configure it or the URL space is
410
+ * reserved for something else.
411
+ *
412
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
413
+ */
414
+ export function createHashHistory(
415
+ options: HashHistoryOptions = {}
416
+ ): HashHistory {
417
+ function createHashLocation(
418
+ window: Window,
419
+ globalHistory: Window["history"]
420
+ ) {
421
+ let {
422
+ pathname = "/",
423
+ search = "",
424
+ hash = "",
425
+ } = parsePath(window.location.hash.substr(1));
426
+ return createLocation(
427
+ "",
428
+ { pathname, search, hash },
429
+ // state defaults to `null` because `window.history.state` does
430
+ (globalHistory.state && globalHistory.state.usr) || null,
431
+ (globalHistory.state && globalHistory.state.key) || "default"
432
+ );
433
+ }
434
+
435
+ function createHashHref(window: Window, to: To) {
436
+ let base = window.document.querySelector("base");
437
+ let href = "";
438
+
439
+ if (base && base.getAttribute("href")) {
440
+ let url = window.location.href;
441
+ let hashIndex = url.indexOf("#");
442
+ href = hashIndex === -1 ? url : url.slice(0, hashIndex);
443
+ }
444
+
445
+ return href + "#" + (typeof to === "string" ? to : createPath(to));
446
+ }
447
+
448
+ function validateHashLocation(location: Location, to: To) {
449
+ warning(
450
+ location.pathname.charAt(0) === "/",
451
+ `relative pathnames are not supported in hash history.push(${JSON.stringify(
452
+ to
453
+ )})`
454
+ );
455
+ }
456
+
457
+ return getUrlBasedHistory(
458
+ createHashLocation,
459
+ createHashHref,
460
+ validateHashLocation,
461
+ options
462
+ );
463
+ }
464
+ //#endregion
465
+
466
+ ////////////////////////////////////////////////////////////////////////////////
467
+ //#region UTILS
468
+ ////////////////////////////////////////////////////////////////////////////////
469
+
470
+ /**
471
+ * @private
472
+ */
473
+ export function invariant(value: boolean, message?: string): asserts value;
474
+ export function invariant<T>(
475
+ value: T | null | undefined,
476
+ message?: string
477
+ ): asserts value is T;
478
+ export function invariant(value: any, message?: string) {
479
+ if (value === false || value === null || typeof value === "undefined") {
480
+ throw new Error(message);
481
+ }
482
+ }
483
+
484
+ function warning(cond: any, message: string) {
485
+ if (!cond) {
486
+ // eslint-disable-next-line no-console
487
+ if (typeof console !== "undefined") console.warn(message);
488
+
489
+ try {
490
+ // Welcome to debugging history!
491
+ //
492
+ // This error is thrown as a convenience so you can more easily
493
+ // find the source for a warning that appears in the console by
494
+ // enabling "pause on exceptions" in your JavaScript debugger.
495
+ throw new Error(message);
496
+ // eslint-disable-next-line no-empty
497
+ } catch (e) {}
498
+ }
499
+ }
500
+
501
+ function createKey() {
502
+ return Math.random().toString(36).substr(2, 8);
503
+ }
504
+
505
+ /**
506
+ * For browser-based histories, we combine the state and key into an object
507
+ */
508
+ function getHistoryState(location: Location, index: number): HistoryState {
509
+ return {
510
+ usr: location.state,
511
+ key: location.key,
512
+ idx: index,
513
+ };
514
+ }
515
+
516
+ /**
517
+ * Creates a Location object with a unique key from the given Path
518
+ */
519
+ export function createLocation(
520
+ current: string | Location,
521
+ to: To,
522
+ state: any = null,
523
+ key?: string
524
+ ): Readonly<Location> {
525
+ let location: Readonly<Location> = {
526
+ pathname: typeof current === "string" ? current : current.pathname,
527
+ search: "",
528
+ hash: "",
529
+ ...(typeof to === "string" ? parsePath(to) : to),
530
+ state,
531
+ // TODO: This could be cleaned up. push/replace should probably just take
532
+ // full Locations now and avoid the need to run through this flow at all
533
+ // But that's a pretty big refactor to the current test suite so going to
534
+ // keep as is for the time being and just let any incoming keys take precedence
535
+ key: (to && (to as Location).key) || key || createKey(),
536
+ };
537
+ return location;
538
+ }
539
+
540
+ /**
541
+ * Creates a string URL path from the given pathname, search, and hash components.
542
+ */
543
+ export function createPath({
544
+ pathname = "/",
545
+ search = "",
546
+ hash = "",
547
+ }: Partial<Path>) {
548
+ if (search && search !== "?")
549
+ pathname += search.charAt(0) === "?" ? search : "?" + search;
550
+ if (hash && hash !== "#")
551
+ pathname += hash.charAt(0) === "#" ? hash : "#" + hash;
552
+ return pathname;
553
+ }
554
+
555
+ /**
556
+ * Parses a string URL path into its separate pathname, search, and hash components.
557
+ */
558
+ export function parsePath(path: string): Partial<Path> {
559
+ let parsedPath: Partial<Path> = {};
560
+
561
+ if (path) {
562
+ let hashIndex = path.indexOf("#");
563
+ if (hashIndex >= 0) {
564
+ parsedPath.hash = path.substr(hashIndex);
565
+ path = path.substr(0, hashIndex);
566
+ }
567
+
568
+ let searchIndex = path.indexOf("?");
569
+ if (searchIndex >= 0) {
570
+ parsedPath.search = path.substr(searchIndex);
571
+ path = path.substr(0, searchIndex);
572
+ }
573
+
574
+ if (path) {
575
+ parsedPath.pathname = path;
576
+ }
577
+ }
578
+
579
+ return parsedPath;
580
+ }
581
+
582
+ export interface UrlHistory extends History {}
583
+
584
+ export type UrlHistoryOptions = {
585
+ window?: Window;
586
+ v5Compat?: boolean;
587
+ };
588
+
589
+ function getUrlBasedHistory(
590
+ getLocation: (window: Window, globalHistory: Window["history"]) => Location,
591
+ createHref: (window: Window, to: To) => string,
592
+ validateLocation: ((location: Location, to: To) => void) | null,
593
+ options: UrlHistoryOptions = {}
594
+ ): UrlHistory {
595
+ let { window = document.defaultView!, v5Compat = false } = options;
596
+ let globalHistory = window.history;
597
+ let action = Action.Pop;
598
+ let listener: Listener | null = null;
599
+
600
+ let index = getIndex()!;
601
+ // Index should only be null when we initialize. If not, it's because the
602
+ // user called history.pushState or history.replaceState directly, in which
603
+ // case we should log a warning as it will result in bugs.
604
+ if (index == null) {
605
+ index = 0;
606
+ globalHistory.replaceState({ ...globalHistory.state, idx: index }, "");
607
+ }
608
+
609
+ function getIndex(): number {
610
+ let state = globalHistory.state || { idx: null };
611
+ return state.idx;
612
+ }
613
+
614
+ function handlePop() {
615
+ action = Action.Pop;
616
+ let nextIndex = getIndex();
617
+ let delta = nextIndex == null ? null : nextIndex - index;
618
+ index = nextIndex;
619
+ if (listener) {
620
+ listener({ action, location: history.location, delta });
621
+ }
622
+ }
623
+
624
+ function push(to: To, state?: any) {
625
+ action = Action.Push;
626
+ let location = createLocation(history.location, to, state);
627
+ if (validateLocation) validateLocation(location, to);
628
+
629
+ index = getIndex() + 1;
630
+ let historyState = getHistoryState(location, index);
631
+ let url = history.createHref(location);
632
+
633
+ // try...catch because iOS limits us to 100 pushState calls :/
634
+ try {
635
+ globalHistory.pushState(historyState, "", url);
636
+ } catch (error) {
637
+ // They are going to lose state here, but there is no real
638
+ // way to warn them about it since the page will refresh...
639
+ window.location.assign(url);
640
+ }
641
+
642
+ if (v5Compat && listener) {
643
+ listener({ action, location: history.location, delta: 1 });
644
+ }
645
+ }
646
+
647
+ function replace(to: To, state?: any) {
648
+ action = Action.Replace;
649
+ let location = createLocation(history.location, to, state);
650
+ if (validateLocation) validateLocation(location, to);
651
+
652
+ index = getIndex();
653
+ let historyState = getHistoryState(location, index);
654
+ let url = history.createHref(location);
655
+ globalHistory.replaceState(historyState, "", url);
656
+
657
+ if (v5Compat && listener) {
658
+ listener({ action, location: history.location, delta: 0 });
659
+ }
660
+ }
661
+
662
+ function createURL(to: To): URL {
663
+ // window.location.origin is "null" (the literal string value) in Firefox
664
+ // under certain conditions, notably when serving from a local HTML file
665
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
666
+ let base =
667
+ window.location.origin !== "null"
668
+ ? window.location.origin
669
+ : window.location.href;
670
+
671
+ let href = typeof to === "string" ? to : createPath(to);
672
+ invariant(
673
+ base,
674
+ `No window.location.(origin|href) available to create URL for href: ${href}`
675
+ );
676
+ return new URL(href, base);
677
+ }
678
+
679
+ let history: History = {
680
+ get action() {
681
+ return action;
682
+ },
683
+ get location() {
684
+ return getLocation(window, globalHistory);
685
+ },
686
+ listen(fn: Listener) {
687
+ if (listener) {
688
+ throw new Error("A history only accepts one active listener");
689
+ }
690
+ window.addEventListener(PopStateEventType, handlePop);
691
+ listener = fn;
692
+
693
+ return () => {
694
+ window.removeEventListener(PopStateEventType, handlePop);
695
+ listener = null;
696
+ };
697
+ },
698
+ createHref(to) {
699
+ return createHref(window, to);
700
+ },
701
+ createURL,
702
+ encodeLocation(to) {
703
+ // Encode a Location the same way window.location would
704
+ let url = createURL(to);
705
+ return {
706
+ pathname: url.pathname,
707
+ search: url.search,
708
+ hash: url.hash,
709
+ };
710
+ },
711
+ push,
712
+ replace,
713
+ go(n) {
714
+ return globalHistory.go(n);
715
+ },
716
+ };
717
+
718
+ return history;
719
+ }
720
+
721
+ //#endregion