summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2023-07-19 09:33:45 -0500
committerGravatar GitHub <noreply@github.com> 2023-07-19 09:33:45 -0500
commitd088351f54d2518e2bb539d7bbf8691427ff8a7a (patch)
tree5b0f19bed608f94926e3ea4fa800db275e69ae12
parent4eba967d2b7e0593ed49f191b70dfea7a43e9829 (diff)
downloadastro-d088351f54d2518e2bb539d7bbf8691427ff8a7a.tar.gz
astro-d088351f54d2518e2bb539d7bbf8691427ff8a7a.tar.zst
astro-d088351f54d2518e2bb539d7bbf8691427ff8a7a.zip
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
-rw-r--r--.changeset/sixty-plums-appear.md5
-rw-r--r--packages/astro/src/core/create-vite.ts2
-rw-r--r--packages/astro/src/core/render/route-cache.ts2
-rw-r--r--packages/astro/src/core/routing/validation.ts12
-rw-r--r--packages/astro/src/vite-plugin-scanner/index.ts25
-rw-r--r--packages/astro/src/vite-plugin-scanner/scan.ts10
6 files changed, 35 insertions, 21 deletions
diff --git a/.changeset/sixty-plums-appear.md b/.changeset/sixty-plums-appear.md
new file mode 100644
index 000000000..3b8150ca0
--- /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 279a7ffde..52b5fd277 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 71b2966de..7ad247ef8 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 1a3bab82a..9a562c044 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 465ff74ed..439a56d46 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 83ca3c5c5..69a0945d6 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<PageOptions> {
+export async function scan(code: string, id: string, settings?: AstroSettings): Promise<PageOptions> {
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;
}