Skip to content
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: reload page when routeModules doesn't contain module for current route #6409

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions integration/browser-entry-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { Request } from "@playwright/test";
import { test, expect } from "@playwright/test";

import type { AppFixture, Fixture } from "./helpers/create-fixture";
import { createFixture, js, createAppFixture } from "./helpers/create-fixture";
import { PlaywrightFixture } from "./helpers/playwright-fixture";

let fixture: Fixture;
let appFixture: AppFixture;

test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/routes/index.jsx": js`
import { Link } from "@remix-run/react";

export default function Index() {
return (
<div>
<div id="pizza">pizza</div>
<Link to="/burgers">burger link</Link>
</div>
)
}
`,

"app/routes/burgers.jsx": js`

export default function Index() {
return (
<div id="cheeseburger">cheeseburger</div>
);
}
`,
},
});

// This creates an interactive app using puppeteer.
appFixture = await createAppFixture(fixture);
});

test.afterAll(() => appFixture.close());

// This test generally fails without the corresponding fix in browser.tsx,
// but sometimes manages to pass. With the fix, it always passes.
test(`expect to be able to browse backward out of a remix app,
then forward in history and have pages render correctly`, async ({
page,
browserName,
}) => {
test.skip(
browserName === "firefox",
"FireFox doesn't support browsing to an empty page (aka about:blank)"
);

let app = new PlaywrightFixture(appFixture, page);

// This sets up the Remix modules cache in memory, priming the error case.
await app.goto("/");
await app.clickLink("/burgers");
expect(await page.content()).toContain("cheeseburger");

let retry = 4;
for (let i = 0; i < retry; i++) {
// Back to /
await page.goBack();

await page.waitForSelector("#pizza");
expect(await app.getHtml()).toContain("pizza");

// Takes the browser to an empty state.
// This doesn't seem to work in headless Firefox.
await page.goBack();
expect(page.url()).toContain("about:blank");

// This attempts to watch for the request for the entry.client.js chunk
// and redirect before it is finished loading.
let redirectOnEntryChunk = async (request: Request) => {
if (request.url().includes("entry")) {
page.off("request", redirectOnEntryChunk);
await page.goForward();
}
};

page.on("request", redirectOnEntryChunk);

// Forward to /
// This initiates a request for the entry.client.js chunk
await page.goForward();
expect(page.url()).toContain("/");

// The navigation to /burgers happens in `redirectOnEntryChunk`.
// Here's an error: the path should be `/burgers`
// (this validates correctly and passes)
await page.waitForSelector("#cheeseburger");
expect(page.url()).toContain("/burgers");

// but now the content won't contain the string "cheeseburger"
expect(await app.getHtml()).toContain("cheeseburger");
}
});
11 changes: 11 additions & 0 deletions packages/remix-react/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ export function RemixBrowser(_props: RemixBrowserProps): ReactElement {
});
}, [location]);

// Check if __remixRouteModules is missing the module for the current route
// and if so, load it.
for (let match of router.state.matches) {
if (!window.__remixRouteModules[match.route.id]) {
window.location.reload();
// `reload` executes asynchronously, meaning the error will still flash.
// To get around that we return an empty div.
return <div></div>;
n8agrin marked this conversation as resolved.
Show resolved Hide resolved
}
}

// We need to include a wrapper RemixErrorBoundary here in case the root error
// boundary also throws and we need to bubble up outside of the router entirely.
// Then we need a stateful location here so the user can back-button navigate
Expand Down