-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixing deferred registrations with protected data (#135)
* Fixed deferred registrations with protected loaded * Added changeset files * Removed dead code
- Loading branch information
1 parent
48edb89
commit 8e73083
Showing
17 changed files
with
155 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@squide/firefly": patch | ||
--- | ||
|
||
Fixing a remaining issue with deferred registrations that depends on protected data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@squide/react-router": patch | ||
--- | ||
|
||
Internal changes to `useRouteMatch` and `useIsRouteProtected`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
order: -100 | ||
toc: | ||
depth: 2-3 | ||
--- | ||
|
||
# useIsRouteProtected | ||
|
||
Determine whether or not a route is considered as `protected`. | ||
|
||
> To take advantage of this hook, make sure to add a [$visibility hint](../runtime/runtime-class.md#register-a-public-route) to your public pages. | ||
## Reference | ||
|
||
```ts | ||
const isProtected = useIsRouteProtected(route) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `route`: A `Route` object. | ||
|
||
### Returns | ||
|
||
A `boolean` value indicating whether or not the matching route is `protected`. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useLocation } from "react-router-dom"; | ||
import { useIsRouteProtected, useRouteMatch } from "@squide/firefly"; | ||
|
||
const location = useLocation(); | ||
const route = useRouteMatch(location); | ||
|
||
const isActiveRouteProtected = useIsRouteProtected(route); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function NoMatchRouteFallback() { | ||
return ( | ||
<div> | ||
<h1>404 not found</h1> | ||
<p>This page has been dynamically added by Squide to fix an issue with the <code>AppRouter</code> component. <strong>Please replace this page in your application by a <a href="https://reactrouter.com/en/main/start/tutorial#handling-not-found-errors">custom match page</a>.</strong></p> | ||
<p>The code should be like the following:</p> | ||
<pre style={{ backgroundColor: "rgb(243, 245, 249)", color: "rgb(21, 25, 40)", padding: "4px 10px", border: "1px solid rgb(225, 229, 239)" }}> | ||
{`runtime.registerRoute({ | ||
$visibility: "public", | ||
path: "*", | ||
lazy: import("./NoMatchPage.tsx") | ||
});`} | ||
</pre> | ||
</div> | ||
) | ||
} | ||
|
||
export const Component = NoMatchRouteFallback; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Route } from "./routeRegistry.ts"; | ||
|
||
export function useIsRouteProtected(route?: Route) { | ||
if (!route) { | ||
// HACK: | ||
// An unregistrered route will be considered as "protected" by default to facilitate the implementation of deferred routes. | ||
// The issue is that when there's a direct hit on a deferred route, it cannot be determined whether or not a deferred route is public or protected | ||
// because the deferred route hasn't been registered yet (since it's a deferred route). | ||
// If that deferred route depends on protected data, if we don't return "true" here, the deferred route will be registered without providing the protected data | ||
// which will cause a runtime error. | ||
return true; | ||
} | ||
|
||
if (route.path === "*") { | ||
// HACK: | ||
// With the current AppRouter implementation, when there's a direct hit to a deferred route, since the route has not been registered yet to | ||
// the React Router router instance, the router is trying to render the no match route which confuse this hook as it would return a boolean value | ||
// for the visibility of the no match route (which is usually public) rather than the visiblity of the route asked by the consumer (which may be | ||
// protected). To circumvent this issue, true is returned for no match route. Anyway, that would really make sense to direct hit the no match route. | ||
return true; | ||
} | ||
|
||
return route.$visibility === "protected"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { RefObject, useCallback, useRef } from "react"; | ||
|
||
export function useRefState<T>(initialValue?: T): [RefObject<T | undefined>, (newValue: T) => void] { | ||
const valueRef = useRef<T | undefined>(initialValue); | ||
|
||
const setValue = useCallback((newValue: T) => { | ||
if (valueRef.current !== newValue) { | ||
valueRef.current = newValue; | ||
} | ||
}, [valueRef]); | ||
|
||
return [valueRef, setValue]; | ||
} |