From d088351f54d2518e2bb539d7bbf8691427ff8a7a Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 19 Jul 2023 09:33:45 -0500 Subject: [PATCH] Warn when `getStaticPaths` exists without a `prerender` statement (#7713) * wip: warning on getStaticPaths without prerender * refactor: move getStaticPaths warning to scanner plugin * chore: do not add to test fixture * chore: remove legacy getStaticPaths validation * refactor: update warning message * chore: typo * chore: add changeset * chore: remove unused variables * refactor: make settings optional * chore: fix lint * chore: update message to include reason --- .changeset/sixty-plums-appear.md | 5 ++++ packages/astro/src/core/create-vite.ts | 2 +- packages/astro/src/core/render/route-cache.ts | 2 +- packages/astro/src/core/routing/validation.ts | 12 +-------- .../astro/src/vite-plugin-scanner/index.ts | 25 +++++++++++++++---- .../astro/src/vite-plugin-scanner/scan.ts | 10 +++++--- 6 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 .changeset/sixty-plums-appear.md diff --git a/.changeset/sixty-plums-appear.md b/.changeset/sixty-plums-appear.md new file mode 100644 index 000000000000..3b8150ca0573 --- /dev/null +++ b/.changeset/sixty-plums-appear.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Update warning when `getStaticPaths` is detected but a route is not prerendered. diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 279a7ffde940..52b5fd277a40 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -125,7 +125,7 @@ export async function createVite( mode === 'dev' && astroIntegrationsContainerPlugin({ settings, logging }), astroScriptsPageSSRPlugin({ settings }), astroHeadPlugin(), - astroScannerPlugin({ settings }), + astroScannerPlugin({ settings, logging }), astroInjectEnvTsPlugin({ settings, logging, fs }), astroContentVirtualModPlugin({ settings }), astroContentImportPlugin({ fs, settings }), diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index 71b2966dea35..7ad247ef8d81 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -34,7 +34,7 @@ export async function callGetStaticPaths({ const cached = routeCache.get(route); if (cached?.staticPaths) return cached.staticPaths; - validateDynamicRouteModule(mod, { ssr, logging, route }); + validateDynamicRouteModule(mod, { ssr, route }); // No static paths in SSR mode. Return an empty RouteCacheEntry. if (ssr && !route.prerender) { diff --git a/packages/astro/src/core/routing/validation.ts b/packages/astro/src/core/routing/validation.ts index 1a3bab82a2ee..9a562c044aa3 100644 --- a/packages/astro/src/core/routing/validation.ts +++ b/packages/astro/src/core/routing/validation.ts @@ -1,4 +1,3 @@ -import { bold } from 'kleur/colors'; import type { ComponentInstance, GetStaticPathsResult, RouteData } from '../../@types/astro'; import { AstroError, AstroErrorData } from '../errors/index.js'; import type { LogOptions } from '../logger/core'; @@ -19,26 +18,17 @@ export function validateGetStaticPathsParameter([key, value]: [string, any], rou } } -/** Warn or error for deprecated or malformed route components */ +/** Error for deprecated or malformed route components */ export function validateDynamicRouteModule( mod: ComponentInstance, { ssr, - logging, route, }: { ssr: boolean; - logging: LogOptions; route: RouteData; } ) { - if (ssr && mod.getStaticPaths && !route.prerender) { - warn( - logging, - 'getStaticPaths', - `getStaticPaths() in ${bold(route.component)} is ignored when "output: server" is set.` - ); - } if ((!ssr || route.prerender) && !mod.getStaticPaths) { throw new AstroError({ ...AstroErrorData.GetStaticPathsRequired, diff --git a/packages/astro/src/vite-plugin-scanner/index.ts b/packages/astro/src/vite-plugin-scanner/index.ts index 465ff74ed6c4..439a56d4649f 100644 --- a/packages/astro/src/vite-plugin-scanner/index.ts +++ b/packages/astro/src/vite-plugin-scanner/index.ts @@ -1,11 +1,20 @@ -import { normalizePath, type Plugin as VitePlugin } from 'vite'; +import type { Plugin as VitePlugin } from 'vite'; import type { AstroSettings } from '../@types/astro.js'; -import { isEndpoint, isPage } from '../core/util.js'; +import { type LogOptions } from '../core/logger/core.js'; -import { getPrerenderDefault } from '../prerender/utils.js'; +import { normalizePath } from 'vite'; +import { bold } from 'kleur/colors'; +import { warn } from '../core/logger/core.js'; +import { isEndpoint, isPage, rootRelativePath } from '../core/util.js'; +import { getPrerenderDefault, isServerLikeOutput } from '../prerender/utils.js'; import { scan } from './scan.js'; -export default function astroScannerPlugin({ settings }: { settings: AstroSettings }): VitePlugin { +export interface AstroPluginScannerOptions { + settings: AstroSettings; + logging: LogOptions; +} + +export default function astroScannerPlugin({ settings, logging }: AstroPluginScannerOptions): VitePlugin { return { name: 'astro:scanner', enforce: 'post', @@ -26,11 +35,17 @@ export default function astroScannerPlugin({ settings }: { settings: AstroSettin const fileIsEndpoint = isEndpoint(fileURL, settings); if (!(fileIsPage || fileIsEndpoint)) return; const defaultPrerender = getPrerenderDefault(settings.config); - const pageOptions = await scan(code, id, settings.config.output === 'hybrid'); + const pageOptions = await scan(code, id, settings); if (typeof pageOptions.prerender === 'undefined') { pageOptions.prerender = defaultPrerender; } + + // `getStaticPaths` warning is just a string check, should be good enough for most cases + if (!pageOptions.prerender && isServerLikeOutput(settings.config) && code.includes('getStaticPaths')) { + const reason = ` because \`output: "${settings.config.output}"\` is set` + warn(logging, "getStaticPaths", `The getStaticPaths() statement in ${bold(rootRelativePath(settings.config.root, fileURL, true))} has been ignored${reason}.\n\nAdd \`export const prerender = true;\` to prerender this page.`); + } const { meta = {} } = this.getModuleInfo(id) ?? {}; return { diff --git a/packages/astro/src/vite-plugin-scanner/scan.ts b/packages/astro/src/vite-plugin-scanner/scan.ts index 83ca3c5c5cde..69a0945d6bb7 100644 --- a/packages/astro/src/vite-plugin-scanner/scan.ts +++ b/packages/astro/src/vite-plugin-scanner/scan.ts @@ -1,6 +1,8 @@ +import type { PageOptions } from '../vite-plugin-astro/types.js'; +import type { AstroSettings } from '../@types/astro.js'; + import * as eslexer from 'es-module-lexer'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; -import type { PageOptions } from '../vite-plugin-astro/types.js'; const BOOLEAN_EXPORTS = new Set(['prerender']); @@ -34,7 +36,7 @@ function isFalsy(value: string) { let didInit = false; -export async function scan(code: string, id: string, isHybridOutput = false): Promise { +export async function scan(code: string, id: string, settings?: AstroSettings): Promise { if (!includesExport(code)) return {}; if (!didInit) { await eslexer.init; @@ -42,6 +44,7 @@ export async function scan(code: string, id: string, isHybridOutput = false): Pr } const [, exports] = eslexer.parse(code, id); + let pageOptions: PageOptions = {}; for (const _export of exports) { const { n: name, le: endOfLocalName } = _export; @@ -62,7 +65,7 @@ export async function scan(code: string, id: string, isHybridOutput = false): Pr if (prefix !== 'const' || !(isTruthy(suffix) || isFalsy(suffix))) { throw new AstroError({ ...AstroErrorData.InvalidPrerenderExport, - message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix, isHybridOutput), + message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix, settings?.config.output === 'hybrid' ?? false), location: { file: id }, }); } else { @@ -70,5 +73,6 @@ export async function scan(code: string, id: string, isHybridOutput = false): Pr } } } + return pageOptions; }