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(remix-dev/vite): support HMR for routes with handle export #8022

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/blue-guests-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Support HMR for routes with `handle` export in Vite dev
79 changes: 79 additions & 0 deletions integration/vite-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ test.describe("Vite dev", () => {
</>
}
`,
"app/routes/known-route-exports.tsx": js`
import { useMatches } from "@remix-run/react";

export const meta = () => [{
title: "HMR meta: 0"
}]

export const links = () => [{
rel: "icon",
href: "/favicon.ico",
type: "image/png",
"data-link": "HMR links: 0",
}]

export const handle = {
data: "HMR handle: 0"
};

export default function TestRoute() {
const matches = useMatches();

return (
<div id="known-route-export-hmr">
<input />
<p data-hmr>HMR component: 0</p>
<p data-handle>{matches[1].handle.data}</p>
</div>
);
}
`,
},
});

Expand Down Expand Up @@ -354,6 +384,55 @@ test.describe("Vite dev", () => {

expect(pageErrors).toEqual([]);
});

test("handle known route exports with HMR", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

await page.goto(`http://localhost:${devPort}/known-route-exports`, {
waitUntil: "networkidle",
});
expect(pageErrors).toEqual([]);

// file editing utils
let filepath = path.join(projectDir, "app/routes/known-route-exports.tsx");
let filedata = await fs.readFile(filepath, "utf8");
async function editFile(edit: (data: string) => string) {
filedata = edit(filedata);
await fs.writeFile(filepath, filedata, "utf8");
}

// verify input state is preserved after each update
let input = page.locator("input");
await input.type("stateful");
await expect(input).toHaveValue("stateful");

// component
await editFile((data) =>
data.replace("HMR component: 0", "HMR component: 1")
);
await expect(page.locator("[data-hmr]")).toHaveText("HMR component: 1");
await expect(input).toHaveValue("stateful");

// handle
await editFile((data) => data.replace("HMR handle: 0", "HMR handle: 1"));
await expect(page.locator("[data-handle]")).toHaveText("HMR handle: 1");
await expect(input).toHaveValue("stateful");

// meta
await editFile((data) => data.replace("HMR meta: 0", "HMR meta: 1"));
await expect(page).toHaveTitle("HMR meta: 1");
await expect(input).toHaveValue("stateful");

// links
await editFile((data) => data.replace("HMR links: 0", "HMR links: 1"));
await expect(page.locator("[data-link]")).toHaveAttribute(
"data-link",
"HMR links: 1"
);

expect(pageErrors).toEqual([]);
});
});

let bufferize = (stream: Readable): (() => string) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ function addRefreshWrapper(
id: string
): string {
let isRoute = getRoute(pluginConfig, id);
let acceptExports = isRoute ? ["meta", "links", "shouldRevalidate"] : [];
let acceptExports = isRoute
? ["handle", "meta", "links", "shouldRevalidate"]
: [];
return (
REACT_REFRESH_HEADER.replace("__SOURCE__", JSON.stringify(id)) +
code +
Expand Down
Loading