Skip to content

Add an asset resolver override #917

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

Merged
merged 6 commits into from
Jul 1, 2025
Merged

Add an asset resolver override #917

merged 6 commits into from
Jul 1, 2025

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Jun 26, 2025

fixes #707

We plan to use that for the Cloudflare adapter when the worker runs in front of the assets (see )

The current override looks like:

const resolver: AssetResolver = {
	name: "cloudflare-worker-first",
	async maybeGetAssetResult(event: InternalEvent) {
		const { ASSETS } = getCloudflareContext().env;
		if (!ASSETS) {
			return undefined;
		}
		const { method, headers } = event;
		if (method !== "GET" && method != "HEAD") {
			return undefined;
		}

		const response = await ASSETS.fetch(new URL(event.rawPath, "https://assets.local"), {
			headers,
			method,
		});

		if (response.status === 404) {
			return undefined;
		}

		const body = response.body || new ReadableStream();

		return {
			type: "core",
			statusCode: response.status,
			headers: Object.fromEntries(response.headers.entries()),
			// Workers and Node types differ.
			// eslint-disable-next-line @typescript-eslint/no-explicit-any
			body: body as any,
			isBase64Encoded: false,
		} satisfies InternalResult;
	},
};

Note I think beforeFiles and afterFiles behavior do not match the Next doc - they all should be applied (vs only the first match in ON).

Copy link

changeset-bot bot commented Jun 26, 2025

🦋 Changeset detected

Latest commit: 1dfa027

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Minor
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vicb vicb marked this pull request as draft June 26, 2025 16:47
Copy link

pkg-pr-new bot commented Jun 26, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@917

commit: 1dfa027

Copy link
Contributor

github-actions bot commented Jun 26, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 26.97% 2639 / 9784
🔵 Statements 26.97% 2639 / 9784
🔵 Functions 54.26% 140 / 258
🔵 Branches 73.23% 621 / 848
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/open-next/src/adapters/middleware.ts 0% 0% 0% 0% 1-129
packages/open-next/src/core/createMainHandler.ts 0% 0% 0% 0% 1-67
packages/open-next/src/core/requestHandler.ts 0% 0% 0% 0% 1-347
packages/open-next/src/core/resolve.ts 0% 100% 100% 0% 14-170
packages/open-next/src/core/routingHandler.ts 0% 0% 0% 0% 1-294
packages/open-next/src/core/routing/matcher.ts 73.55% 73.46% 92.3% 73.55% 39-41, 50-54, 59-61, 64, 103, 105, 108, 111, 121-122, 164-166, 237-244, 315-317, 319-332, 351-352, 424-486
packages/open-next/src/overrides/assetResolver/dummy.ts 0% 100% 100% 0% 8-12
packages/open-next/src/types/global.ts 0% 0% 0% 0%
packages/open-next/src/types/open-next.ts 0% 0% 0% 0%
packages/open-next/src/types/overrides.ts 0% 0% 0% 0%
Generated in workflow #1346 for commit 1dfa027 by the Vitest Coverage Report Action

@vicb vicb force-pushed the vicb/asset-resolver branch from 6b4c1e1 to eb42c67 Compare June 30, 2025 09:48
@vicb vicb requested a review from conico974 June 30, 2025 10:20
@vicb vicb marked this pull request as ready for review June 30, 2025 10:20
@vicb vicb force-pushed the vicb/asset-resolver branch from eb42c67 to 62a2bac Compare June 30, 2025 10:23
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this pull request Jun 30, 2025
Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't have a choice, but to check every single static assets in maybeGetAssetResult

@@ -170,6 +170,11 @@ export function getNextConfigHeaders(
return requestHeaders;
}

/**
* TODO: This method currently only check for the first match.
* It should check for all matches for `beforeFiles` and `afterFiles` rewrite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it for all or only for beforeFiles ? The docs talks about beforeFiles only for that.
We need to look at how this is done in Next, but yeah definitely needs to be fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a few lines appart but

Good to know: rewrites in beforeFiles do not check the filesystem/dynamic routes immediately after matching a source, they continue until all beforeFiles have been checked.

and

afterFiles rewrites are checked/applied, if one of these rewrites is matched we check dynamic routes/static files after each match

Copy link
Contributor Author

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review

I'll check the implementation and update the PR

@@ -170,6 +170,11 @@ export function getNextConfigHeaders(
return requestHeaders;
}

/**
* TODO: This method currently only check for the first match.
* It should check for all matches for `beforeFiles` and `afterFiles` rewrite
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a few lines appart but

Good to know: rewrites in beforeFiles do not check the filesystem/dynamic routes immediately after matching a source, they continue until all beforeFiles have been checked.

and

afterFiles rewrites are checked/applied, if one of these rewrites is matched we check dynamic routes/static files after each match

@vicb
Copy link
Contributor Author

vicb commented Jul 1, 2025

@conico974 I think I have addressed all the feedback, could you please take another look?

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks!

@vicb vicb merged commit 60848d9 into main Jul 1, 2025
3 checks passed
@vicb vicb deleted the vicb/asset-resolver branch July 1, 2025 09:27
@vicb
Copy link
Contributor Author

vicb commented Jul 1, 2025

Thanks for the review and feedback Nico!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add an override to handle asset in the routing layer
2 participants