Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4602ad6
progress
mathu97 Feb 24, 2025
50297f5
add an overrides file to explicitely override nextjs defaults for app…
mathu97 Feb 24, 2025
5df4372
move adapterMetadata to common func
mathu97 Feb 24, 2025
2c7a1ca
check for middleware usage and add header
mathu97 Feb 24, 2025
62a71db
look at middleware manifest to check if middleware exists
mathu97 Feb 25, 2025
3662682
fix path
mathu97 Feb 25, 2025
b77e40c
fix test
mathu97 Feb 25, 2025
8dbf472
fix lint error
mathu97 Feb 25, 2025
8ac3016
bump nextjs adapter
mathu97 Feb 25, 2025
2e6d383
fix next app version test
mathu97 Feb 25, 2025
8b5b018
update package-lock
mathu97 Feb 25, 2025
005b780
update patch version instead of minor
mathu97 Feb 25, 2025
e2d7cc5
replace .eslintrc.json with eslint.config.mjs (preferred for next 15)
mathu97 Feb 25, 2025
e16ed40
move from using .eslintrc.json to eslint.config.mjs (preferred for ne…
mathu97 Feb 25, 2025
b14ccb4
fix e2e test
mathu97 Feb 25, 2025
01bf2b7
attempt2
mathu97 Feb 25, 2025
1387bc9
fix local test
mathu97 Feb 25, 2025
f0d7595
Merge branch 'bug/fix-nextjs-adapter-functional-tests' into feat/cust…
mathu97 Feb 25, 2025
23a5efc
add unit tests and e2e tests to test route overrides
mathu97 Feb 25, 2025
461b27a
add jsdocs
mathu97 Feb 25, 2025
f88c546
Merge remote-tracking branch 'origin/main' into feat/custom-apphostin…
mathu97 Feb 25, 2025
f4b045d
add jsdoc for interfaces
mathu97 Feb 25, 2025
c658e2c
address comments
mathu97 Feb 26, 2025
852da0d
refactor e2e scaffolding to be able to setup nextjs apps to test diff…
mathu97 Feb 26, 2025
564fc58
fix e2e tests failing for older node versions
mathu97 Feb 26, 2025
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions packages/@apphosting/adapter-nextjs/e2e/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import * as assert from "assert";
import { posix } from "path";
import fsExtra from "fs-extra";

export const host = process.env.HOST;

const { readJson } = fsExtra;
let adapterVersion: string;

before(async () => {
const packageJson = await readJson("package.json");
adapterVersion = packageJson.version;
if (!adapterVersion) throw new Error("couldn't parse package.json version");
});

if (!host) {
throw new Error("HOST environment variable expected");
}
Expand Down Expand Up @@ -114,4 +124,30 @@ describe("app", () => {
"private, no-cache, no-store, max-age=0, must-revalidate",
);
});

it("should have x-fah-adapter header and no x-fah-middleware header on all routes", async () => {
const routes = [
"/",
"/ssg",
"/ssr",
"/ssr/streaming",
"/isr/time",
"/isr/demand",
"/nonexistent-route",
];

for (const route of routes) {
const response = await fetch(posix.join(host, route));
assert.equal(
response.headers.get("x-fah-adapter"),
`nextjs-${adapterVersion}`,
`Route ${route} missing x-fah-adapter header`,
);
assert.equal(
response.headers.get("x-fah-middleware"),
null,
`Route ${route} should not have x-fah-middleware header`,
);
}
});
});
4 changes: 2 additions & 2 deletions packages/@apphosting/adapter-nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apphosting/adapter-nextjs",
"version": "14.0.9",
"version": "14.0.10",
"main": "dist/index.js",
"description": "Experimental addon to the Firebase CLI to add web framework support",
"repository": {
Expand All @@ -23,7 +23,7 @@
"scripts": {
"build": "rm -rf dist && tsc && chmod +x ./dist/bin/*",
"test": "npm run test:unit && npm run test:functional",
"test:unit": "ts-mocha -p tsconfig.json src/**/*.spec.ts",
"test:unit": "ts-mocha -p tsconfig.json 'src/**/*.spec.ts' 'src/*.spec.ts'",
"test:functional": "node --loader ts-node/esm ./e2e/run-local.ts",
"localregistry:start": "npx verdaccio --config ../publish-dev/verdaccio-config.yaml",
"localregistry:publish": "(npm view --registry=http://localhost:4873 @apphosting/adapter-nextjs && npm unpublish --@apphosting:registry=http://localhost:4873 --force); npm publish --@apphosting:registry=http://localhost:4873"
Expand Down
32 changes: 23 additions & 9 deletions packages/@apphosting/adapter-nextjs/src/bin/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ describe("build commands", () => {
});

it("expects all output bundle files to be generated", async () => {
const { generateBuildOutput, validateOutputDirectory, createMetadata } = await importUtils;
const { generateBuildOutput, validateOutputDirectory, getAdapterMetadata } = await importUtils;
const files = {
".next/standalone/server.js": "",
".next/static/staticfile": "",
".next/routes-manifest.json": `{
"headers":[],
"rewrites":[],
"headers":[],
"rewrites":[],
"redirects":[]
}`,
};
const packageVersion = createMetadata(defaultNextVersion).adapterVersion;
const adapterMetadata = getAdapterMetadata();
generateTestFiles(tmpDir, files);
await generateBuildOutput(
tmpDir,
tmpDir,
outputBundleOptions,
path.join(tmpDir, ".next"),
defaultNextVersion,
adapterMetadata,
);
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));

Expand All @@ -53,7 +54,7 @@ runConfig:
runCommand: node .next/standalone/server.js
metadata:
adapterPackageName: "@apphosting/adapter-nextjs"
adapterVersion: ${packageVersion}
adapterVersion: ${adapterMetadata.adapterVersion}
framework: nextjs
frameworkVersion: ${defaultNextVersion}
`,
Expand Down Expand Up @@ -97,6 +98,10 @@ metadata:
},
path.join(tmpDir, ".next"),
defaultNextVersion,
{
adapterPackageName: "@apphosting/adapter-nextjs",
adapterVersion: "14.0.1",
},
);

const expectedFiles = {
Expand All @@ -117,8 +122,8 @@ metadata:
".next/standalone/notserver.js": "",
".next/static/staticfile": "",
".next/routes-manifest.json": `{
"headers":[{"source":"source", "headers":["header1"]}],
"rewrites":[{"source":"source", "destination":"destination"}],
"headers":[{"source":"source", "headers":["header1"]}],
"rewrites":[{"source":"source", "destination":"destination"}],
"redirects":[{"source":"source", "destination":"destination"}]
}`,
};
Expand All @@ -129,6 +134,10 @@ metadata:
outputBundleOptions,
path.join(tmpDir, ".next"),
defaultNextVersion,
{
adapterPackageName: "@apphosting/adapter-nextjs",
adapterVersion: "14.0.1",
},
);
assert.rejects(
async () => await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next")),
Expand All @@ -142,8 +151,8 @@ metadata:
"public/publicfile": "",
extrafile: "",
".next/routes-manifest.json": `{
"headers":[],
"rewrites":[],
"headers":[],
"rewrites":[],
"redirects":[]
}`,
};
Expand All @@ -154,6 +163,10 @@ metadata:
outputBundleOptions,
path.join(tmpDir, ".next"),
defaultNextVersion,
{
adapterPackageName: "@apphosting/adapter-nextjs",
adapterVersion: "14.0.1",
},
);
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));

Expand All @@ -180,6 +193,7 @@ metadata:
expectedOutputBundleOptions,
);
});

afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
Expand Down
8 changes: 7 additions & 1 deletion packages/@apphosting/adapter-nextjs/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
populateOutputBundleOptions,
generateBuildOutput,
validateOutputDirectory,
getAdapterMetadata,
} from "../utils.js";
import { join } from "path";
import { getBuildOptions, runBuild } from "@apphosting/common";
import { addRouteOverrides } from "../overrides.js";

const root = process.cwd();
const opts = getBuildOptions();
Expand All @@ -20,20 +22,24 @@ if (!process.env.FRAMEWORK_VERSION) {
}
await runBuild();

const adapterMetadata = getAdapterMetadata();

const { distDir } = await loadConfig(root, opts.projectDirectory);
const nextBuildDirectory = join(opts.projectDirectory, distDir);

const outputBundleOptions = populateOutputBundleOptions(
root,
opts.projectDirectory,
nextBuildDirectory,
);

await addRouteOverrides(outputBundleOptions.outputDirectoryAppPath, distDir, adapterMetadata);

await generateBuildOutput(
root,
opts.projectDirectory,
outputBundleOptions,
nextBuildDirectory,
process.env.FRAMEWORK_VERSION,
adapterMetadata,
);
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);
2 changes: 2 additions & 0 deletions packages/@apphosting/adapter-nextjs/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {
PHASE_PRODUCTION_BUILD as NEXT_PHASE_PRODUCTION_BUILD,
ROUTES_MANIFEST as NEXT_ROUTES_MANIFEST,
MIDDLEWARE_MANIFEST as NEXT_MIDDLEWARE_MANIFEST,
} from "next/constants.js";

// export next/constants ourselves so we don't have to dynamically import them
// also this gives us a better heads up if the NextJS API changes
export const PHASE_PRODUCTION_BUILD: typeof NEXT_PHASE_PRODUCTION_BUILD = "phase-production-build";
export const ROUTES_MANIFEST: typeof NEXT_ROUTES_MANIFEST = "routes-manifest.json";
export const MIDDLEWARE_MANIFEST: typeof NEXT_MIDDLEWARE_MANIFEST = "middleware-manifest.json";
47 changes: 44 additions & 3 deletions packages/@apphosting/adapter-nextjs/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export interface RoutesManifest {
localeDetection?: false;
};
}

// The output bundle options are specified here
export interface OutputBundleOptions {
/**
Expand Down Expand Up @@ -102,10 +101,52 @@ export interface OutputBundleOptions {
outputStaticDirectoryPath: string;
}

// Metadata schema for bundle.yaml outputted by next.js adapter
export interface Metadata {
// Metadata schema for adapter metadata
export interface AdapterMetadata {
adapterPackageName: string;
adapterVersion: string;
}

// Metadata schema for bundle.yaml outputted by next.js adapter
export interface Metadata extends AdapterMetadata {
framework: string;
frameworkVersion: string;
}

// Metadata schema for asset bindings
export interface AssetBinding {
filePath: string;
name: string;
}

// Metadata schema for middleware matchers
export interface MiddlewareMatcher {
regexp: string;
locale?: false;
has?: RouteHas[];
missing?: RouteHas[];
originalSource: string;
}

// Metadata schema for edge function definitions
export interface EdgeFunctionDefinition {
files: string[];
name: string;
page: string;
matchers: MiddlewareMatcher[];
wasm?: AssetBinding[];
assets?: AssetBinding[];
regions?: string[] | string;
}

// Metadata schema for middleware manifest
export interface MiddlewareManifest {
version: number;
sortedMiddleware: string[];
middleware: {
[page: string]: EdgeFunctionDefinition;
};
functions: {
[page: string]: EdgeFunctionDefinition;
};
}
Loading