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(vite): fix Fast Refresh load error when using defer #7842

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/sharp-kids-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Fix React Fast Refresh error on load when using `defer` in Vite dev server
34 changes: 26 additions & 8 deletions integration/vite-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ test.describe("Vite dev", () => {
}
`,
"app/routes/_index.tsx": js`
import { useState, useEffect } from "react";
import { Suspense } from "react";
import { defer } from "@remix-run/node";
import { Await, useLoaderData } from "@remix-run/react";

export function loader() {
let deferred = new Promise((resolve) => {
setTimeout(() => resolve(true), 1000)
});
return defer({ deferred });
}

export default function IndexRoute() {
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
const { deferred } = useLoaderData<typeof loader>();

return (
<div id="index">
<h2 data-title>Index</h2>
<input />
<p data-mounted>Mounted: {mounted ? "yes" : "no"}</p>
<p data-hmr>HMR updated: no</p>
<Suspense fallback={<p data-defer>Defer finished: no</p>}>
<Await resolve={deferred}>{() => <p data-defer>Defer finished: yes</p>}</Await>
</Suspense>
</div>
);
}
Expand Down Expand Up @@ -161,12 +169,19 @@ test.describe("Vite dev", () => {
});

test("renders matching routes", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

await page.goto(`http://localhost:${devPort}/`, {
waitUntil: "networkidle",
});

// Ensure no errors on page load
expect(pageErrors).toEqual([]);

await expect(page.locator("#index [data-title]")).toHaveText("Index");
await expect(page.locator("#index [data-mounted]")).toHaveText(
"Mounted: yes"
await expect(page.locator("#index [data-defer]")).toHaveText(
"Defer finished: yes"
);

let hmrStatus = page.locator("#index [data-hmr]");
Expand All @@ -188,6 +203,9 @@ test.describe("Vite dev", () => {
await page.waitForLoadState("networkidle");
await expect(hmrStatus).toHaveText("HMR updated: yes");
await expect(input).toHaveValue("stateful");

// Ensure no errors after HMR
expect(pageErrors).toEqual([]);
});

test("handles multiple set-cookie headers", async ({ page }) => {
Expand Down
1 change: 1 addition & 0 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
'export const LiveReload = process.env.NODE_ENV !== "development" ? () => null : ',
'() => createElement("script", {',
' type: "module",',
" async: true,",
" suppressHydrationWarning: true,",
" dangerouslySetInnerHTML: { __html: `",
` import RefreshRuntime from "${VirtualModule.url(
Expand Down
Loading