Skip to content

[Preview] 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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

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

TODO:

  • add tests
  • handle assets (will need to run the worker before the asset worker)
  • follow-up: move the skew protection logic in the aws/routing layer with pluggable overrides

Copy link

changeset-bot bot commented Jun 20, 2025

⚠️ No Changeset found

Latest commit: b373c64

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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: b373c64

@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

}

const versionDomain = version.split("-")[0];
const hostname = `${versionDomain}-${process.env.CF_WORKER_NAME}.${process.env.CF_PREVIEW_DOMAIN}.workers.dev`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this one will work if people uses durable object, for these cases you could use Cloudflare-Workers-Version-Overrides headers with the worker binding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cloudflare-Workers-Version-Overrides can only access one of 2 versions in a gradual deployment :(

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.

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.

3 participants