Skip to content

Commit

Permalink
Add a workaround for the not-found app directory issue (cloudflare#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz authored Aug 9, 2023
1 parent 23d4dd6 commit 63ef00e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/buildApplication/processVercelFunctions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { processEdgeFunctions } from './edgeFunctions';
import { processPrerenderFunctions } from './prerenderFunctions';
import type { CollectedFunctionIdentifiers } from './dedupeEdgeFunctions';
import { dedupeEdgeFunctions } from './dedupeEdgeFunctions';
import { checkForInvalidFunctions } from './invalidFunctions';
import { checkInvalidFunctions } from './invalidFunctions';

/**
* Processes and dedupes the Vercel build output functions directory.
Expand All @@ -23,7 +23,7 @@ export async function processVercelFunctions(

await processEdgeFunctions(collectedFunctions);

await checkForInvalidFunctions(collectedFunctions);
await checkInvalidFunctions(collectedFunctions);

const identifiers = await dedupeEdgeFunctions(collectedFunctions, opts);

Expand Down
55 changes: 52 additions & 3 deletions src/buildApplication/processVercelFunctions/invalidFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { gtr as versionGreaterThan, coerce } from 'semver';
import { cliError } from '../../cli';
import { cliError, cliWarn } from '../../cli';
import { getPackageVersion } from '../packageManagerUtils';
import { stripFuncExtension } from '../../utils';
import type { CollectedFunctions, FunctionInfo } from './configs';
import { join, resolve } from 'path';

/**
* Checks if there are any invalid functions from the Vercel build output.
*
* If there are any invalid functions, an error message will be printed and the process will exit.
* If there are any invalid functions it will try to see if they are amendable and if the
* build output can still be used.
*
* If however the build output can't be used, an error message will be printed and the process will exit.
*
* @param collectedFunctions Collected functions from the Vercel build output.
*/
export async function checkForInvalidFunctions(
export async function checkInvalidFunctions(
collectedFunctions: CollectedFunctions,
): Promise<void> {
await tryToFixNotFoundRoute(collectedFunctions);

if (collectedFunctions.invalidFunctions.size > 0) {
await printInvalidFunctionsErrorMessage(
collectedFunctions.invalidFunctions,
Expand All @@ -22,6 +28,49 @@ export async function checkForInvalidFunctions(
}
}

/**
* Tries to fix potential not-found invalid functions from the Vercel build output.
*
* Static app/not-found.(jsx|tsx) pages generate an _not-found.func serverless function,
* that can be removed as we can fallback to the statically generated 404 page
*
* If the app/not-found.(jsx|tsx) contains runtime logic alongside the _not-found.func serverless
* function also an _error.func will be generated, in such a case we can only warn the user about
* it.
* (
* That's the only option because:
* - removing the _not-found.func and _error.func doesn't result in a working application
* - we don't have a guarantee that the _error.func hasn't been generated by something else
* and that the _not-found.func is that of a static app/not-found route
* )
*
* @param collectedFunctions Collected functions from the Vercel build output.
*/
async function tryToFixNotFoundRoute(
collectedFunctions: CollectedFunctions,
): Promise<void> {
const functionsDir = resolve('.vercel', 'output', 'functions');
const notFoundDir = join(functionsDir, '_not-found.func');
const errorDir = join(functionsDir, '_error.func');

const invalidNotFound = collectedFunctions.invalidFunctions.get(notFoundDir);
const invalidError = collectedFunctions.invalidFunctions.get(errorDir);

if (invalidNotFound && !invalidError) {
collectedFunctions.invalidFunctions.delete(notFoundDir);
const notFoundRscDir = join(functionsDir, '_not-found.rsc.func');
collectedFunctions.invalidFunctions.delete(notFoundRscDir);
}

if (invalidNotFound && invalidError) {
cliWarn(`
Warning: your app/not-found route might contain runtime logic, this is currently
not supported by @cloudflare/next-on-pages, if that's actually the case please
remove the runtime logic from your not-found route
`);
}
}

/**
* Prints an error message for the invalid functions from the Vercel build output.
*
Expand Down

0 comments on commit 63ef00e

Please sign in to comment.