Skip to content

Commit

Permalink
improve an error message for getstaticpaths (#4153)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Aug 5, 2022
1 parent 9c7021f commit 3321aac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-bananas-refuse.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improve an error message for getStaticPaths
40 changes: 22 additions & 18 deletions packages/astro/src/core/render/route-cache.ts
Expand Up @@ -9,10 +9,7 @@ import type {
import { debug, LogOptions, warn } from '../logger/core.js';

import { stringifyParams } from '../routing/params.js';
import {
validateGetStaticPathsModule,
validateGetStaticPathsResult,
} from '../routing/validation.js';
import { validateDynamicRouteModule, validateGetStaticPathsResult } from '../routing/validation.js';
import { generatePaginateFunction } from './paginate.js';

interface CallGetStaticPathsOptions {
Expand All @@ -30,21 +27,28 @@ export async function callGetStaticPaths({
route,
ssr,
}: CallGetStaticPathsOptions): Promise<RouteCacheEntry> {
validateGetStaticPathsModule(mod, { ssr });

let staticPaths: GetStaticPathsResult = [];
if (mod.getStaticPaths) {
staticPaths = (
await mod.getStaticPaths({
paginate: generatePaginateFunction(route),
rss() {
throw new Error(
'The RSS helper has been removed from getStaticPaths! Try the new @astrojs/rss package instead. See https://docs.astro.build/en/guides/rss/'
);
},
})
).flat();
validateDynamicRouteModule(mod, { ssr, logging });
// No static paths in SSR mode. Return an empty RouteCacheEntry.
if (ssr) {
return { staticPaths: Object.assign([], { keyed: new Map() }) };
}
// Add a check here to my TypeScript happy.
// This is already checked in validateDynamicRouteModule().
if (!mod.getStaticPaths) {
throw new Error('Unexpected Error.');
}
// Calculate your static paths.
let staticPaths: GetStaticPathsResult = [];
staticPaths = (
await mod.getStaticPaths({
paginate: generatePaginateFunction(route),
rss() {
throw new Error(
'The RSS helper has been removed from getStaticPaths! Try the new @astrojs/rss package instead. See https://docs.astro.build/en/guides/rss/'
);
},
})
).flat();

const keyedStaticPaths = staticPaths as GetStaticPathsResultKeyed;
keyedStaticPaths.keyed = new Map<string, GetStaticPathsItem>();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/index.ts
Expand Up @@ -2,4 +2,4 @@ export { createRouteManifest } from './manifest/create.js';
export { deserializeRouteData, serializeRouteData } from './manifest/serialization.js';
export { matchAllRoutes, matchRoute } from './match.js';
export { getParams } from './params.js';
export { validateGetStaticPathsModule, validateGetStaticPathsResult } from './validation.js';
export { validateDynamicRouteModule, validateGetStaticPathsResult } from './validation.js';
26 changes: 18 additions & 8 deletions packages/astro/src/core/routing/validation.ts
Expand Up @@ -4,10 +4,6 @@ import { warn } from '../logger/core.js';

const VALID_PARAM_TYPES = ['string', 'number', 'undefined'];

interface ValidationOptions {
ssr: boolean;
}

/** Throws error for invalid parameter in getStaticPaths() response */
export function validateGetStaticPathsParameter([key, value]: [string, any]) {
if (!VALID_PARAM_TYPES.includes(typeof value)) {
Expand All @@ -17,14 +13,28 @@ export function validateGetStaticPathsParameter([key, value]: [string, any]) {
}
}

/** Throw error for deprecated/malformed APIs */
export function validateGetStaticPathsModule(mod: ComponentInstance, { ssr }: ValidationOptions) {
/** Warn or error for deprecated or malformed route components */
export function validateDynamicRouteModule(
mod: ComponentInstance,
{
ssr,
logging,
}: {
ssr: boolean;
logging: LogOptions;
}
) {
if ((mod as any).createCollection) {
throw new Error(`[createCollection] deprecated. Please use getStaticPaths() instead.`);
}
if (!mod.getStaticPaths && !ssr) {
if (ssr && mod.getStaticPaths) {
warn(logging, 'getStaticPaths', 'getStaticPaths() is ignored when "output: server" is set.');
}
if (!ssr && !mod.getStaticPaths) {
throw new Error(
`[getStaticPaths] getStaticPaths() function is required. Make sure that you \`export\` the function from your component.`
`[getStaticPaths] getStaticPaths() function is required.
Make sure that you \`export\` a \`getStaticPaths\` function from your dynamic route.
Alternatively, set \`output: "server"\` in your Astro config file to switch to a non-static server build. `
);
}
}
Expand Down

0 comments on commit 3321aac

Please sign in to comment.