Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: route manifest refactor #12597

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-crews-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue with some special routes not being passed to the `astro:routes:resolved` hook
11 changes: 0 additions & 11 deletions packages/astro/src/assets/endpoint/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ export function injectImageEndpoint(
manifest.routes.unshift(getImageEndpointData(settings, mode, cwd));
}

export function ensureImageEndpointRoute(
settings: AstroSettings,
manifest: ManifestData,
mode: 'dev' | 'build',
cwd?: string,
) {
if (!manifest.routes.some((route) => route.route === settings.config.image.endpoint.route)) {
manifest.routes.unshift(getImageEndpointData(settings, mode, cwd));
}
}

function getImageEndpointData(
settings: AstroSettings,
mode: 'dev' | 'build',
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
} from '../path.js';
import { RenderContext } from '../render-context.js';
import { createAssetLink } from '../render/ssr-element.js';
import { createDefaultRoutes, injectDefaultRoutes } from '../routing/default.js';
import { ensure404Route } from '../routing/astro-designed-error-pages.js';
import { createDefaultRoutes } from '../routing/default.js';
import { matchRoute } from '../routing/match.js';
import { AppPipeline } from './pipeline.js';

Expand Down Expand Up @@ -88,9 +89,10 @@ export class App {

constructor(manifest: SSRManifest, streaming = true) {
this.#manifest = manifest;
this.#manifestData = injectDefaultRoutes(manifest, {
this.#manifestData = {
routes: manifest.routes.map((route) => route.routeData),
});
};
ensure404Route(this.#manifestData);
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#manifest.base);
this.#pipeline = this.#createPipeline(this.#manifestData, streaming);
this.#adapterLogger = new AstroIntegrationLogger(
Expand Down
22 changes: 11 additions & 11 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { performance } from 'node:perf_hooks';
import { fileURLToPath } from 'node:url';
import { blue, bold, green } from 'kleur/colors';
import type * as vite from 'vite';
import { injectImageEndpoint } from '../../assets/endpoint/config.js';
import { telemetry } from '../../events/index.js';
import { eventCliSession } from '../../events/session.js';
import {
Expand All @@ -24,13 +23,13 @@ import type { Logger } from '../logger/core.js';
import { levels, timerMessage } from '../logger/core.js';
import { apply as applyPolyfill } from '../polyfill.js';
import { createRouteManifest } from '../routing/index.js';
import { getServerIslandRouteData } from '../server-islands/endpoint.js';
import { clearContentLayerCache } from '../sync/index.js';
import { ensureProcessNodeEnv } from '../util.js';
import { collectPagesData } from './page-data.js';
import { staticBuild, viteBuild } from './static-build.js';
import type { StaticBuildOptions } from './types.js';
import { getTimeStat } from './util.js';
import { injectServerIslandRoute } from '../server-islands/endpoint.js';

export interface BuildOptions {
/**
Expand Down Expand Up @@ -123,10 +122,6 @@ class AstroBuilder {

this.manifest = await createRouteManifest({ settings: this.settings }, this.logger);

if (this.settings.buildOutput === 'server') {
injectImageEndpoint(this.settings, this.manifest, 'build');
}

await runHookConfigDone({ settings: this.settings, logger: logger, command: 'build' });

// If we're building for the server, we need to ensure that an adapter is installed.
Expand Down Expand Up @@ -214,9 +209,15 @@ class AstroBuilder {
const { internals, ssrOutputChunkNames, contentFileNames } = await viteBuild(opts);

const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
// Error if there are server islands but no adapter provided.
if (hasServerIslands && this.settings.buildOutput !== 'server') {
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
if (hasServerIslands) {
if (this.settings.buildOutput === 'server') {
// Can't be injected in runHookRoutesResolved because serverIslandNameMap is updated
// in a vite plugin transform
injectServerIslandRoute(this.settings.config, this.manifest);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the data is not passed to the manifest, probably a matter of changing when this is called

} else {
// Error if there are server islands but no adapter provided.
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
}
}

await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);
Expand All @@ -238,8 +239,7 @@ class AstroBuilder {
pages: pageNames,
routes: Object.values(allPages)
.flat()
.map((pageData) => pageData.route)
.concat(hasServerIslands ? getServerIslandRouteData(this.settings.config) : []),
.map((pageData) => pageData.route),
logging: this.logger,
});

Expand Down
7 changes: 1 addition & 6 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as vite from 'vite';
import {
runHookConfigDone,
runHookConfigSetup,
runHookRoutesResolved,
runHookServerDone,
runHookServerStart,
} from '../../integrations/hooks.js';
Expand All @@ -16,7 +15,6 @@ import { createDevelopmentManifest } from '../../vite-plugin-astro-server/plugin
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';
import { warnMissingAdapter } from './adapter-validation.js';
Expand Down Expand Up @@ -84,12 +82,9 @@ 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
let manifest = await createRouteManifest({ settings, fsMod: fs }, logger, { dev: true });
const manifest = await createRouteManifest({ settings, fsMod: fs }, logger, { dev: true });
const devSSRManifest = createDevelopmentManifest(settings);

manifest = injectDefaultDevRoutes(settings, devSSRManifest, manifest);
await runHookRoutesResolved({ settings, logger, routes: manifest.routes });

await runHookConfigDone({ settings, logger, command: 'dev' });

warnMissingAdapter(logger, settings);
Expand Down
15 changes: 2 additions & 13 deletions packages/astro/src/core/routing/default.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import type { ComponentInstance, ManifestData } from '../../types/astro.js';
import type { ComponentInstance } from '../../types/astro.js';
import type { SSRManifest } from '../app/types.js';
import { DEFAULT_404_COMPONENT } from '../constants.js';
import {
SERVER_ISLAND_COMPONENT,
SERVER_ISLAND_ROUTE,
createEndpoint as createServerIslandEndpoint,
ensureServerIslandRoute,
} from '../server-islands/endpoint.js';
import {
DEFAULT_404_ROUTE,
default404Instance,
ensure404Route,
} from './astro-designed-error-pages.js';

export function injectDefaultRoutes(ssrManifest: SSRManifest, routeManifest: ManifestData) {
ensure404Route(routeManifest);
ensureServerIslandRoute(ssrManifest, routeManifest);
return routeManifest;
}
import { DEFAULT_404_ROUTE, default404Instance } from './astro-designed-error-pages.js';

type DefaultRouteParams = {
instance: ComponentInstance;
Expand Down
14 changes: 0 additions & 14 deletions packages/astro/src/core/routing/dev-default.ts

This file was deleted.

12 changes: 10 additions & 2 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { routeComparator } from '../priority.js';
import { getRouteGenerator } from './generator.js';
import { getPattern } from './pattern.js';
import { getRoutePrerenderOption } from './prerender.js';
import { injectImageEndpoint } from '../../../assets/endpoint/config.js';
import { ensure404Route } from '../astro-designed-error-pages.js';
import { injectServerIslandRoute } from '../../server-islands/endpoint.js';
const require = createRequire(import.meta.url);

interface Item {
Expand Down Expand Up @@ -732,9 +735,14 @@ export async function createRouteManifest(
}
}

if (!dev) {
await runHookRoutesResolved({ routes, settings, logger });
if (dev) {
ensure404Route({ routes });
injectImageEndpoint(settings, { routes }, 'dev');
injectServerIslandRoute(settings.config, { routes });
} else if (settings.buildOutput === 'server') {
injectImageEndpoint(settings, { routes }, 'build');
}
await runHookRoutesResolved({ routes, settings, logger });

return {
routes,
Expand Down
6 changes: 1 addition & 5 deletions packages/astro/src/core/server-islands/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ export function getServerIslandRouteData(config: ConfigFields) {
return route;
}

export function ensureServerIslandRoute(config: ConfigFields, routeManifest: ManifestData) {
if (routeManifest.routes.some((route) => route.route === '/_server-islands/[name]')) {
return;
}

export function injectServerIslandRoute(config: ConfigFields, routeManifest: ManifestData) {
routeManifest.routes.unshift(getServerIslandRouteData(config));
}

Expand Down
9 changes: 2 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { patchOverlay } from '../core/errors/overlay.js';
import type { Logger } from '../core/logger/core.js';
import { NOOP_MIDDLEWARE_FN } from '../core/middleware/noop-middleware.js';
import { createViteLoader } from '../core/module-loader/index.js';
import { injectDefaultDevRoutes } from '../core/routing/dev-default.js';
import { createRouteManifest } from '../core/routing/index.js';
import { getRoutePrerenderOption } from '../core/routing/manifest/prerender.js';
import { toFallbackType, toRoutingStrategy } from '../i18n/utils.js';
Expand Down Expand Up @@ -74,15 +73,11 @@ export default function createVitePluginAstroServer({
try {
const content = await fsMod.promises.readFile(routePath, 'utf-8');
await getRoutePrerenderOption(content, route, settings, logger);
await runHookRoutesResolved({ routes: routeManifest.routes, settings, logger });
} catch (_) {}
} else {
routeManifest = injectDefaultDevRoutes(
settings,
devSSRManifest,
await createRouteManifest({ settings, fsMod }, logger, { dev: true }),
);
routeManifest = await createRouteManifest({ settings, fsMod }, logger, { dev: true });
}
await runHookRoutesResolved({ routes: routeManifest.routes, settings, logger });

warnMissingAdapter(logger, settings);
pipeline.manifest.checkOrigin =
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/units/routing/manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ describe('routing - createRouteManifest', () => {
});

assert.deepEqual(getManifestRoutes(manifest), [
{
route: '/_image',
type: 'endpoint',
},
{
route: '/blog/[...slug]',
type: 'page',
Expand Down Expand Up @@ -306,6 +310,10 @@ describe('routing - createRouteManifest', () => {
});

assert.deepEqual(getManifestRoutes(manifest), [
{
route: '/_image',
type: 'endpoint',
},
{
route: '/blog/about',
type: 'redirect',
Expand Down Expand Up @@ -441,6 +449,7 @@ describe('routing - createRouteManifest', () => {
});

assert.deepEqual(getManifestRoutes(manifest), [
{ type: 'endpoint', route: '/_image' },
{ type: 'endpoint', route: '/blog/a-[b].233' },
{ type: 'redirect', route: '/posts/a-[b].233' },
{ type: 'page', route: '/[c]-d' },
Expand Down
Loading