From 941b44926f35fb9ac848e9f0bc82284c0a20f493 Mon Sep 17 00:00:00 2001 From: ematipico Date: Mon, 16 Mar 2026 13:53:07 +0000 Subject: [PATCH] fix(hmr): normalize paths when reloading manifest --- .changeset/sweet-socks-tap.md | 5 +++++ packages/astro/src/manifest/serialized.ts | 7 +++++-- packages/astro/src/vite-plugin-routes/index.ts | 6 ++++-- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 .changeset/sweet-socks-tap.md diff --git a/.changeset/sweet-socks-tap.md b/.changeset/sweet-socks-tap.md new file mode 100644 index 000000000000..658c140b9e4c --- /dev/null +++ b/.changeset/sweet-socks-tap.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where HMR didn't correctly work on Windows when adding/changing/deleting routes in `pages/`. diff --git a/packages/astro/src/manifest/serialized.ts b/packages/astro/src/manifest/serialized.ts index 121bf3d99887..ccb0008e91d9 100644 --- a/packages/astro/src/manifest/serialized.ts +++ b/packages/astro/src/manifest/serialized.ts @@ -1,4 +1,5 @@ -import type { Plugin, ViteDevServer } from 'vite'; +import { fileURLToPath } from 'node:url'; +import { normalizePath, type Plugin, type ViteDevServer } from 'vite'; import { ACTIONS_ENTRYPOINT_VIRTUAL_MODULE_ID } from '../actions/consts.js'; import { toFallbackType } from '../core/app/common.js'; import { toRoutingStrategy } from '../core/app/entrypoints/index.js'; @@ -41,8 +42,10 @@ export function serializedManifestPlugin({ command: 'dev' | 'build'; sync: boolean; }): Plugin { + const normalizedSrcDir = normalizePath(fileURLToPath(settings.config.srcDir)); + function reloadManifest(path: string | null, server: ViteDevServer) { - if (path != null && path.startsWith(settings.config.srcDir.pathname)) { + if (path != null && normalizePath(path).startsWith(normalizedSrcDir)) { const environment = server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr]; const virtualMod = environment.moduleGraph.getModuleById(SERIALIZED_MANIFEST_RESOLVED_ID); if (!virtualMod) return; diff --git a/packages/astro/src/vite-plugin-routes/index.ts b/packages/astro/src/vite-plugin-routes/index.ts index 039f492ba94b..ca8c889470aa 100644 --- a/packages/astro/src/vite-plugin-routes/index.ts +++ b/packages/astro/src/vite-plugin-routes/index.ts @@ -79,11 +79,13 @@ export default async function astroPluginRoutes({ }, ); + const normalizedSrcDir = normalizePath(fileURLToPath(settings.config.srcDir)); + async function rebuildRoutes(path: string | null = null, server: ViteDevServer) { - if (path != null && path.startsWith(settings.config.srcDir.pathname)) { + if (path != null && normalizePath(path).startsWith(normalizedSrcDir)) { logger.debug( 'update', - `Re-calculating routes for ${path.slice(settings.config.srcDir.pathname.length)}`, + `Re-calculating routes for ${normalizePath(path).slice(normalizedSrcDir.length)}`, ); const file = pathToFileURL(normalizePath(path)); const newRoutesList = await createRoutesList(