-
-
Notifications
You must be signed in to change notification settings - Fork 812
fix(vite): don't send browser full-reload for ssr-only changes #4034
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
4 commits
Select commit
Hold shift + click to select a range
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
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,3 @@ | ||
| import { state } from "../shared.ts"; | ||
|
|
||
| export default () => ({ state }); |
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,3 @@ | ||
| import { state } from "../shared.ts"; | ||
|
|
||
| document.getElementById("client-state-value")!.textContent = state + " (modified)"; |
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,26 @@ | ||
| import { html } from "nitro/h3"; | ||
| import { serverFetch } from "nitro"; | ||
| import { state } from "../shared.ts"; | ||
|
|
||
| export default { | ||
| fetch: async () => { | ||
| const apiData = (await serverFetch("/api/state").then((res) => res.json())) as { | ||
| state: number; | ||
| }; | ||
| const viteClientScript = "<script type='module' src='/@vite/client'></script>"; | ||
| const clientScript = "<script type='module' src='/app/entry-client.ts'></script>"; | ||
| return html` | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head>${viteClientScript}</head> | ||
| <body> | ||
| <h1>SSR Page</h1> | ||
| <p>[SSR] state: ${state}</p> | ||
| <p>[API] state: ${apiData.state}</p> | ||
| <p id="client-state">[Client] state: <span id="client-state-value">?</span></p> | ||
| ${clientScript} | ||
| </body> | ||
| </html> | ||
| `; | ||
| }, | ||
| }; |
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 @@ | ||
| export const state = 1; |
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,3 @@ | ||
| { | ||
| "extends": "nitro/tsconfig" | ||
| } |
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,6 @@ | ||
| import { defineConfig } from "vite"; | ||
| import { nitro } from "nitro/vite"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [nitro({ serverDir: "./" })], | ||
| }); |
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,157 @@ | ||
| import { join } from "pathe"; | ||
| import { readFileSync, writeFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import type { ViteDevServer } from "vite"; | ||
| import { describe, test, expect, beforeAll, afterEach, afterAll } from "vitest"; | ||
|
|
||
| const { createServer } = (await import( | ||
| process.env.NITRO_VITE_PKG || "vite" | ||
| )) as typeof import("vite"); | ||
|
|
||
| describe("vite:hmr", { sequential: true }, () => { | ||
| let server: ViteDevServer; | ||
| let serverURL: string; | ||
| const wsMessages: any[] = []; | ||
|
|
||
| const rootDir = fileURLToPath(new URL("./hmr-fixture", import.meta.url)); | ||
|
|
||
| const files = { | ||
| client: openFileForEditing(join(rootDir, "app/entry-client.ts")), | ||
| api: openFileForEditing(join(rootDir, "api/state.ts")), | ||
| shared: openFileForEditing(join(rootDir, "shared.ts")), | ||
| ssr: openFileForEditing(join(rootDir, "app/entry-server.ts")), | ||
| }; | ||
|
|
||
| beforeAll(async () => { | ||
| process.chdir(rootDir); | ||
| server = await createServer({ root: rootDir }); | ||
|
|
||
| const originalSend = server.ws.send.bind(server.ws); | ||
| server.ws.send = function (payload: any) { | ||
| wsMessages.push(payload); | ||
| return originalSend(payload); | ||
| }; | ||
|
|
||
| await server.listen("0" as unknown as number); | ||
| const addr = server.httpServer?.address() as { port: number; address: string; family: string }; | ||
| serverURL = `http://${addr.family === "IPv6" ? `[${addr.address}]` : addr.address}:${addr.port}`; | ||
|
|
||
| const html = await fetch(serverURL).then((r) => r.text()); | ||
| expect(html).toContain("<h1>SSR Page</h1>"); | ||
| expect(html).toContain("[SSR] state: 1"); | ||
| expect(html).toContain("[API] state: 1"); | ||
| }, 30_000); | ||
|
|
||
| afterAll(async () => { | ||
| await server?.close(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| wsMessages.length = 0; | ||
| let restored = false; | ||
| for (const file of Object.values(files)) { | ||
| if (file.restore()) { | ||
| restored = true; | ||
| } | ||
| } | ||
| if (restored) { | ||
| await waitFor(() => wsMessages.length > 0, 500); | ||
| } | ||
| wsMessages.length = 0; | ||
| }); | ||
|
|
||
| test("editing API entry", async () => { | ||
| files.api.update((content) => | ||
| content.replace("({ state })", '({ state: state + " (modified)" })') | ||
| ); | ||
| await pollResponse(`${serverURL}/api/state`, /modified/); | ||
| expect(wsMessages).toMatchObject([{ type: "full-reload" }]); | ||
| }); | ||
|
|
||
| test("Editing client entry (no full-reload)", async () => { | ||
| files.client.update((content) => content.replace(`+ ""`, `+ " (modified)"`)); | ||
| await pollResponse(`${serverURL}/app/entry-client.ts`, /modified/); | ||
| expect(wsMessages.length).toBe(0); | ||
| }); | ||
|
pi0 marked this conversation as resolved.
|
||
|
|
||
| test("editing SSR entry (no full-reload)", async () => { | ||
| files.ssr.update((content) => | ||
| content.replace("<h1>SSR Page</h1>", "<h1>Modified SSR Page</h1>") | ||
| ); | ||
| await pollResponse(serverURL, /Modified SSR Page/); | ||
| expect(wsMessages.length).toBe(0); | ||
| }); | ||
|
|
||
| test("Editing shared entry", async () => { | ||
| files.shared.update((content) => content.replace(`state = 1`, `state = 2`)); | ||
| await pollResponse( | ||
| `${serverURL}`, | ||
| (txt) => txt.includes("state: 2") && !txt.includes("state: 1") | ||
| ); | ||
| expect(wsMessages).toMatchObject([{ type: "full-reload" }]); | ||
| }); | ||
| }); | ||
|
|
||
| function openFileForEditing(path: string) { | ||
| const originalContent = readFileSync(path, "utf-8"); | ||
| return { | ||
| path, | ||
| update(cb: (content: string) => string) { | ||
| const currentContent = readFileSync(path, "utf-8"); | ||
| const newContent = cb(currentContent); | ||
| writeFileSync(path, newContent); | ||
| }, | ||
| restore() { | ||
| if (readFileSync(path, "utf-8") !== originalContent) { | ||
| writeFileSync(path, originalContent); | ||
| return true; | ||
| } | ||
| return false; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function waitFor(check: () => boolean, duration: number): Promise<void> { | ||
| const start = Date.now(); | ||
| return new Promise((resolve) => { | ||
| const poll = () => { | ||
| if (check() || Date.now() - start > duration) { | ||
| resolve(); | ||
| } else { | ||
| setTimeout(poll, 10); | ||
| } | ||
| }; | ||
| poll(); | ||
| }); | ||
| } | ||
|
|
||
| function pollResponse( | ||
| url: string, | ||
| match: RegExp | ((txt: string) => boolean), | ||
| timeout = 5000 | ||
| ): Promise<string> { | ||
| const start = Date.now(); | ||
| let lastResponse = ""; | ||
| return new Promise((resolve, reject) => { | ||
| const check = async () => { | ||
| try { | ||
| const response = await fetch(url); | ||
| lastResponse = await response.text(); | ||
| if (typeof match === "function" ? match(lastResponse) : match.test(lastResponse)) { | ||
| resolve(lastResponse); | ||
| } else if (Date.now() - start > timeout) { | ||
| reject( | ||
| new Error( | ||
| `Timeout waiting for response to match ${match} at ${url}. Last response: ${lastResponse}` | ||
| ) | ||
| ); | ||
| } else { | ||
| setTimeout(check, 100); | ||
| } | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| }; | ||
| check(); | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.