Skip to content

Commit 1c21a0f

Browse files
committed
Merge branch 'dev' into release-6.4.3
2 parents ac7e403 + 2d4addb commit 1c21a0f

File tree

8 files changed

+77
-3
lines changed

8 files changed

+77
-3
lines changed

.changeset/green-dragons-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
fix `createURL` in local file execution in Firefox ([#9464](https://github.com/remix-run/react-router/pull/9464))

.changeset/nine-apricots-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
`useRoutes` should be able to return `null` when passing `locationArg` ([#9485](https://github.com/remix-run/react-router/pull/9485))

.changeset/odd-kids-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
fix initialEntries type in createMemoryRouter

contributors.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- abdallah-nour
22
- abhi-kr-2100
33
- adamdotjs
4+
- AchThomas
45
- Ajayff4
56
- alany411
67
- alexlbr
@@ -27,6 +28,7 @@
2728
- codeape2
2829
- coryhouse
2930
- cvbuelow
31+
- danielberndt
3032
- dauletbaev
3133
- david-crespo
3234
- dokeet

packages/react-router/__tests__/useRoutes-test.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,57 @@ describe("useRoutes", () => {
7575
`);
7676
});
7777

78+
it("returns null when no route matches", () => {
79+
let routes = [{ path: "one", element: <h1>one</h1> }];
80+
81+
const NullRenderer = (props: { routes: RouteObject[] }) => {
82+
const element = useRoutes(props.routes);
83+
return element === null ? <div>is null</div> : <div>is not null</div>;
84+
};
85+
86+
let renderer: TestRenderer.ReactTestRenderer;
87+
TestRenderer.act(() => {
88+
renderer = TestRenderer.create(
89+
<MemoryRouter initialEntries={["/two"]}>
90+
<NullRenderer routes={routes} />
91+
</MemoryRouter>
92+
);
93+
});
94+
95+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
96+
<div>
97+
is null
98+
</div>
99+
`);
100+
});
101+
102+
it("returns null when no route matches and a `location` prop is passed", () => {
103+
let routes = [{ path: "one", element: <h1>one</h1> }];
104+
105+
const NullRenderer = (props: {
106+
routes: RouteObject[];
107+
location?: Partial<Location> & { pathname: string };
108+
}) => {
109+
const element = useRoutes(props.routes, props.location);
110+
return element === null ? <div>is null</div> : <div>is not null</div>;
111+
};
112+
113+
let renderer: TestRenderer.ReactTestRenderer;
114+
TestRenderer.act(() => {
115+
renderer = TestRenderer.create(
116+
<MemoryRouter initialEntries={["/two"]}>
117+
<NullRenderer routes={routes} location={{ pathname: "/three" }} />
118+
</MemoryRouter>
119+
);
120+
});
121+
122+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
123+
<div>
124+
is null
125+
</div>
126+
`);
127+
});
128+
78129
describe("warns", () => {
79130
let consoleWarn: jest.SpyInstance;
80131
beforeEach(() => {

packages/react-router/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
Router as RemixRouter,
1818
ShouldRevalidateFunction,
1919
To,
20+
InitialEntry,
2021
} from "@remix-run/router";
2122
import {
2223
AbortedDeferredError,
@@ -203,7 +204,7 @@ export function createMemoryRouter(
203204
opts?: {
204205
basename?: string;
205206
hydrationData?: HydrationState;
206-
initialEntries?: string[];
207+
initialEntries?: InitialEntry[];
207208
initialIndex?: number;
208209
}
209210
): RemixRouter {

packages/react-router/lib/hooks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export function useRoutes(
415415
// When a user passes in a `locationArg`, the associated routes need to
416416
// be wrapped in a new `LocationContext.Provider` in order for `useLocation`
417417
// to use the scoped location instead of the global location.
418-
if (locationArg) {
418+
if (locationArg && renderedMatches) {
419419
return (
420420
<LocationContext.Provider
421421
value={{

packages/router/history.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,13 @@ export function parsePath(path: string): Partial<Path> {
540540
}
541541

542542
export function createURL(location: Location | string): URL {
543+
// window.location.origin is "null" (the literal string value) in Firefox
544+
// under certain conditions, notably when serving from a local HTML file
545+
// See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
543546
let base =
544-
typeof window !== "undefined" && typeof window.location !== "undefined"
547+
typeof window !== "undefined" &&
548+
typeof window.location !== "undefined" &&
549+
window.location.origin !== "null"
545550
? window.location.origin
546551
: "unknown://unknown";
547552
let href = typeof location === "string" ? location : createPath(location);

0 commit comments

Comments
 (0)