diff --git a/.changeset/flat-radios-cheer.md b/.changeset/flat-radios-cheer.md new file mode 100644 index 000000000..ab01d34dd --- /dev/null +++ b/.changeset/flat-radios-cheer.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +`--experimental-ssr` now is only required when using a 3rd-party adapter diff --git a/examples/ssr/package.json b/examples/ssr/package.json index 401f7e06a..370e79972 100644 --- a/examples/ssr/package.json +++ b/examples/ssr/package.json @@ -3,9 +3,9 @@ "version": "0.0.1", "private": true, "scripts": { - "dev": "astro dev --experimental-ssr", + "dev": "astro dev", "start": "astro dev", - "build": "astro build --experimental-ssr", + "build": "astro build", "server": "node server/server.mjs" }, "devDependencies": { diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 33b03a775..800ab9b23 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -345,7 +345,8 @@ export interface AstroUserConfig { */ experimentalStaticBuild?: boolean; /** - * Enable a build for SSR support. + * Enable SSR support for 3rd-party adapters. + * Not required when using a built-in adapter. * Default: false */ experimentalSsr?: boolean; diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 534937d8c..c68a97a74 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -39,7 +39,7 @@ function printAstroHelp() { ['--project-root ', 'Specify the path to the project root folder.'], ['--no-sitemap', 'Disable sitemap generation (build only).'], ['--legacy-build', 'Use the build strategy prior to 0.24.0'], - ['--experimental-ssr', 'Enable SSR compilation.'], + ['--experimental-ssr', 'Enable SSR compilation fot 3rd-party adapters.'], ['--drafts', 'Include markdown draft pages in the build.'], ['--verbose', 'Enable verbose logging'], ['--silent', 'Disable logging'], diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 7c8e92ce5..5d0c70a41 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -12,7 +12,7 @@ import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; import { call as callEndpoint } from '../endpoint/index.js'; import { render } from '../render/core.js'; import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js'; -import { getOutputFilename } from '../util.js'; +import { getOutputFilename, isBuildingToSSR } from '../util.js'; import { getOutFile, getOutFolder } from './common.js'; import { eachPageData, getPageDataByComponent } from './internal.js'; import type { PageBuildData, SingleFileBuiltModule, StaticBuildOptions } from './types'; @@ -71,7 +71,7 @@ export async function generatePages(result: RollupOutput, opts: StaticBuildOptio const timer = performance.now(); info(opts.logging, null, `\n${bgGreen(black(' generating static routes '))}`); - const ssr = !!opts.astroConfig._ctx.adapter?.serverEntrypoint; + const ssr = isBuildingToSSR(opts.astroConfig); const serverEntry = opts.buildConfig.serverEntry; const outFolder = ssr ? opts.buildConfig.server : opts.astroConfig.dist; const ssrEntryURL = new URL('./' + serverEntry + `?time=${Date.now()}`, outFolder); @@ -197,7 +197,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G route: pageData.route, routeCache, site: astroConfig.buildOptions.site, - ssr: opts.astroConfig.buildOptions.experimentalSsr, + ssr: isBuildingToSSR(opts.astroConfig), }; let body: string; diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 02e1898f8..db876176a 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -17,7 +17,7 @@ import { staticBuild } from './static-build.js'; import { RouteCache } from '../render/route-cache.js'; import { runHookBuildDone, runHookBuildStart, runHookConfigDone, runHookConfigSetup } from '../../integrations/index.js'; import { getTimeStat } from './util.js'; -import { createSafeError } from '../util.js'; +import { createSafeError, isBuildingToSSR } from '../util.js'; import { fixViteErrorMessage } from '../errors.js'; export interface BuildOptions { @@ -101,7 +101,7 @@ class AstroBuilder { origin, routeCache: this.routeCache, viteServer, - ssr: this.config.buildOptions.experimentalSsr, + ssr: isBuildingToSSR(this.config), }); // Filter pages by using conditions based on their frontmatter. @@ -182,7 +182,7 @@ class AstroBuilder { await runHookBuildDone({ config: this.config, pages: pageNames, routes: Object.values(allPages).map((pd) => pd.route) }); if (this.logging.level && levels[this.logging.level] <= levels['info']) { - const buildMode = this.config.buildOptions.experimentalSsr ? 'ssr' : 'static'; + const buildMode = isBuildingToSSR(this.config) ? 'ssr' : 'static'; await this.printStats({ logging: this.logging, timeStart: this.timer.init, pageCount: pageNames.length, buildMode }); } } diff --git a/packages/astro/src/core/build/page-data.ts b/packages/astro/src/core/build/page-data.ts index 707c89fd7..c84e29588 100644 --- a/packages/astro/src/core/build/page-data.ts +++ b/packages/astro/src/core/build/page-data.ts @@ -10,6 +10,7 @@ import { debug } from '../logger/core.js'; import { preload as ssrPreload } from '../render/dev/index.js'; import { generateRssFunction } from '../render/rss.js'; import { callGetStaticPaths, RouteCache, RouteCacheEntry } from '../render/route-cache.js'; +import { isBuildingToSSR } from '../util.js'; export interface CollectPagesDataOptions { astroConfig: AstroConfig; @@ -33,7 +34,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise = {}; const allPages: AllPagesData = {}; - const buildMode = astroConfig.buildOptions.experimentalSsr ? 'ssr' : 'static'; + const buildMode = isBuildingToSSR(astroConfig) ? 'ssr' : 'static'; const dataCollectionLogTimeout = setInterval(() => { info(opts.logging, 'build', 'The data collection step may take longer for larger projects...'); diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 481d51cfa..4e475afb9 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -111,7 +111,7 @@ export async function staticBuild(opts: StaticBuildOptions) { async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set) { const { astroConfig, viteConfig } = opts; - const ssr = astroConfig.buildOptions.experimentalSsr; + const ssr = isBuildingToSSR(astroConfig); const out = ssr ? opts.buildConfig.server : astroConfig.dist; const viteBuildConfig = { diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts index 394c1c42d..97b467e9d 100644 --- a/packages/astro/src/core/config.ts +++ b/packages/astro/src/core/config.ts @@ -261,12 +261,7 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) { if (typeof flags.host === 'string' || typeof flags.host === 'boolean') astroConfig.devOptions.host = flags.host; if (typeof flags.hostname === 'string') astroConfig.devOptions.hostname = flags.hostname; if (typeof flags.legacyBuild === 'boolean') astroConfig.buildOptions.legacyBuild = flags.legacyBuild; - if (typeof flags.experimentalSsr === 'boolean') { - astroConfig.buildOptions.experimentalSsr = flags.experimentalSsr; - if (flags.experimentalSsr) { - astroConfig.buildOptions.legacyBuild = false; - } - } + if (typeof flags.experimentalSsr === 'boolean') astroConfig.buildOptions.experimentalSsr = flags.experimentalSsr; if (typeof flags.experimentalIntegrations === 'boolean') astroConfig.experimentalIntegrations = flags.experimentalIntegrations; if (typeof flags.drafts === 'boolean') astroConfig.buildOptions.drafts = flags.drafts; return astroConfig; diff --git a/packages/astro/src/core/endpoint/dev/index.ts b/packages/astro/src/core/endpoint/dev/index.ts index da3671bc0..1e7abe8df 100644 --- a/packages/astro/src/core/endpoint/dev/index.ts +++ b/packages/astro/src/core/endpoint/dev/index.ts @@ -1,12 +1,13 @@ import type { EndpointHandler } from '../../../@types/astro'; import type { SSROptions } from '../../render/dev'; import { preload } from '../../render/dev/index.js'; +import { isBuildingToSSR } from '../../util.js'; import { call as callEndpoint } from '../index.js'; export async function call(ssrOpts: SSROptions) { const [, mod] = await preload(ssrOpts); return await callEndpoint(mod as unknown as EndpointHandler, { ...ssrOpts, - ssr: ssrOpts.astroConfig.buildOptions.experimentalSsr, + ssr: isBuildingToSSR(ssrOpts.astroConfig), }); } diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 1ced45a40..b81ef6863 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -9,6 +9,8 @@ import { createModuleScriptElementWithSrcSet } from '../ssr-element.js'; import { getStylesForURL } from './css.js'; import { getHmrScript } from './hmr.js'; import { injectTags } from './html.js'; +import { isBuildingToSSR } from '../../util.js'; + export interface SSROptions { /** an instance of the AstroConfig */ astroConfig: AstroConfig; @@ -146,7 +148,7 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta route, routeCache, site: astroConfig.buildOptions.site, - ssr: astroConfig.buildOptions.experimentalSsr, + ssr: isBuildingToSSR(astroConfig), }); if (route?.type === 'endpoint' || content.type === 'response') { diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 45885e9e8..2ec2a1744 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -139,7 +139,27 @@ export function emptyDir(_dir: URL, skip?: Set): void { } export function isBuildingToSSR(config: AstroConfig): boolean { - return !!config._ctx.adapter?.serverEntrypoint; + const adapter = config._ctx.adapter; + if (!adapter) return false; + + if (typeof adapter.serverEntrypoint === 'string') { + if (!adapter.name.startsWith('@astrojs/') && !config.buildOptions.experimentalSsr) { + throw new Error( + [ + `Server-side rendering (SSR) is still experimental.`, + ``, + `Only official "@astrojs/*" adapters are currently supported.`, + `To enable SSR for 3rd-party adapters, use the "--experimental-ssr" flag.`, + `Breaking changes may occur in this API before Astro v1.0 is released.`, + ``, + ].join('\n') + ); + } else { + return true; + } + } else { + return false; + } } export function emoji(char: string, fallback: string) { diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index 67df56aeb..5d85555af 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -6,7 +6,7 @@ import { debug, info, warn, error, LogOptions } from '../core/logger/core.js'; import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/core.js'; import { createRouteManifest, matchRoute } from '../core/routing/index.js'; import stripAnsi from 'strip-ansi'; -import { createSafeError } from '../core/util.js'; +import { createSafeError, isBuildingToSSR } from '../core/util.js'; import { ssr, preload } from '../core/render/dev/index.js'; import { call as callEndpoint } from '../core/endpoint/dev/index.js'; import * as msg from '../core/messages.js'; @@ -123,7 +123,7 @@ async function handleRequest( const site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined; const devRoot = site ? site.pathname : '/'; const origin = `${viteServer.config.server.https ? 'https' : 'http'}://${req.headers.host}`; - const buildingToSSR = !!config._ctx.adapter?.serverEntrypoint; + const buildingToSSR = isBuildingToSSR(config); const url = new URL(origin + req.url); const pathname = decodeURI(url.pathname); const rootRelativeUrl = pathname.substring(devRoot.length - 1); @@ -185,7 +185,7 @@ async function handleRequest( routeCache, pathname: rootRelativeUrl, logging, - ssr: config.buildOptions.experimentalSsr, + ssr: isBuildingToSSR(config), }); if (paramsAndPropsRes === GetParamsAndPropsError.NoMatchingStaticPath) { warn(logging, 'getStaticPaths', `Route pattern matched, but no matching static path found. (${pathname})`); diff --git a/packages/astro/test/ssr-api-route.test.js b/packages/astro/test/ssr-api-route.test.js index 54991e80a..6bf8fcd20 100644 --- a/packages/astro/test/ssr-api-route.test.js +++ b/packages/astro/test/ssr-api-route.test.js @@ -10,9 +10,7 @@ describe('API routes in SSR', () => { before(async () => { fixture = await loadFixture({ projectRoot: './fixtures/ssr-api-route/', - buildOptions: { - experimentalSsr: true, - }, + buildOptions: { experimentalSsr: true }, adapter: testAdapter(), }); await fixture.build(); diff --git a/packages/astro/test/ssr-dynamic.test.js b/packages/astro/test/ssr-dynamic.test.js index d938e5c95..98d73b98e 100644 --- a/packages/astro/test/ssr-dynamic.test.js +++ b/packages/astro/test/ssr-dynamic.test.js @@ -11,9 +11,7 @@ describe('Dynamic pages in SSR', () => { before(async () => { fixture = await loadFixture({ projectRoot: './fixtures/ssr-dynamic/', - buildOptions: { - experimentalSsr: true, - }, + buildOptions: { experimentalSsr: true }, adapter: testAdapter(), }); await fixture.build(); diff --git a/packages/integrations/deno/test/fixtures/basics/astro.config.mjs b/packages/integrations/deno/test/fixtures/basics/astro.config.mjs index dfdf9e182..38490926b 100644 --- a/packages/integrations/deno/test/fixtures/basics/astro.config.mjs +++ b/packages/integrations/deno/test/fixtures/basics/astro.config.mjs @@ -2,5 +2,6 @@ import { defineConfig } from 'astro/config'; import deno from '@astrojs/deno'; export default defineConfig({ - adapter: deno() + adapter: deno(), + buildOptions: { experimentalSsr: true } }) diff --git a/packages/integrations/deno/test/helpers.js b/packages/integrations/deno/test/helpers.js index 659d24d5e..bc2bc0622 100644 --- a/packages/integrations/deno/test/helpers.js +++ b/packages/integrations/deno/test/helpers.js @@ -13,7 +13,7 @@ export async function runBuild(fixturePath) { export async function runBuildAndStartApp(fixturePath, cb) { const url = new URL(fixturePath, dir); const close = await runBuild(fixturePath); - const mod = await import(new URL('./dist/entry.mjs', url)); + const mod = await import(new URL('./dist/server/entry.mjs', url)); await cb(); await mod.stop(); await close();