Skip to content

Commit

Permalink
fix: forbid isr + edge for same endpoint. fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Aug 20, 2024
1 parent 08090e6 commit a930ad4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/vercel/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ export async function buildEndpoints(resolvedConfig: ResolvedConfig): Promise<{
);
}

if (
(entry.isr !== undefined || exports.isr !== undefined) &&
(entry.edge !== undefined || exports.edge !== undefined)
) {
throw new Error(`isr cannot be enabled for edge functions ('${entry.source}')`);
}

if (exports.isr) {
entry.isr = exports.isr;
}
Expand Down
18 changes: 16 additions & 2 deletions packages/vike-integration/vike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function assertEdge(exports: unknown): boolean | null {
return edge;
}

function assertIsr(resolvedConfig: UserConfig | ResolvedConfig, exports: unknown): number | null {
function extractIsr(exports: unknown) {
if (exports === null || typeof exports !== "object") return null;
if (!("isr" in exports)) return null;
const isr = (exports as { isr: unknown }).isr;
Expand All @@ -127,6 +127,13 @@ function assertIsr(resolvedConfig: UserConfig | ResolvedConfig, exports: unknown
" `{ expiration }` must be a positive number",
);

return isr;
}

function assertIsr(resolvedConfig: UserConfig | ResolvedConfig, exports: unknown): number | null {
const isr = extractIsr(exports);
if (isr === null || isr === undefined) return null;

if (isr === true) {
assert(
typeof resolvedConfig.vercel?.expiration === "number" && resolvedConfig.vercel?.expiration > 0,
Expand Down Expand Up @@ -421,17 +428,24 @@ export function vitePluginVercelVikeConfigPlugin(): Plugin {
}

const route = getRouteDynamicRoute(pageRoutes, pageId) ?? getRouteFsRoute(pageRoutes, pageId);
const rawIsr = extractIsr(page.config);
let isr = assertIsr(userConfig, page.config);
const edge = assertEdge(page.config);

// if ISR + Function routing -> warn because ISR is not unsupported in this case
if (typeof route === "function" && isr) {
console.warn(
`Page ${pageId}: ISR is not supported when using route function. Remove \`{ isr }\` export or use a route string if possible.`,
`Page ${pageId}: ISR is not supported when using route function. Remove \`{ isr }\` config or use a route string if possible.`,
);
isr = null;
}

if (edge && rawIsr !== null && typeof rawIsr === "object") {
throw new Error(
`Page ${pageId}: ISR cannot be enabled for edge functions. Remove \`{ isr }\` config or set \`{ edge: false }\`.`,
);
}

return {
_pageId: pageId,
// used for debug purpose
Expand Down

0 comments on commit a930ad4

Please sign in to comment.