-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(Page): handle failed dynamic content loading safely #8006
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cc7c82
fix(Page): handle failed dynamic content loading safely
mr-baraiya ee00510
fix(test): correct router import and align error message
mr-baraiya 3defed0
fix(test): stabilize test for CI (router mock + scrollTo)
mr-baraiya b134409
fix(test): remove jest-dom dependency and use native assertion
mr-baraiya d86027e
fix(test): resolve eslint issues in Page test
mr-baraiya 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 hidden or 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 hidden or 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,50 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
|
|
||
| import { render, screen } from "@testing-library/react"; | ||
| import { MemoryRouter } from "react-router-dom"; | ||
| import Page from "../Page.jsx"; | ||
|
|
||
| jest.mock("../../Contributors/Contributors.jsx", () => { | ||
| const MockContributors = () => <div />; | ||
| return MockContributors; | ||
| }); | ||
|
|
||
| jest.mock("../../PageLinks/PageLinks.jsx", () => { | ||
| const MockPageLinks = () => <div />; | ||
| return MockPageLinks; | ||
| }); | ||
|
|
||
| jest.mock("react-router-dom", () => { | ||
| const actual = jest.requireActual("react-router-dom"); | ||
| return { | ||
| ...actual, | ||
| useLocation: () => ({ pathname: "/test", hash: "" }), | ||
| }; | ||
| }); | ||
|
|
||
| Object.defineProperty(window, "scrollTo", { | ||
| value: jest.fn(), | ||
| writable: true, | ||
| }); | ||
|
|
||
| describe("Page component", () => { | ||
| it("renders error message when content.__error exists", async () => { | ||
| const content = { | ||
| __error: true, | ||
| message: "Failed to load page content.", | ||
| }; | ||
|
|
||
| render( | ||
| <MemoryRouter> | ||
| <Page content={content} title="Test" path="/test" /> | ||
| </MemoryRouter>, | ||
| ); | ||
|
|
||
| const errorElement = await screen.findByText( | ||
| /failed to load page content/i, | ||
| ); | ||
| expect(errorElement).toBeTruthy(); | ||
| }); | ||
| }); |
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.
Can we add a test case for this
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.
Do you mean adding a unit test for the
content.__errorbranch to verify that the error message is rendered?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.
Yes
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.
Can we get this route on the real site? I feel like we are adding something what never be happened
Uh oh!
There was an error while loading. Please reload this page.
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.
This case can occur if dynamic content (MDX import) fails to resolve at runtime — for example:
broken or missing MDX file
network issues during dynamic import
incorrect path or deployment mismatch
Previously, this resulted in a plain string fallback, which breaks the render logic since it expects structured content. This change ensures the component handles such failures safely and avoids runtime crashes by providing a structured fallback
The unit test verifies that this fallback is rendered correctly when such a failure occurs.