Skip to content

Commit

Permalink
fix(core): add error log for server islands (#12768)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent 892dd9f commit 524c855
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-tools-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where Astro didn't print error logs when Astro Islands were used in incorrect cases.
5 changes: 5 additions & 0 deletions .changeset/silver-cars-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where Astro was printing the incorrect output format when running the `astro build` command
5 changes: 5 additions & 0 deletions .changeset/spotty-timers-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/underscore-redirects': minor
---

Adds a new `buildOutput` property to the API `createRedirectsFromAstroRoutes`
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
runHookConfigSetup,
} from '../../integrations/hooks.js';
import type { AstroSettings, ManifestData } from '../../types/astro.js';
import type { AstroConfig, AstroInlineConfig, RuntimeMode } from '../../types/public/config.js';
import type { AstroInlineConfig, RuntimeMode } from '../../types/public/config.js';
import { resolveConfig } from '../config/config.js';
import { createNodeLogger } from '../config/logging.js';
import { createSettings } from '../config/settings.js';
Expand Down Expand Up @@ -163,7 +163,7 @@ class AstroBuilder {
await runHookBuildStart({ config: this.settings.config, logging: this.logger });
this.validateConfig();

this.logger.info('build', `output: ${blue('"' + this.settings.config.output + '"')}`);
this.logger.info('build', `output: ${blue('"' + this.settings.buildOutput + '"')}`);
this.logger.info('build', `directory: ${blue(fileURLToPath(this.settings.config.outDir))}`);
if (this.settings.adapter) {
this.logger.info('build', `adapter: ${green(this.settings.adapter.name)}`);
Expand Down Expand Up @@ -283,7 +283,7 @@ class AstroBuilder {
logger: Logger;
timeStart: number;
pageCount: number;
buildMode: AstroConfig['output'];
buildMode: AstroSettings['buildOutput'];
}) {
const total = getTimeStat(timeStart, performance.now());

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function viteBuild(opts: StaticBuildOptions) {
registerAllPlugins(container);
// Build your project (SSR application code, assets, client JS, etc.)
const ssrTime = performance.now();
opts.logger.info('build', `Building ${settings.config.output} entrypoints...`);
opts.logger.info('build', `Building ${settings.buildOutput} entrypoints...`);
const ssrOutput = await ssrBuild(opts, internals, pageInput, container);
opts.logger.info('build', green(`✓ Completed in ${getTimeStat(ssrTime, performance.now())}.`));

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export async function createVite(
astroInternationalization({ settings }),
vitePluginActions({ fs, settings }),
vitePluginUserActions({ settings }),
vitePluginServerIslands({ settings }),
vitePluginServerIslands({ settings, logger }),
astroContainer(),
astroHmrReloadPlugin(),
],
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/logger/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type LoggerLabel =
| 'env'
| 'update'
| 'adapter'
| 'islands'
// SKIP_FORMAT: A special label that tells the logger not to apply any formatting.
// Useful for messages that are already formatted, like the server start message.
| 'SKIP_FORMAT';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../../types/astro.js';
import type { AstroPluginOptions } from '../../types/astro.js';
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js';

export const VIRTUAL_ISLAND_MAP_ID = '@astro-server-islands';
export const RESOLVED_VIRTUAL_ISLAND_MAP_ID = '\0' + VIRTUAL_ISLAND_MAP_ID;
const serverIslandPlaceholder = "'$$server-islands$$'";

export function vitePluginServerIslands({ settings }: { settings: AstroSettings }): VitePlugin {
export function vitePluginServerIslands({ settings, logger }: AstroPluginOptions): VitePlugin {
let command: ConfigEnv['command'] = 'serve';
let viteServer: ViteDevServer | null = null;
const referenceIdMap = new Map<string, string>();
Expand Down Expand Up @@ -37,6 +37,13 @@ export function vitePluginServerIslands({ settings }: { settings: AstroSettings
if (astro?.serverComponents.length) {
for (const comp of astro.serverComponents) {
if (!settings.serverIslandNameMap.has(comp.resolvedPath)) {
if (!settings.adapter) {
logger.error(
'islands',
"You tried to render a server island without an adapter added to your project. An adapter is required to use the `server:defer` attribute on any component. Your project will fail to build unless you add an adapter or remove the attribute.",
);
}

let name = comp.localName;
let idx = 1;

Expand Down
5 changes: 4 additions & 1 deletion packages/underscore-redirects/src/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface CreateRedirectsFromAstroRoutesParams {
*/
routeToDynamicTargetMap: Map<IntegrationRouteData, string>;
dir: URL;
buildOutput: 'static' | 'server';
}

/**
Expand All @@ -27,14 +28,16 @@ export function createRedirectsFromAstroRoutes({
config,
routeToDynamicTargetMap,
dir,
buildOutput,
}: CreateRedirectsFromAstroRoutesParams) {
const base =
config.base && config.base !== '/'
? config.base.endsWith('/')
? config.base.slice(0, -1)
: config.base
: '';
const output = config.output;
// TODO: the use of `config.output` is deprecated. We need to update the adapters that use this package to pass the new buildOutput
const output = buildOutput ?? config.output;
const _redirects = new Redirects();

for (const [route, dynamicTarget = ''] of routeToDynamicTargetMap) {
Expand Down

0 comments on commit 524c855

Please sign in to comment.