Skip to content

Commit

Permalink
fix: ensure image endpoint is there on dev reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Sep 6, 2024
1 parent e7bd25a commit ef10248
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
24 changes: 22 additions & 2 deletions packages/astro/src/assets/endpoint/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
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,
manifest: ManifestData,
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',
Expand All @@ -23,5 +43,5 @@ export function injectImageEndpoint(
pathname: '/_image',
prerender: false,
fallbackRoutes: [],
});
};
}
7 changes: 5 additions & 2 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -49,6 +50,7 @@ interface CreateViteOptions {
fs?: typeof nodeFs;
sync: boolean;
manifest: ManifestData;
ssrManifest?: SSRManifest;
}

const ALWAYS_NOEXTERNAL = [
Expand Down Expand Up @@ -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<vite.InlineConfig> {
const astroPkgsConfig = await crawlFrameworkPkgs({
root: fileURLToPath(settings.config.root),
Expand Down Expand Up @@ -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 }),
Expand Down
19 changes: 15 additions & 4 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ 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,
runHookServerDone,
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';

Expand Down Expand Up @@ -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(
{
Expand All @@ -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 });
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/src/core/routing/dev-default.ts
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 9 additions & 8 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,21 +25,21 @@ export interface AstroPluginOptions {
logger: Logger;
fs: typeof fs;
manifest: ManifestData;
ssrManifest: SSRManifest;
}

export default function createVitePluginAstroServer({
settings,
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,
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -101,7 +102,7 @@ export default function createVitePluginAstroServer({
localStorage.run(request, () => {
handleRequest({
pipeline,
manifestData,
manifestData: routeManifest,
controller,
incomingRequest: request,
incomingResponse: response,
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef10248

Please sign in to comment.