Skip to content

Commit 9d2b501

Browse files
ematipicoascorbicmatthewp
authored
chore: handle invalidation of manifest (#14537)
Co-authored-by: ascorbic <[email protected]> Co-authored-by: matthewp <[email protected]>
1 parent f91503f commit 9d2b501

File tree

1 file changed

+19
-9
lines changed
  • packages/astro/src/vite-plugin-routes

1 file changed

+19
-9
lines changed

packages/astro/src/vite-plugin-routes/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type fsMod from 'node:fs';
22
import { extname } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
3+
import { fileURLToPath, pathToFileURL } from 'node:url';
44
import { bold } from 'kleur/colors';
5-
import type { Plugin } from 'vite';
5+
import { normalizePath, type Plugin, type ViteDevServer } from 'vite';
66
import { serializeRouteData } from '../core/app/index.js';
77
import type { SerializedRouteInfo } from '../core/app/types.js';
88
import { warnMissingAdapter } from '../core/dev/adapter-validation.js';
99
import type { Logger } from '../core/logger/core.js';
1010
import { createRoutesList } from '../core/routing/index.js';
1111
import { getRoutePrerenderOption } from '../core/routing/manifest/prerender.js';
1212
import { isEndpoint, isPage } from '../core/util.js';
13-
import { normalizePath, rootRelativePath } from '../core/viteUtils.js';
13+
import { rootRelativePath } from '../core/viteUtils.js';
1414
import type { AstroSettings } from '../types/astro.js';
1515
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
1616
import type { PluginMetadata } from '../vite-plugin-astro/types.js';
@@ -56,8 +56,13 @@ export default async function astroPluginRoutes({
5656
},
5757
);
5858

59-
async function rebuildRoutes(path: string | null = null) {
60-
if (path != null) {
59+
async function rebuildRoutes(path: string | null = null, server: ViteDevServer) {
60+
if (path != null && path.startsWith(settings.config.srcDir.pathname)) {
61+
logger.debug(
62+
'update',
63+
`Re-calculating routes for ${path.slice(settings.config.srcDir.pathname.length)}`,
64+
);
65+
const file = pathToFileURL(normalizePath(path));
6166
routeList = await createRoutesList(
6267
{
6368
settings,
@@ -70,21 +75,26 @@ export default async function astroPluginRoutes({
7075

7176
serializedRouteInfo = routeList.routes.map((r): SerializedRouteInfo => {
7277
return {
73-
file: '',
78+
file: fileURLToPath(file),
7479
links: [],
7580
scripts: [],
7681
styles: [],
7782
routeData: serializeRouteData(r, settings.config.trailingSlash),
7883
};
7984
});
85+
let environment = server.environments.ssr;
86+
const virtualMod = environment.moduleGraph.getModuleById(ASTRO_ROUTES_MODULE_ID_RESOLVED);
87+
if (!virtualMod) return;
88+
89+
environment.moduleGraph.invalidateModule(virtualMod);
8090
}
8191
}
8292
return {
8393
name: 'astro:routes',
8494
configureServer(server) {
85-
server.watcher.on('add', rebuildRoutes.bind(null, null));
86-
server.watcher.on('unlink', rebuildRoutes.bind(null, null));
87-
server.watcher.on('change', rebuildRoutes);
95+
server.watcher.on('add', (path) => rebuildRoutes(path, server));
96+
server.watcher.on('unlink', (path) => rebuildRoutes(path, server));
97+
server.watcher.on('change', (path) => rebuildRoutes(path, server));
8898
},
8999

90100
applyToEnvironment(environment) {

0 commit comments

Comments
 (0)