diff --git a/examples/minimal/src/pages/index.astro b/examples/minimal/src/pages/index.astro index 7264ff502..725332b73 100644 --- a/examples/minimal/src/pages/index.astro +++ b/examples/minimal/src/pages/index.astro @@ -1,4 +1,5 @@ --- + --- @@ -11,5 +12,13 @@

Astro

+ + + + diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 2d7c07d84..ddd148751 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -13,6 +13,7 @@ import type { import { renderSlot } from '../../runtime/server/index.js'; import { renderJSX } from '../../runtime/server/jsx.js'; import { AstroCookies } from '../cookies/index.js'; +import { AstroError, AstroErrorCodes } from '../errors/index.js'; import { LogOptions, warn } from '../logger/core.js'; import { isScriptRequest } from './script.js'; import { isCSSRequest } from './util.js'; @@ -22,7 +23,11 @@ const clientAddressSymbol = Symbol.for('astro.clientAddress'); function onlyAvailableInSSR(name: string) { return function _onlyAvailableInSSR() { // TODO add more guidance when we have docs and adapters. - throw new Error(`Oops, you are trying to use ${name}, which is only available with SSR.`); + throw new AstroError({ + errorCode: AstroErrorCodes.UnavailableInSSR, + message: `Oops, you are trying to use ${name}, which is only available with SSR.`, + hint: 'See https://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-project for information on how to enable SSR', + }); }; } diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index 0489cfb5c..8ef3dc520 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -302,9 +302,11 @@ function isRedirect(statusCode: number) { function throwIfRedirectNotAllowed(response: Response, config: AstroConfig) { if (config.output !== 'server' && isRedirect(response.status)) { - throw new Error( - `Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.` - ); + throw new AstroError({ + errorCode: AstroErrorCodes.StaticRedirectNotAllowed, + message: + "Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.", + }); } } @@ -441,13 +443,22 @@ export default function createPlugin({ settings, logging }: AstroPluginOptions): }); }; }, - // HACK: hide `.tip` in Vite's ErrorOverlay and replace [vite] messages with [astro] transform(code, id, opts = {}) { if (opts.ssr) return; if (!id.includes('vite/dist/client/client.mjs')) return; - return code - .replace(/\.tip \{[^}]*\}/gm, '.tip {\n display: none;\n}') - .replace(/\[vite\]/g, '[astro]'); + return ( + code + // Transform links in the message to clickable links + .replace( + "this.text('.message-body', message.trim());", + `const urlPattern = /(\\b(https?|ftp):\\/\\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|])/gim; + this.root.querySelector(".message-body").innerHTML = message.trim().replace(urlPattern, '$1');` + ) + // Remove Vite's ErrorOverlay tip as it refers to some Viteism + .replace(/\.tip \{[^}]*\}/gm, '.tip {\n display: none;\n}') + // Replace all mentions of [vite] with [astro] + .replace(/\[vite\]/g, '[astro]') + ); }, }; } diff --git a/packages/astro/test/units/compile/invalid-css.test.js b/packages/astro/test/units/compile/invalid-css.test.js index 00d4fb7f6..0ea6f77a1 100644 --- a/packages/astro/test/units/compile/invalid-css.test.js +++ b/packages/astro/test/units/compile/invalid-css.test.js @@ -1,6 +1,6 @@ import { expect } from 'chai'; import { cachedCompilation } from '../../../dist/core/compile/index.js'; -import { AggregateError } from '../../../dist/core/util.js'; +import { AggregateError } from '../../../dist/core/errors/index.js'; describe('astro/src/core/compile', () => { describe('Invalid CSS', () => {