From ef102484255c4ca98f37e75ae25e1e87609718f8 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:20:37 +0200 Subject: [PATCH] fix: ensure image endpoint is there on dev reload --- packages/astro/src/assets/endpoint/config.ts | 24 +++++++++++++++++-- packages/astro/src/core/create-vite.ts | 7 ++++-- packages/astro/src/core/dev/container.ts | 19 +++++++++++---- .../astro/src/core/routing/dev-default.ts | 14 +++++++++++ .../src/vite-plugin-astro-server/plugin.ts | 17 ++++++------- pnpm-lock.yaml | 2 -- 6 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 packages/astro/src/core/routing/dev-default.ts diff --git a/packages/astro/src/assets/endpoint/config.ts b/packages/astro/src/assets/endpoint/config.ts index 965b82812a9e..382645d6ee80 100644 --- a/packages/astro/src/assets/endpoint/config.ts +++ b/packages/astro/src/assets/endpoint/config.ts @@ -1,5 +1,6 @@ import { resolveInjectedRoute } from '../../core/routing/manifest/create.js'; import type { AstroSettings, ManifestData } from '../../types/astro.js'; +import type { RouteData } from '../../types/public/internal.js'; export function injectImageEndpoint( settings: AstroSettings, @@ -7,11 +8,30 @@ export function injectImageEndpoint( mode: 'dev' | 'build', cwd?: string, ) { + manifest.routes.push(getImageEndpointData(settings, mode, cwd)); +} + +export function ensureImageEndpointRoute( + settings: AstroSettings, + manifest: ManifestData, + mode: 'dev' | 'build', + cwd?: string, +) { + if (!manifest.routes.some((route) => route.route === '/_image')) { + manifest.routes.push(getImageEndpointData(settings, mode, cwd)); + } +} + +function getImageEndpointData( + settings: AstroSettings, + mode: 'dev' | 'build', + cwd?: string, +): RouteData { const endpointEntrypoint = settings.config.image.endpoint ?? (mode === 'dev' ? 'astro/assets/endpoint/node' : 'astro/assets/endpoint/generic'); - manifest.routes.push({ + return { type: 'endpoint', isIndex: false, route: '/_image', @@ -23,5 +43,5 @@ export function injectImageEndpoint( pathname: '/_image', prerender: false, fallbackRoutes: [], - }); + }; } diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index cda60942a8f2..e8d9edfe3660 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -33,6 +33,7 @@ import astroScannerPlugin from '../vite-plugin-scanner/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js'; import { vitePluginSSRManifest } from '../vite-plugin-ssr-manifest/index.js'; +import type { SSRManifest } from './app/types.js'; import type { Logger } from './logger/core.js'; import { createViteLogger } from './logger/vite.js'; import { vitePluginMiddleware } from './middleware/vite-plugin.js'; @@ -49,6 +50,7 @@ interface CreateViteOptions { fs?: typeof nodeFs; sync: boolean; manifest: ManifestData; + ssrManifest?: SSRManifest; } const ALWAYS_NOEXTERNAL = [ @@ -76,7 +78,7 @@ const ONLY_DEV_EXTERNAL = [ /** Return a base vite config as a common starting point for all Vite commands. */ export async function createVite( commandConfig: vite.InlineConfig, - { settings, logger, mode, command, fs = nodeFs, sync, manifest }: CreateViteOptions, + { settings, logger, mode, command, fs = nodeFs, sync, manifest, ssrManifest }: CreateViteOptions, ): Promise { const astroPkgsConfig = await crawlFrameworkPkgs({ root: fileURLToPath(settings.config.root), @@ -132,7 +134,8 @@ export async function createVite( astroScriptsPlugin({ settings }), // The server plugin is for dev only and having it run during the build causes // the build to run very slow as the filewatcher is triggered often. - mode !== 'build' && vitePluginAstroServer({ settings, logger, fs, manifest }), + mode !== 'build' && + vitePluginAstroServer({ settings, logger, fs, manifest, ssrManifest: ssrManifest! }), // ssrManifest is only required in dev mode, where it gets created before a Vite instance is created, and get passed to this function envVitePlugin({ settings }), astroEnv({ settings, mode, sync }), markdownVitePlugin({ settings, logger }), diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index d7f2c027427c..1fea20620b0a 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -4,7 +4,6 @@ import type { AstroSettings } from '../../types/astro.js'; import nodeFs from 'node:fs'; import * as vite from 'vite'; -import { injectImageEndpoint } from '../../assets/endpoint/config.js'; import { runHookConfigDone, runHookConfigSetup, @@ -12,9 +11,11 @@ import { runHookServerStart, } from '../../integrations/hooks.js'; import type { AstroInlineConfig } from '../../types/public/config.js'; +import { createDevelopmentManifest } from '../../vite-plugin-astro-server/plugin.js'; import { createVite } from '../create-vite.js'; import type { Logger } from '../logger/core.js'; import { apply as applyPolyfill } from '../polyfill.js'; +import { injectDefaultDevRoutes } from '../routing/dev-default.js'; import { createRouteManifest } from '../routing/index.js'; import { syncInternal } from '../sync/index.js'; @@ -81,9 +82,10 @@ export async function createContainer({ .filter(Boolean) as string[]; // Create the route manifest already outside of Vite so that `runHookConfigDone` can use it to inform integrations of the build output - const manifest = await createRouteManifest({ settings, fsMod: fs }, logger); + let manifest = await createRouteManifest({ settings, fsMod: fs }, logger); + const devSSRManifest = createDevelopmentManifest(settings); - injectImageEndpoint(settings, manifest, 'dev'); + manifest = injectDefaultDevRoutes(settings, devSSRManifest, manifest); const viteConfig = await createVite( { @@ -93,7 +95,16 @@ export async function createContainer({ include: rendererClientEntries, }, }, - { settings, logger, mode: 'dev', command: 'dev', fs, sync: false, manifest }, + { + settings, + logger, + mode: 'dev', + command: 'dev', + fs, + sync: false, + manifest, + ssrManifest: devSSRManifest, + }, ); await runHookConfigDone({ settings, logger }); diff --git a/packages/astro/src/core/routing/dev-default.ts b/packages/astro/src/core/routing/dev-default.ts new file mode 100644 index 000000000000..ac2ea1d3b03b --- /dev/null +++ b/packages/astro/src/core/routing/dev-default.ts @@ -0,0 +1,14 @@ +import { ensureImageEndpointRoute } from '../../assets/endpoint/config.js'; +import type { AstroSettings, ManifestData } from '../../types/astro.js'; +import type { SSRManifest } from '../app/types.js'; +import { injectDefaultRoutes } from './default.js'; + +export function injectDefaultDevRoutes( + settings: AstroSettings, + ssrManifest: SSRManifest, + routeManifest: ManifestData, +) { + ensureImageEndpointRoute(settings, routeManifest, 'dev'); + injectDefaultRoutes(ssrManifest, routeManifest); + return routeManifest; +} diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index e8dec86765e2..20020559e8f5 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -9,7 +9,7 @@ import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { patchOverlay } from '../core/errors/overlay.js'; import type { Logger } from '../core/logger/core.js'; import { createViteLoader } from '../core/module-loader/index.js'; -import { injectDefaultRoutes } from '../core/routing/default.js'; +import { injectDefaultDevRoutes } from '../core/routing/dev-default.js'; import { createRouteManifest } from '../core/routing/index.js'; import { toFallbackType, toRoutingStrategy } from '../i18n/utils.js'; import type { AstroSettings, ManifestData } from '../types/astro.js'; @@ -25,6 +25,7 @@ export interface AstroPluginOptions { logger: Logger; fs: typeof fs; manifest: ManifestData; + ssrManifest: SSRManifest; } export default function createVitePluginAstroServer({ @@ -32,14 +33,13 @@ export default function createVitePluginAstroServer({ logger, fs: fsMod, manifest: routeManifest, + ssrManifest: devSSRManifest, }: AstroPluginOptions): vite.Plugin { return { name: 'astro:server', configureServer(viteServer) { const loader = createViteLoader(viteServer); - const devSSRManifest = createDevelopmentManifest(settings); - let manifestData: ManifestData = injectDefaultRoutes(devSSRManifest, routeManifest); - const pipeline = DevPipeline.create(manifestData, { + const pipeline = DevPipeline.create(routeManifest, { loader, logger, manifest: devSSRManifest, @@ -52,11 +52,12 @@ export default function createVitePluginAstroServer({ async function rebuildManifest(needsManifestRebuild: boolean) { pipeline.clearRouteCache(); if (needsManifestRebuild) { - manifestData = injectDefaultRoutes( + routeManifest = injectDefaultDevRoutes( + settings, devSSRManifest, - await createRouteManifest({ settings, fsMod }, logger), + await createRouteManifest({ settings, fsMod }, logger), // TODO: Handle partial updates to the manifest ); - pipeline.setManifestData(manifestData); + pipeline.setManifestData(routeManifest); } } @@ -101,7 +102,7 @@ export default function createVitePluginAstroServer({ localStorage.run(request, () => { handleRequest({ pipeline, - manifestData, + manifestData: routeManifest, controller, incomingRequest: request, incomingResponse: response, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ecfeac33a404..dc6a790465d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8752,12 +8752,10 @@ packages: libsql@0.3.19: resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==} - cpu: [x64, arm64, wasm32] os: [darwin, linux, win32] libsql@0.4.1: resolution: {integrity: sha512-qZlR9Yu1zMBeLChzkE/cKfoKV3Esp9cn9Vx5Zirn4AVhDWPcjYhKwbtJcMuHehgk3mH+fJr9qW+3vesBWbQpBg==} - cpu: [x64, arm64, wasm32] os: [darwin, linux, win32] lilconfig@2.1.0: