-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(router): fallback support in new_defineRouter and new_createPages #1013
Merged
+66
−17
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
745d701
fix: render type test issues with new_createPages
tylersayshi dbf430b
Merge remote-tracking branch 'upstream/main' into refactor-e2e-test-c…
tylersayshi 15931b2
wrong mapping
tylersayshi 0cf6fa6
fix: render type with right mapping
tylersayshi 12c8770
Merge remote-tracking branch 'upstream/main' into refactor-e2e-test-c…
tylersayshi 474adb6
fix: regression to layouts
tylersayshi 48bb258
fix: remove stale comment
tylersayshi bdfec76
refactor: use new_createPages for fs-router
tylersayshi 4500c06
Merge branch 'dai-shi:main' into refactor-fs-router
tylersayshi 8232409
Merge branch 'dai-shi:main' into refactor-fs-router
tylersayshi cd89be0
Merge remote-tracking branch 'upstream/main' into refactor-fs-router
tylersayshi 1eb74a3
improve type
dai-shi b6b3ba2
Merge branch 'refactor-fs-router' into fix/define-router-for-error
dai-shi dbc92b1
wip: fallbackId
dai-shi 9a4dac4
optional fallbackElement
dai-shi db90f9e
unstable_renderPrev hack
dai-shi 0dfea25
Revert "Merge branch 'refactor-fs-router' into fix/define-router-for-…
dai-shi e896ffd
refactor
dai-shi ff3b69f
rethrow error for fallback
dai-shi d3bb6d4
create-pages hack for now
dai-shi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -224,8 +224,10 @@ const ChildrenContextProvider = memo(ChildrenContext.Provider); | |
|
||
type OuterSlotProps = { | ||
elementsPromise: Elements; | ||
shouldRenderPrev: ((err: unknown) => boolean) | undefined; | ||
renderSlot: (elements: Record<string, ReactNode>) => ReactNode; | ||
unstable_shouldRenderPrev: | ||
| ((err: unknown, prevElements: Record<string, ReactNode>) => boolean) | ||
| undefined; | ||
renderSlot: (elements: Record<string, ReactNode>, err?: unknown) => ReactNode; | ||
children?: ReactNode; | ||
}; | ||
|
||
|
@@ -245,9 +247,12 @@ class OuterSlot extends Component<OuterSlotProps, { error?: unknown }> { | |
// probably caused by history api fallback | ||
(e as any).statusCode = 404; | ||
} | ||
if (this.props.shouldRenderPrev?.(e) && this.props.elementsPromise.prev) { | ||
const elements = this.props.elementsPromise.prev; | ||
return this.props.renderSlot(elements); | ||
const prevElements = this.props.elementsPromise.prev; | ||
if ( | ||
prevElements && | ||
this.props.unstable_shouldRenderPrev?.(e, prevElements) | ||
) { | ||
return this.props.renderSlot(prevElements, e); | ||
} else { | ||
throw e; | ||
} | ||
|
@@ -261,12 +266,16 @@ const InnerSlot = ({ | |
renderSlot, | ||
}: { | ||
elementsPromise: Elements; | ||
renderSlot: (elements: Record<string, ReactNode>) => ReactNode; | ||
renderSlot: (elements: Record<string, ReactNode>, err?: unknown) => ReactNode; | ||
}) => { | ||
const elements = use(elementsPromise); | ||
return renderSlot(elements); | ||
}; | ||
|
||
const InnerErr = ({ err }: { err: unknown }) => { | ||
throw err; | ||
}; | ||
|
||
/** | ||
* Slot component | ||
* This is used under the Root component. | ||
|
@@ -286,19 +295,32 @@ export const Slot = ({ | |
children, | ||
fallback, | ||
unstable_shouldRenderPrev, | ||
unstable_renderPrev, | ||
}: { | ||
id: string; | ||
children?: ReactNode; | ||
fallback?: ReactNode; | ||
unstable_shouldRenderPrev?: (err: unknown) => boolean; | ||
unstable_shouldRenderPrev?: ( | ||
err: unknown, | ||
prevElements: Record<string, ReactNode>, | ||
) => boolean; | ||
unstable_renderPrev?: boolean; | ||
}) => { | ||
const elementsPromise = use(ElementsContext); | ||
if (!elementsPromise) { | ||
throw new Error('Missing Root component'); | ||
} | ||
const renderSlot = (elements: Record<string, ReactNode>) => { | ||
const renderSlot = (elements: Record<string, ReactNode>, err?: unknown) => { | ||
if (!(id in elements)) { | ||
if (fallback) { | ||
if (err) { | ||
// HACK I'm not sure if this is the right way | ||
return createElement( | ||
ChildrenContextProvider, | ||
{ value: createElement(InnerErr, { err }) }, | ||
fallback, | ||
); | ||
} | ||
return fallback; | ||
} | ||
throw new Error('Not found: ' + id); | ||
|
@@ -309,11 +331,17 @@ export const Slot = ({ | |
elements[id], | ||
); | ||
}; | ||
if (unstable_renderPrev) { | ||
if (!elementsPromise.prev) { | ||
throw new Error('Missing prev elements'); | ||
} | ||
return renderSlot(elementsPromise.prev); | ||
} | ||
Comment on lines
+334
to
+339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not happy with this, and hope to redesign it after finishing our current transition. |
||
return createElement( | ||
OuterSlot, | ||
{ | ||
elementsPromise, | ||
shouldRenderPrev: unstable_shouldRenderPrev, | ||
unstable_shouldRenderPrev, | ||
renderSlot, | ||
}, | ||
createElement(InnerSlot, { elementsPromise, renderSlot }), | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this hack works for new_createPages fallback. Let check it after merging...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work. so #1015.