Skip to content

feat: add support for skew protection #746

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 4 commits into from
Jul 10, 2025
Merged

feat: add support for skew protection #746

merged 4 commits into from
Jul 10, 2025

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Jun 20, 2025

There are 2 main files in this PR:

packages/cloudflare/src/cli/commands/skew-protection.ts (build time)

This builds a mapping from deploymentId to the worker version at build time.
Note that because the worker you are building has no version yet, "current" is used instead.

packages/cloudflare/src/cli/templates/skew-protection.ts (runtime)

At runtime, if a particular version is requested and it is present in the mapping,
we'll fetch the result from a preview URL with a hostname of <version>-<worker_name>.<domain>.workers.dev

How to use it:

  • set cloudflare.skewProtection.enabled to true in your OpenNext config
  • provide values for the new env vars added to packages/cloudflare/src/api/cloudflare-context.ts
  • update your next config to set the deployementId - you can use the getDeploymentId() helper
  • set run_worker_first to true

Docs PR opennextjs/docs#164

TODO:

Copy link

changeset-bot bot commented Jun 20, 2025

🦋 Changeset detected

Latest commit: 17064f4

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

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Minor

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

Copy link

pkg-pr-new bot commented Jun 20, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@746

commit: 17064f4

@vicb vicb force-pushed the vicb/skew-protection branch from 2356748 to 0a6f2dd Compare June 20, 2025 13:11
@vicb vicb requested review from conico974, james-elicx, dario-piotrowicz and sommeeeer and removed request for conico974 June 20, 2025 13:11
@vicb vicb force-pushed the vicb/skew-protection branch from 0a6f2dd to 18ba05a Compare June 20, 2025 13:14
@vicb vicb requested a review from IgorMinar June 20, 2025 13:26
Copy link
Collaborator

@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 just took a really quick look for now, will make a proper review later

Copy link
Collaborator

@james-elicx james-elicx 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 this is looking pretty good on my initial read though. Going to have another look later on as well.

Copy link
Collaborator

@james-elicx james-elicx 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 this looks good and the logic makes sense to me.

It might be nice for us to look at having an e2e that runs against a deployment at some point in the future, for testing features like this.

@vicb vicb force-pushed the vicb/skew-protection branch 4 times, most recently from 4c21542 to 471111c Compare June 23, 2025 12:13
@vicb vicb changed the title [Preview] Add support for skew protection Add support for skew protection Jun 24, 2025
Copy link
Collaborator

@sommeeeer sommeeeer left a comment

Choose a reason for hiding this comment

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

LGTM

@vicb
Copy link
Contributor Author

vicb commented Jun 24, 2025

Thanks for the review @sommeeeer !

Copy link
Collaborator

@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.

Just a few nits/question, other than that LGTM

* @param options Options to pass to `getPlatformProxy`, i.e. to set the environment
* @returns the env vars
*/
export async function getEnvFromPlatformProxy(options: GetPlatformProxyOptions) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should have only one instance of this, that we spawn at the beginning and that we dispose at the end. It's a bit wasteful to launch multiple workerd process.
And once we have support for remote bindings here, we could reuse it to populate the cache directly (which should be way faster)

path = path.slice(basePath.length);
}
if (path.startsWith("/_next/static/") || isFileInTree(path, __CF_ASSETS_TREE__)) {
return assets.fetch(request);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this one respect the _headers file ?

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 should but I'll test that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Follow up:

  • Check what Next does add by default and apply that in the asset resolver
  • Document how to override (using middleware or Next config)

@vicb vicb force-pushed the vicb/skew-protection branch from 2f1f47b to 45edc9f Compare June 30, 2025 12:50
@vicb vicb changed the title Add support for skew protection [WIP] Add support for skew protection Jul 4, 2025
@vicb vicb force-pushed the vicb/skew-protection branch from 43be1dd to 12c4094 Compare July 8, 2025 10:03
@vicb vicb force-pushed the vicb/skew-protection branch from 12c4094 to 637d8f4 Compare July 9, 2025 09:32
@vicb vicb changed the title [WIP] Add support for skew protection feat: add support for skew protection Jul 9, 2025
@vicb vicb force-pushed the vicb/skew-protection branch from 637d8f4 to ca41ac2 Compare July 9, 2025 09:39
@vicb
Copy link
Contributor Author

vicb commented Jul 9, 2025

I have rebase the PR on top of #768 as the asset resolver will be used to resolve the assets from previous versions (when run_worker_first=false the worker does not get a chance to override the assets as they are served even before the worker is executed).

Docs plan (today/tomorrow)

  • write some new doc for the asset resolver
  • update the existing doc for the skew protection to sync with this PR

@dario-piotrowicz @james-elicx @conico974 please take a look at the PR when you get a chance.

Thanks!

@vicb vicb requested review from conico974 and james-elicx July 9, 2025 09:42
Copy link
Collaborator

@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.

Just a few nits/question, but other than that LGTM.
BTW do we want to allow users to override the assets resolver ?

@@ -4,5 +4,7 @@
export const dynamic = "force-dynamic";

export async function GET() {
return new Response(JSON.stringify(process.env));
return new Response(JSON.stringify(process.env, null, 2), {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used this API for debugging (the env contains the build id, deployment id, and deployment mapping) and the response was easier to read when formatted this way.


const now = Date.now();

client.workers.scripts.versions.list = vi.fn().mockReturnValue([
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can the client return this in the wrong order ? This is not tested here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my experience, the client always returns an ordered lists.

BUT I agree we should not rely on the result of a few experiments.

That's why I pay special attention to not order the input (~2 is before ~1) and you can see that the output of listWorkerVersions is ordered (~2 after ~1)

@vicb
Copy link
Contributor Author

vicb commented Jul 9, 2025

BTW do we want to allow users to override the assets resolver ?

Yes we should but I agree that our current defineCloudflareConfig is not flexible enough:

export default {
	...defineCloudflareConfig({
		incrementalCache: r2IncrementalCache,
	}),
	cloudflare: {
		skewProtection: {
			enabled: false,
		},
	},
} satisfies OpenNextConfig;

As of today users can override the assets resolver but the syntax is not great.

I'll try to work on a better way to do that.

@vicb vicb merged commit 6d020fe into main Jul 10, 2025
7 of 8 checks passed
@vicb vicb deleted the vicb/skew-protection branch July 10, 2025 08:13
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.

4 participants