Skip to content

Commit

Permalink
Add CLI option to ignore invalid functions
Browse files Browse the repository at this point in the history
The presence of invalid functions shouldn't force a build failure as a
deployment can still be valid (and even correct as outlined in cloudflare#845)
with invalid functions.

This commit adds a `--force` cli option (bike-shedding welcomed) that
allows users to opt to ignore invalid functions (and skip more checks in
the future?).

Closes cloudflare#845
  • Loading branch information
nickbabcock committed Aug 27, 2024
1 parent 12080f6 commit f897cc9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/next-on-pages/src/buildApplication/buildApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function buildApplication({
watch,
outdir: outputDir,
customEntrypoint,
force,
}: Pick<
CliOptions,
| 'skipBuild'
Expand All @@ -38,6 +39,7 @@ export async function buildApplication({
| 'watch'
| 'outdir'
| 'customEntrypoint'
| 'force'
>) {
const pm = await getPackageManager();

Expand Down Expand Up @@ -87,6 +89,7 @@ export async function buildApplication({
disableChunksDedup,
disableWorkerMinification,
customEntrypoint,
force,
});

const totalBuildTime = ((Date.now() - buildStartTime) / 1000).toFixed(2);
Expand All @@ -99,9 +102,13 @@ async function prepareAndBuildWorker(
disableChunksDedup,
disableWorkerMinification,
customEntrypoint,
force,
}: Pick<
CliOptions,
'disableChunksDedup' | 'disableWorkerMinification' | 'customEntrypoint'
| 'disableChunksDedup'
| 'disableWorkerMinification'
| 'customEntrypoint'
| 'force'
>,
): Promise<void> {
let vercelConfig: VercelConfig;
Expand Down Expand Up @@ -137,6 +144,7 @@ async function prepareAndBuildWorker(
nopDistDir,
disableChunksDedup,
vercelConfig,
ignoreInvalidFunctions: force,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type ProcessVercelFunctionsOpts = {
nopDistDir: string;
disableChunksDedup?: boolean;
vercelConfig: VercelConfig;
ignoreInvalidFunctions: boolean;
};

export type ProcessedVercelFunctions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isUsingAppRouter, isUsingPagesRouter } from '../getVercelConfig';
type InvalidFunctionsOpts = Pick<
ProcessVercelFunctionsOpts,
'functionsDir' | 'vercelConfig'
>;
> & { ignoreInvalidFunctions?: boolean };

/**
* Checks if there are any invalid functions from the Vercel build output.
Expand Down Expand Up @@ -46,7 +46,10 @@ export async function checkInvalidFunctions(
await tryToFixInvalidFuncsWithValidIndexAlternative(collectedFunctions);
await tryToFixInvalidDynamicISRFuncs(collectedFunctions);

if (collectedFunctions.invalidFunctions.size > 0) {
if (
collectedFunctions.invalidFunctions.size > 0 &&
!opts.ignoreInvalidFunctions
) {
await printInvalidFunctionsErrorMessage(
collectedFunctions.invalidFunctions,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/next-on-pages/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ program
'--custom-entrypoint <path>',
'Wrap the generated worker for your application in a custom worker entrypoint',
)
.option('-f, --force', 'Ignore checks for edge runtime compatibility', false)
.enablePositionalOptions(false)
.version(
nextOnPagesVersion,
Expand All @@ -79,6 +80,7 @@ export type CliOptions = {
info?: boolean;
outdir: string;
customEntrypoint?: string;
force: boolean;
};

export function parseCliArgs(): CliOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,54 @@ describe('checkInvalidFunctions', () => {
mockedConsoleError.restore();
mockedConsoleWarn.restore();
});

test('should ignore dynamic isr routes when there are no prerendered children', async () => {
const processExitMock = vi
.spyOn(process, 'exit')
.mockImplementation(async () => undefined as never);

const { collectedFunctions, restoreFsMock } = await collectFunctionsFrom({
functions: {
'[dynamic-1].func': prerenderFuncDir,
'edge-route.func': edgeFuncDir,
},
});

const opts = {
functionsDir,
outputDir: resolve('.vercel/output/static'),
vercelConfig: { version: 3 as const },
ignoreInvalidFunctions: true,
};

await processEdgeFunctions(collectedFunctions);
await processPrerenderFunctions(collectedFunctions, opts);
await checkInvalidFunctions(collectedFunctions, opts);
restoreFsMock();

const {
edgeFunctions,
prerenderedFunctions,
invalidFunctions,
ignoredFunctions,
} = collectedFunctions;

expect(edgeFunctions.size).toEqual(1);
expect(prerenderedFunctions.size).toEqual(0);
expect(invalidFunctions.size).toEqual(1);
expect(ignoredFunctions.size).toEqual(0);

expect(getRouteInfo(edgeFunctions, 'edge-route.func')).toEqual({
path: '/edge-route',
overrides: [],
});

expect([...invalidFunctions.keys()]).toEqual([
resolve(functionsDir, '[dynamic-1].func'),
]);

expect(processExitMock).not.toBeCalled();

processExitMock.mockRestore();
});
});

0 comments on commit f897cc9

Please sign in to comment.