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(