Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check whether app not-found is static #6558

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Added the ability to deploy Angular apps using [the new application-builder](https://angular.dev/tools/cli/esbuild). (#6480)
- Fixed an issue where `--non-interactive` flag is not respected in Firestore indexes deploys. (#6539)
- Fixed an issue where `login:use` would not work outside of a Firebase project directory. (#6526)
- Prevent app router static `not-found` requiring a Cloud Function in Next.js deployments. (#6558)
8 changes: 8 additions & 0 deletions src/frameworks/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
getHeadersFromMetaFiles,
cleanI18n,
getNextVersion,
hasStaticAppNotFoundComponent,
} from "./utils";
import { NODE_VERSION, NPM_COMMAND_TIMEOUT_MILLIES, SHARP_VERSION, I18N_ROOT } from "../constants";
import type {
Expand Down Expand Up @@ -81,13 +82,13 @@
const DEFAULT_NUMBER_OF_REASONS_TO_LIST = 5;

function getReactVersion(cwd: string): string | undefined {
return findDependency("react-dom", { cwd, omitDev: false })?.version;

Check warning on line 85 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe return of an `any` typed value

Check warning on line 85 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .version on an `any` value
}

/**
* Returns whether this codebase is a Next.js backend.
*/
export async function discover(dir: string) {

Check warning on line 91 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Missing return type on function
if (!(await pathExists(join(dir, "package.json")))) return;
if (!(await pathExists("next.config.js")) && !getNextVersion(dir)) return;

Expand All @@ -110,10 +111,10 @@

const nextBuild = new Promise((resolve, reject) => {
const buildProcess = spawn(cli, ["build"], { cwd: dir });
buildProcess.stdout?.on("data", (data) => logger.info(data.toString()));

Check warning on line 114 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe argument of type `any` assigned to a parameter of type `Error`

Check warning on line 114 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .toString on an `any` value

Check warning on line 114 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe call of an `any` typed value
buildProcess.stderr?.on("data", (data) => logger.info(data.toString()));

Check warning on line 115 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe argument of type `any` assigned to a parameter of type `Error`

Check warning on line 115 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .toString on an `any` value

Check warning on line 115 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe call of an `any` typed value
buildProcess.on("error", (err) => {
reject(new FirebaseError(`Unable to build your Next.js app: ${err}`));

Check warning on line 117 in src/frameworks/next/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Invalid type "Error" of template literal expression
});
buildProcess.on("exit", (code) => {
resolve(code);
Expand Down Expand Up @@ -215,6 +216,13 @@
dynamicRoutes
);

if (
unrenderedServerComponents.has("/_not-found") &&
(await hasStaticAppNotFoundComponent(dir, distDir))
) {
unrenderedServerComponents.delete("/_not-found");
}

for (const key of unrenderedServerComponents) {
reasonsForBackend.add(`non-static component ${key}`);
}
Expand Down
18 changes: 16 additions & 2 deletions src/frameworks/next/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export function getNonStaticServerComponents(
appPathRoutesManifest: AppPathRoutesManifest,
prerenderedRoutes: string[],
dynamicRoutes: string[]
): string[] {
): Set<string> {
const nonStaticServerComponents = Object.entries(appPathsManifest)
.filter(([it, src]) => {
if (extname(src) !== ".js") return;
Expand All @@ -347,7 +347,7 @@ export function getNonStaticServerComponents(
})
.map(([it]) => it);

return nonStaticServerComponents;
return new Set(nonStaticServerComponents);
}

/**
Expand Down Expand Up @@ -407,3 +407,17 @@ export function getNextVersion(cwd: string): string | undefined {

return nextVersionSemver.toString();
}

/**
* Whether the Next.js project has a static `not-found` page in the app directory.
*
* The Next.js build manifests are misleading regarding the existence of a static
* `not-found` component. Therefore, we check if a `_not-found.html` file exists
* in the generated app directory files to know whether `not-found` is static.
*/
export async function hasStaticAppNotFoundComponent(
sourceDir: string,
distDir: string
): Promise<boolean> {
return pathExists(join(sourceDir, distDir, "server", "app", "_not-found.html"));
}
2 changes: 1 addition & 1 deletion src/test/frameworks/next/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ describe("Next.js utils", () => {
Object.keys(prerenderManifest.routes),
Object.keys(prerenderManifest.dynamicRoutes)
)
).to.deep.equal(["/api/test/route"]);
).to.deep.equal(new Set(["/api/test/route"]));
});
});

Expand Down
Loading