@backtickjs/expo-router 0.0.0-stage → 0.3.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jean-François Bisson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
- # Temporary Holding Version
1
+ # @backtickjs/expo-router
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ expo-router as the Backtick app runs it: the same API and types, with a `Link` that fetches its href ahead while its screen is on top, and as it's pressed, as Next.js's `Link` prefetches every link it shows. The Backtick app serves it as `expo-router`, so apps import `expo-router` as usual and never write `prefetch`.
4
+
5
+ The fetch is data only: expo-router never preloads a route for it. `prefetch={false}` turns it off. An app sets how a `Link` fetches with `PrefetchContext`; without it, a `Link` is expo-router's.
6
+
7
+ ## License
8
+
9
+ MIT
@@ -0,0 +1,17 @@
1
+ import { Link as ExpoLink, router as expoRouter, useRouter as useExpoRouter } from "expo-router";
2
+ import { type ReactNode } from "react";
3
+ export * from "expo-router";
4
+ type Prefetch = (href: string, page: boolean) => void;
5
+ /**
6
+ * How links and `router.prefetch` under it prefetch, set by the app
7
+ * serving this module: `prefetch` is the page too.
8
+ */
9
+ export declare function PrefetchProvider(props: {
10
+ value: Prefetch;
11
+ children: ReactNode;
12
+ }): import("react").FunctionComponentElement<import("react").ProviderProps<Prefetch | undefined>>;
13
+ export declare const Link: typeof ExpoLink;
14
+ type Router = typeof expoRouter;
15
+ export declare const router: Router;
16
+ /** expo-router's `useRouter`, whose `prefetch` prefetches for its app. */
17
+ export declare function useRouter(): ReturnType<typeof useExpoRouter>;
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ import { Link as ExpoLink, router as expoRouter, useIsFocused, useRouter as useExpoRouter, } from "expo-router";
2
+ import { resolveHref } from "expo-router/build/link/href";
3
+ import { createContext, createElement, use, useEffect, useMemo, } from "react";
4
+ export * from "expo-router";
5
+ // How a Link fetches its href ahead (with its page, for `prefetch`).
6
+ const PrefetchContext = createContext(undefined);
7
+ // The provider's fetch, which every Link and useRouter needs: the app serving
8
+ // this module always sets one.
9
+ function usePrefetch() {
10
+ const prefetch = use(PrefetchContext);
11
+ if (prefetch === undefined) {
12
+ throw new Error("@backtickjs/expo-router needs a PrefetchProvider above it.");
13
+ }
14
+ return prefetch;
15
+ }
16
+ // The fetch of the app on top, for `router.prefetch`, which has no screen.
17
+ let prefetchOnTop;
18
+ /**
19
+ * How links and `router.prefetch` under it prefetch, set by the app
20
+ * serving this module: `prefetch` is the page too.
21
+ */
22
+ export function PrefetchProvider(props) {
23
+ const focused = useIsFocused();
24
+ useEffect(() => {
25
+ if (!focused) {
26
+ return;
27
+ }
28
+ prefetchOnTop = props.value;
29
+ return () => {
30
+ if (prefetchOnTop === props.value) {
31
+ prefetchOnTop = undefined;
32
+ }
33
+ };
34
+ }, [focused, props.value]);
35
+ return createElement(PrefetchContext, { value: props.value }, props.children);
36
+ }
37
+ // expo-router's Link, which fetches its href ahead while its screen is on
38
+ // top, and again as it's pressed, as Next.js's prefetches every link it
39
+ // shows: what its screen needs to show, and its page too with `prefetch`;
40
+ // `prefetch={false}` turns it off. It never has expo-router preload a route:
41
+ // the fetch is data only.
42
+ function PrefetchingLink({ prefetch, onPressIn, ...props }) {
43
+ const prefetchHref = usePrefetch();
44
+ const focused = useIsFocused();
45
+ const href = resolveHref(props.href);
46
+ const enabled = prefetch !== false;
47
+ const page = prefetch === true;
48
+ useEffect(() => {
49
+ if (enabled && focused) {
50
+ prefetchHref(href, page);
51
+ }
52
+ }, [enabled, focused, prefetchHref, href, page]);
53
+ return createElement(ExpoLink, {
54
+ ...props,
55
+ onPressIn: (event) => {
56
+ if (enabled) {
57
+ prefetchHref(href, page);
58
+ }
59
+ onPressIn?.(event);
60
+ },
61
+ });
62
+ }
63
+ export const Link = Object.assign(PrefetchingLink, ExpoLink);
64
+ // The router's `prefetch`, as a Link without `prefetch` prefetches: for the
65
+ // app on top, and with none (a moment at startup), nothing. Never a preload.
66
+ const prefetch = (href) => prefetchOnTop?.(resolveHref(href), false);
67
+ // expo-router's router, with that `prefetch`. Read through on each access:
68
+ // expo-router binds the router's methods once it renders.
69
+ export const router = new Proxy(expoRouter, {
70
+ get(target, key, receiver) {
71
+ return key === "prefetch" ? prefetch : Reflect.get(target, key, receiver);
72
+ },
73
+ });
74
+ /** expo-router's `useRouter`, whose `prefetch` prefetches for its app. */
75
+ export function useRouter() {
76
+ const actions = useExpoRouter();
77
+ const prefetchHref = usePrefetch();
78
+ return useMemo(() => ({
79
+ ...actions,
80
+ prefetch: (href) => prefetchHref(resolveHref(href), false),
81
+ }), [actions, prefetchHref]);
82
+ }
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index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AAU,EAAE;IAClD,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ;QACvB,OAAO,GAAG,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC5E,CAAC;CACF,CAAC,CAAC;AAEH,0EAA0E;AAC1E,MAAM,UAAU,SAAS;IACvB,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC;IAChC,MAAM,YAAY,GAAG,WAAW,EAAE,CAAC;IACnC,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC;QACL,GAAG,OAAO;QACV,QAAQ,EAAE,CAAC,IAAuC,EAAE,EAAE,CACpD,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC;KACzC,CAAC,EACF,CAAC,OAAO,EAAE,YAAY,CAAC,CACxB,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@backtickjs/expo-router",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.0",
4
+ "description": "expo-router as the Backtick app runs it: every Link fetches its screen's loading state ahead, as Next.js's does",
5
+ "keywords": [
6
+ "backtick",
7
+ "expo-router",
8
+ "react-native",
9
+ "server-driven-ui"
10
+ ],
11
+ "homepage": "https://backtickjs.com",
12
+ "bugs": {
13
+ "url": "https://github.com/backtickjs/backtick/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/backtickjs/backtick.git",
18
+ "directory": "packages/expo-router"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Jean-François Bisson <jf@backtickjs.com>",
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "!dist/._cache",
32
+ "src"
33
+ ],
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "peerDependencies": {
38
+ "expo-router": "~58.0.30",
39
+ "react": "*"
40
+ },
41
+ "devDependencies": {
42
+ "@types/react": "19.3.0",
43
+ "expo-router": "~58.0.30",
44
+ "prettier": "^3.6.2",
45
+ "react": "19.3.0",
46
+ "typescript": "^6.0.3"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc -b",
50
+ "dev": "tsc -b --watch",
51
+ "format": "prettier --write --ignore-unknown .",
52
+ "format:check": "prettier --check --ignore-unknown .",
53
+ "clean": "rm -rf node_modules dist .turbo"
54
+ }
6
55
  }
package/src/index.ts ADDED
@@ -0,0 +1,120 @@
1
+ import {
2
+ Link as ExpoLink,
3
+ router as expoRouter,
4
+ useIsFocused,
5
+ useRouter as useExpoRouter,
6
+ } from "expo-router";
7
+ import { resolveHref } from "expo-router/build/link/href";
8
+ import {
9
+ createContext,
10
+ createElement,
11
+ type ReactNode,
12
+ use,
13
+ useEffect,
14
+ useMemo,
15
+ } from "react";
16
+
17
+ export * from "expo-router";
18
+
19
+ type Prefetch = (href: string, page: boolean) => void;
20
+
21
+ // How a Link fetches its href ahead (with its page, for `prefetch`).
22
+ const PrefetchContext = createContext<Prefetch | undefined>(undefined);
23
+
24
+ // The provider's fetch, which every Link and useRouter needs: the app serving
25
+ // this module always sets one.
26
+ function usePrefetch(): Prefetch {
27
+ const prefetch = use(PrefetchContext);
28
+ if (prefetch === undefined) {
29
+ throw new Error(
30
+ "@backtickjs/expo-router needs a PrefetchProvider above it.",
31
+ );
32
+ }
33
+ return prefetch;
34
+ }
35
+
36
+ // The fetch of the app on top, for `router.prefetch`, which has no screen.
37
+ let prefetchOnTop: Prefetch | undefined;
38
+
39
+ /**
40
+ * How links and `router.prefetch` under it prefetch, set by the app
41
+ * serving this module: `prefetch` is the page too.
42
+ */
43
+ export function PrefetchProvider(props: {
44
+ value: Prefetch;
45
+ children: ReactNode;
46
+ }) {
47
+ const focused = useIsFocused();
48
+ useEffect(() => {
49
+ if (!focused) {
50
+ return;
51
+ }
52
+ prefetchOnTop = props.value;
53
+ return () => {
54
+ if (prefetchOnTop === props.value) {
55
+ prefetchOnTop = undefined;
56
+ }
57
+ };
58
+ }, [focused, props.value]);
59
+ return createElement(PrefetchContext, { value: props.value }, props.children);
60
+ }
61
+
62
+ type LinkProps = Parameters<typeof ExpoLink>[0];
63
+
64
+ // expo-router's Link, which fetches its href ahead while its screen is on
65
+ // top, and again as it's pressed, as Next.js's prefetches every link it
66
+ // shows: what its screen needs to show, and its page too with `prefetch`;
67
+ // `prefetch={false}` turns it off. It never has expo-router preload a route:
68
+ // the fetch is data only.
69
+ function PrefetchingLink({ prefetch, onPressIn, ...props }: LinkProps) {
70
+ const prefetchHref = usePrefetch();
71
+ const focused = useIsFocused();
72
+ const href = resolveHref(props.href);
73
+ const enabled = prefetch !== false;
74
+ const page = prefetch === true;
75
+ useEffect(() => {
76
+ if (enabled && focused) {
77
+ prefetchHref(href, page);
78
+ }
79
+ }, [enabled, focused, prefetchHref, href, page]);
80
+ return createElement(ExpoLink, {
81
+ ...props,
82
+ onPressIn: (event) => {
83
+ if (enabled) {
84
+ prefetchHref(href, page);
85
+ }
86
+ onPressIn?.(event);
87
+ },
88
+ });
89
+ }
90
+
91
+ export const Link = Object.assign(PrefetchingLink, ExpoLink) as typeof ExpoLink;
92
+
93
+ type Router = typeof expoRouter;
94
+
95
+ // The router's `prefetch`, as a Link without `prefetch` prefetches: for the
96
+ // app on top, and with none (a moment at startup), nothing. Never a preload.
97
+ const prefetch: Router["prefetch"] = (href) =>
98
+ prefetchOnTop?.(resolveHref(href), false);
99
+
100
+ // expo-router's router, with that `prefetch`. Read through on each access:
101
+ // expo-router binds the router's methods once it renders.
102
+ export const router: Router = new Proxy(expoRouter, {
103
+ get(target, key, receiver) {
104
+ return key === "prefetch" ? prefetch : Reflect.get(target, key, receiver);
105
+ },
106
+ });
107
+
108
+ /** expo-router's `useRouter`, whose `prefetch` prefetches for its app. */
109
+ export function useRouter(): ReturnType<typeof useExpoRouter> {
110
+ const actions = useExpoRouter();
111
+ const prefetchHref = usePrefetch();
112
+ return useMemo(
113
+ () => ({
114
+ ...actions,
115
+ prefetch: (href: Parameters<Router["prefetch"]>[0]) =>
116
+ prefetchHref(resolveHref(href), false),
117
+ }),
118
+ [actions, prefetchHref],
119
+ );
120
+ }