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

fix(core): add error log for server islands #12768

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/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 @@
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 Expand Up @@ -366,7 +366,7 @@
files.map(async (filename) => {
const url = new URL(filename, out);
const map = new URL(url + '.map');
await Promise.all([fs.promises.rm(url), fs.promises.rm(new URL(map)).catch((e) => {})]);

Check notice on line 369 in packages/astro/src/core/build/static-build.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedFunctionParameters

This parameter is unused.

Check notice on line 369 in packages/astro/src/core/build/static-build.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedVariables

This parameter is unused.
}),
);

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,20 @@ 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 use a server island without an adapter. This an error and your project won't build.",
ematipico marked this conversation as resolved.
Show resolved Hide resolved
);
}

if (settings.buildOutput !== 'server') {
logger.error(
'islands',
'You tried to use a server island, but your output isn\'t `"server"`. Use an adapter that support server output.',
ematipico marked this conversation as resolved.
Show resolved Hide resolved
);
}
ematipico marked this conversation as resolved.
Show resolved Hide resolved

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
Loading